dsh-subagent-dispatch-control 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +183 -2
- package/client.js +9781 -0
- package/cordis.patch.yml +13 -0
- package/docs/BOOST-EVIDENCE.md +259 -0
- package/docs/CONFORMANCE.md +236 -0
- package/docs/FACTS-CONTRACT.md +368 -0
- package/docs/FLOW-COVERAGE.md +215 -0
- package/docs/PACKAGE-README.md +858 -0
- package/docs/RANGE-PLAN.md +357 -0
- package/docs/SECURITY-NOTES.md +110 -0
- package/docs/SPLIT-PLAN.md +604 -0
- package/docs/STAGE3-CONTRACT.md +287 -0
- package/docs/TIER-ROUTING.md +275 -0
- package/docs/badge/CONFORMANCE.md +246 -0
- package/docs/badge/DETAIL.md +319 -0
- package/docs/badge/PROJECTIONS.md +264 -0
- package/docs/badge/TEST-PLAN.md +252 -0
- package/docs/images/badge-menu.png +0 -0
- package/docs/images/dispatch.png +0 -0
- package/docs/images/header.png +0 -0
- package/docs/images/pair-header.png +0 -0
- package/docs/images/pair-panel.png +0 -0
- package/docs/images/panel.png +0 -0
- package/docs/wiki/Home.md +43 -0
- package/docs/wiki//344/275/277/347/224/250/346/214/207/345/215/227.md +97 -0
- package/docs/wiki//345/256/211/350/243/205/344/270/216/345/215/207/347/272/247.md +87 -0
- package/docs/wiki//346/225/205/351/232/234/346/216/222/346/237/245.md +94 -0
- package/icon.svg +12 -0
- package/lib/badge-policy.mjs +502 -0
- package/lib/badge-sync.mjs +385 -0
- package/lib/command.mjs +490 -0
- package/lib/completion.mjs +288 -0
- package/lib/concurrency.mjs +2555 -0
- package/lib/dispatch-command.mjs +181 -0
- package/lib/dispatch-gate.mjs +924 -0
- package/lib/facts-store.mjs +183 -0
- package/lib/gate-state.mjs +465 -0
- package/lib/gate.mjs +886 -0
- package/lib/index.js +309 -0
- package/lib/inject.mjs +195 -0
- package/lib/interrupt.mjs +376 -0
- package/lib/ledger.mjs +948 -0
- package/lib/lifecycle.mjs +274 -0
- package/lib/messages.mjs +532 -0
- package/lib/notice.mjs +769 -0
- package/lib/plan-gate.mjs +385 -0
- package/lib/projection.mjs +978 -0
- package/lib/report.mjs +290 -0
- package/lib/route.mjs +346 -0
- package/lib/run-registry.mjs +402 -0
- package/lib/services.mjs +293 -0
- package/lib/stuck.mjs +567 -0
- package/lib/tier-config.mjs +212 -0
- package/lib/tier-policy.mjs +167 -0
- package/lib/tier-state.mjs +137 -0
- package/lib/todo-gate.mjs +1672 -0
- package/lib/tools.mjs +2912 -0
- package/locale/en.json +9 -0
- package/locale/zh.json +9 -0
- package/package.json +83 -4
- package/vendor/node_modules/@deepseek-ai/cosmokit/LICENSE +21 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/README.md +30 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/lib/index.js +418 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/package.json +32 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/array.ts +42 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/index.ts +12 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/misc.ts +78 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/string.ts +113 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/time.ts +92 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/types.ts +168 -0
- package/vendor/node_modules/@deepseek-ai/cosmokit/src/volatile.ts +86 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/LICENSE +21 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/README.md +393 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/lib/index.cjs +635 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/lib/index.mjs +635 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/package.json +39 -0
- package/vendor/node_modules/@deepseek-ai/schemastery/src/index.ts +962 -0
- package/vendor/node_modules/@standard-schema/spec/LICENSE +21 -0
- package/vendor/node_modules/@standard-schema/spec/README.md +198 -0
- package/vendor/node_modules/@standard-schema/spec/dist/index.cjs +18 -0
- package/vendor/node_modules/@standard-schema/spec/dist/index.js +0 -0
- package/vendor/node_modules/@standard-schema/spec/package.json +39 -0
package/lib/tools.mjs
ADDED
|
@@ -0,0 +1,2912 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 三个 ToolDefinition(check_subagent_selection / suggest_concurrency / send_interrupt)
|
|
3
|
+
* + buildReport + 确认卡(工具定义与其私有 helper)。
|
|
4
|
+
*
|
|
5
|
+
* 从 lib/index.js 原样搬出,函数体逐字未改。
|
|
6
|
+
*
|
|
7
|
+
* v10(2026-11-25,N3 决策工具链合并工位):
|
|
8
|
+
* ① SEC-B-01 撤销接线:answered && !concurrencyApproved → revokeConcurrencyConfirmation
|
|
9
|
+
* (同轮「最近答复」撤销既有授权,写序见 execute 内注释);
|
|
10
|
+
* ② 确认卡判断先行:detail 首行 = 四维结论一句话 + 推荐 + 理由(自有设计④);
|
|
11
|
+
* ③ 不派发主动推荐:single 四维都低时推荐项 description 写劝退理由(自有设计①);
|
|
12
|
+
* ④ 并发数三档选项(下限/中位/上限,推荐下限;偏好可覆盖推荐档)+ 偏好两层**读取**
|
|
13
|
+
* (~/.dsh/subroute.json 全局 + <workspace>/.dsh/subroute.json 工作区覆盖,
|
|
14
|
+
* 读序工作区 > 全局 > 无;**写入只落工作区层**,全局层保留为只读回退;
|
|
15
|
+
* 仍逐次问不免除;fs 读写 try/catch 静默降级)。
|
|
16
|
+
*
|
|
17
|
+
* 【变更 2026-11-27 档位配置】(/tier 编辑档位模型,lane T1 交付 tier-config.mjs):
|
|
18
|
+
* ① resolveModelTierOf 读本会话档位绑定(readTierConfig(sessionKeyOf(agent)))合并进
|
|
19
|
+
* 档位策略 —— /tier 深度 绑定的模型用于「强模型」档解析(显式绑定 > 白名单启发式);
|
|
20
|
+
* ② 第二问 detail 首句披露当前深度思考模型(绑定 → 「当前深度思考模型:provider/model」;
|
|
21
|
+
* 未绑定 → 「深度思考模型未设置(可先 /tier 深度 选择)」);选项顺序/标签零改动。
|
|
22
|
+
* 【变更 2026-11-27 badge 绑定并读】01 徽标「深度思考代理模型」二级菜单把选择写进
|
|
23
|
+
* 徽标宿主会话表条目(deepProvider/deepModel/deepEffort);档位解析与披露改为
|
|
24
|
+
* readTierBindings(badge UI 绑定)+ readTierConfig(/tier 命令)按序合并 ——
|
|
25
|
+
* tier-config 优先于 badge,读取失败/无会话键各自静默降级(硬约束第 4 条)。
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { scoreTask, analyzeTask, buildReasoningBrief, reasoningPlanOf, planVerdictOf, verdictForConcurrency } from './concurrency.mjs'
|
|
29
|
+
import { concurrencyModeLabel } from './messages.mjs'
|
|
30
|
+
import { buildReasoningConcurrencyReport } from './report.mjs'
|
|
31
|
+
import { teamEnabled, readUiSelection, sessionKeyOf, hasDiscoveryTool } from './services.mjs'
|
|
32
|
+
import { readAllowedModels, allowedRoutesFrom } from './projection.mjs'
|
|
33
|
+
import { resolveChildRoute, parentRouteOf, formatRoute, resolveEffectiveRoute } from './route.mjs'
|
|
34
|
+
// W4-27:提问通道原语(ASK_TIMEOUT_MS / askWithDeadline)搬到最底层 lib/facts-store.mjs ——
|
|
35
|
+
// command.mjs 也要用它,留在本模块会让「命令(入口层)→ 工具大模块」的分层倒置继续存在。
|
|
36
|
+
// 本地仍需绑定这两个名字(本文件内部直接调用),并在下方原样 re-export 保持旧导入面
|
|
37
|
+
// (test.mjs 仍从 './lib/tools.mjs' 取 askWithDeadline)。
|
|
38
|
+
import { ASK_TIMEOUT_MS, askWithDeadline } from './facts-store.mjs'
|
|
39
|
+
import { markConcurrencyConfirmed, markConcurrencyUnavailable, recordDecisionEpoch, revokeConcurrencyConfirmation, readLedgerSnapshotByKey, unfinishedAgentsOfSnapshot } from './gate.mjs'
|
|
40
|
+
// V-INT(批次 2 / B2-1):中断标志的**唯一**状态源在 gate.mjs(与确认纪元同表纪律)。
|
|
41
|
+
// tools.mjs 只负责「解析目标 + 写标志 + 发消息 + 出回执」,不持有第二份标志表。
|
|
42
|
+
import { setInterruptFlag } from './gate.mjs'
|
|
43
|
+
// 【变更 2026-11-28 启动恢复检查】自检报告的「未完成子代理」段:台账快照 + 未完成判定
|
|
44
|
+
// 都取自 gate.mjs(单一真相源),本模块只做「读取 + 渲染」,不持第二份台账状态。
|
|
45
|
+
// ═══ 档位路由显式化(第二波,2026-11-27)═══
|
|
46
|
+
// tier-policy.mjs(第一波交付):档位 → 白名单路由的**纯函数**单一真相源(resolveTierRoute);
|
|
47
|
+
// tier-state.mjs(另一 lane 交付):已确认档位的纯内存会话状态(recordConfirmedTier)——
|
|
48
|
+
// 用户在第二问里真实选定档位后按会话记录,供 inject.mjs 在委派前读取(第二优先级来源)。
|
|
49
|
+
import { resolveTierRoute, defaultTierPolicy } from './tier-policy.mjs'
|
|
50
|
+
import { recordConfirmedTier } from './tier-state.mjs'
|
|
51
|
+
// 【变更 2026-11-27 档位配置】/tier 编辑档位模型(lane T1 交付 tier-config.mjs):
|
|
52
|
+
// 本会话档位绑定的**唯一读取口**——用户经 /tier 写进配置的档位 → 路由绑定(deep/
|
|
53
|
+
// standard/cheap → {provider,model,effort?}),读取失败/未设置返回 undefined。
|
|
54
|
+
// resolveModelTierOf 在白名单策略之上合并它,使「/tier 深度 绑定的模型」
|
|
55
|
+
// 用于确认卡第二问的「强模型」档解析。
|
|
56
|
+
// 【变更 2026-11-27 badge 绑定并读】01 徽标新增的「深度思考代理模型」二级菜单把选择
|
|
57
|
+
// 写进徽标宿主会话表条目的 deepProvider/deepModel/deepEffort(badge-sync.readTierBindings
|
|
58
|
+
// 读取)——tools.mjs 的档位解析必须看到这层 UI 绑定,合并序见 resolveModelTierOf。
|
|
59
|
+
import { readTierConfig } from './tier-config.mjs'
|
|
60
|
+
import { readTierBindings } from './badge-sync.mjs'
|
|
61
|
+
|
|
62
|
+
// 【变更 2026-11-27 plan-first】并发必须先经官方 plan mode:计划意图 / 计划批准
|
|
63
|
+
// 的唯一状态源在 plan-gate.mjs(纯内存、零 I/O),本模块只做读取与引导文案。
|
|
64
|
+
import { notePlanIntent, readPlanApproval, consumePlanApproval } from './plan-gate.mjs'
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* ── 并发偏好两层持久化(N3 子项四;自有设计③「偏好接缝」,2026-11-25)──
|
|
68
|
+
*
|
|
69
|
+
* 内联说明:本实现原为独立 lib/preference.mjs,因 integration §14.1 的
|
|
70
|
+
* 「lib/ 源文件集合精确等于 21 个」结构护栏(四套不可改套件之一,断言冻结)
|
|
71
|
+
* 而内联进 tools.mjs —— 文件集护栏的本意是「新文件漏建 / 废弃文件残留」会被
|
|
72
|
+
* 源码扫描静默缩水,新增非必要文件即触发;偏好逻辑属于确认卡的私有支撑,
|
|
73
|
+
* 内联在语义上也成立(唯一消费者是本模块)。
|
|
74
|
+
*
|
|
75
|
+
* 两层文件(Node fs 同步读写,全部 try/catch 静默降级):
|
|
76
|
+
* · 全局:~/.dsh/subroute.json —— 用户跨项目的一般偏好
|
|
77
|
+
* · 工作区:<workspace>/.dsh/subroute.json —— 项目级覆盖(本插件按工作目录作用)
|
|
78
|
+
* 读序:工作区 > 全局 > 无。读到偏好时,确认卡的推荐路数取偏好值(卡片标注来源),
|
|
79
|
+
* **仍逐次问不免除** —— 历史设计的教训:会话级沿用会让默认档在长会话里被静默
|
|
80
|
+
* 固化、绕过用户(早前确认 ≠ 现在确认);偏好只省「重复点选」,不省「被问到」。
|
|
81
|
+
*
|
|
82
|
+
* 形状:{ concurrency: <正整数>, updatedAt: <ISO 时间串> };其余字段忽略。
|
|
83
|
+
* 写入时机:用户在确认卡上确认并发(concurrencyApproved === true)时记录该次路数。
|
|
84
|
+
* 降级纪律:读不到 / 解析失败 / 形状不符 / 无 agent 会话 → 返回 undefined;
|
|
85
|
+
* 写失败(磁盘只读 / 目录不可建 / JSON 序列化失败)→ 静默放弃。偏好只是推荐项
|
|
86
|
+
* 的取值来源,任何 I/O 失败都绝不影响确认卡 / 闸的本体行为。
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
export { ASK_TIMEOUT_MS, askWithDeadline }
|
|
90
|
+
|
|
91
|
+
import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'
|
|
92
|
+
import { join, dirname, resolve, relative, isAbsolute } from 'node:path'
|
|
93
|
+
import { homedir } from 'node:os'
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 全局层根目录:DSH_SUBROUTE_HOME 环境变量优先(测试接缝 + 部署可重定向),
|
|
97
|
+
* 未设置回落 os.homedir()。测试通过它把两层偏好整体重定向到临时目录,
|
|
98
|
+
* 绝不读写真实 ~/.dsh/subroute.json(否则测试自身会把偏好写进全局层,
|
|
99
|
+
* 污染同机其它套件的「无偏好」前置——integration/functional 冻结断言依赖无偏好)。
|
|
100
|
+
*/
|
|
101
|
+
function globalPreferencePath() {
|
|
102
|
+
const home = typeof process.env.DSH_SUBROUTE_HOME === 'string' && process.env.DSH_SUBROUTE_HOME !== ''
|
|
103
|
+
? process.env.DSH_SUBROUTE_HOME
|
|
104
|
+
: homedir()
|
|
105
|
+
return join(home, '.dsh', 'subroute.json')
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 工作区层:<workspace>/.dsh/subroute.json。
|
|
110
|
+
*
|
|
111
|
+
* workspace 的判定纪律(防御式,取不到即放弃工作区层):
|
|
112
|
+
* · 只认 agent **显式携带**的工作区路径:agent.cwd / workingDirectory /
|
|
113
|
+
* workspaceDirectory / session.cwd / session.directory / session.workspace。
|
|
114
|
+
* · **不回退 process.cwd()**:宿主进程 cwd 不一定是用户工作区(误写风险),
|
|
115
|
+
* 且测试进程 cwd = 仓库目录,裸 agent 的确认测试会把偏好写进仓库 ——
|
|
116
|
+
* 写侧必须比读侧更保守(读不到偏好只是推荐项回默认,写错位置是真实污染)。
|
|
117
|
+
* @param {object} agent
|
|
118
|
+
* @returns {string|undefined}
|
|
119
|
+
*/
|
|
120
|
+
function workspacePreferencePath(agent) {
|
|
121
|
+
const root = workspaceRootOf(agent)
|
|
122
|
+
return root === undefined ? undefined : join(root, '.dsh', 'subroute.json')
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 取 agent 显式携带的工作区根(不做任何护栏判定,纯取值)。 */
|
|
126
|
+
function workspaceRootOf(agent) {
|
|
127
|
+
const candidates = [
|
|
128
|
+
agent?.cwd,
|
|
129
|
+
agent?.workingDirectory,
|
|
130
|
+
agent?.workspaceDirectory,
|
|
131
|
+
agent?.session?.cwd,
|
|
132
|
+
agent?.session?.directory,
|
|
133
|
+
agent?.session?.workspace,
|
|
134
|
+
]
|
|
135
|
+
return candidates.find((value) => typeof value === 'string' && value !== '')
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* ── A-07(P2,批次内按 P1 处理):写侧「非项目根」护栏 ──
|
|
140
|
+
*
|
|
141
|
+
* 缺陷:`workspacePreferencePath` 直接拿 `agent.cwd` 拼 `/.dsh/subroute.json`。
|
|
142
|
+
* `agent.cwd` 就是工作区根,而**任何以主目录为工作区启动 DSH 的用户**(极常见
|
|
143
|
+
* 形态)每一次确认都会写 `~/.dsh/subroute.json` —— 从「边缘误操作」变成
|
|
144
|
+
* 「默认行为」。写出的值随后被 `readConcurrencyPreference` 当 `layer:'global'`
|
|
145
|
+
* 命中,影响后续所有项目的推荐档。这违背本模块与 README 的明文契约
|
|
146
|
+
* (「全局层 = 只读回退层,绝不写」)。
|
|
147
|
+
*
|
|
148
|
+
* 仲裁修法(§2.2 判断点 2):**加写侧护栏,不把偏好从授权链里摘出去** ——
|
|
149
|
+
* 偏好参与闸授权是正确产品语义(卡片首选项 / 问题文本 / 闸授权三者同源),
|
|
150
|
+
* 该修的是「写到哪里」。
|
|
151
|
+
*
|
|
152
|
+
* 判据(保守,宁可少写不可错写):
|
|
153
|
+
* · 主目录本身(`homedir()` 或 `DSH_SUBROUTE_HOME` 重定向后的等价根)→ 拒写
|
|
154
|
+
* · 文件系统根 / 盘符根(`/`、`C:\\`、`\\\\server\\share` 之类)→ 拒写
|
|
155
|
+
* · 主目录的**祖先**(如 `C:\\Users`)→ 拒写(写进去等于跨项目污染)
|
|
156
|
+
* · 主目录的**子孙**(`~/repo/foo`)→ **允许**(那是真正的项目目录)
|
|
157
|
+
*
|
|
158
|
+
* 为什么拒「主目录祖先」而不是只拒「等于」:`C:\\Users` 作工作区同样是
|
|
159
|
+
* 「没有项目标记的目录」,写进去会污染该机器上所有用户的所有项目。
|
|
160
|
+
* 判据用 path.relative 而不是字符串前缀,避免 `/home/ab` 误匹配 `/home/abc`。
|
|
161
|
+
*
|
|
162
|
+
* @param {string} root agent 显式携带的工作区根
|
|
163
|
+
* @returns {{allowed: boolean, reason?: string}} 判定结果(reason 供诊断/测试)
|
|
164
|
+
*/
|
|
165
|
+
function preferenceWriteGuard(root) {
|
|
166
|
+
let resolved
|
|
167
|
+
try {
|
|
168
|
+
resolved = resolve(String(root))
|
|
169
|
+
} catch {
|
|
170
|
+
// 路径非法(含 NUL 等)→ 交给后面的 fs 层自然失败,这里不额外拦
|
|
171
|
+
return { allowed: true }
|
|
172
|
+
}
|
|
173
|
+
// 文件系统根 / 盘符根:resolve('C:\\') 之后 relative 到自身为空串
|
|
174
|
+
if (resolve(resolved, '..') === resolved) return { allowed: false, reason: 'filesystem-root' }
|
|
175
|
+
|
|
176
|
+
let home
|
|
177
|
+
try {
|
|
178
|
+
home = resolve(typeof process.env.DSH_SUBROUTE_HOME === 'string' && process.env.DSH_SUBROUTE_HOME !== ''
|
|
179
|
+
? process.env.DSH_SUBROUTE_HOME
|
|
180
|
+
: homedir())
|
|
181
|
+
} catch {
|
|
182
|
+
home = undefined
|
|
183
|
+
}
|
|
184
|
+
if (home === undefined || home === '') return { allowed: true }
|
|
185
|
+
|
|
186
|
+
const toHome = relative(resolved, home)
|
|
187
|
+
// 相等:resolved 就是主目录
|
|
188
|
+
if (toHome === '') return { allowed: false, reason: 'home-dir' }
|
|
189
|
+
// resolved 是主目录的祖先:home 在 resolved 之内
|
|
190
|
+
const homeInsideResolved = toHome !== '' && !toHome.startsWith('..') && !isAbsolute(toHome)
|
|
191
|
+
if (homeInsideResolved) return { allowed: false, reason: 'home-ancestor' }
|
|
192
|
+
return { allowed: true }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* 读单层偏好文件:不存在 / 读取失败 / 解析失败 / 形状不符 → undefined(静默)。
|
|
197
|
+
* @param {string} file
|
|
198
|
+
* @param {'workspace'|'global'} layer 命中时回传的层名(确认卡来源标注用)
|
|
199
|
+
* @returns {{concurrency: number, updatedAt: string, source: string, layer: string}|undefined}
|
|
200
|
+
*/
|
|
201
|
+
function readPreferenceFile(file, layer) {
|
|
202
|
+
try {
|
|
203
|
+
const raw = readFileSync(file, 'utf8')
|
|
204
|
+
const parsed = JSON.parse(raw)
|
|
205
|
+
// ── A-30(P3):读侧同样严格 —— 只认 JSON number 且为正整数 ──
|
|
206
|
+
//
|
|
207
|
+
// 旧实现 `Number(parsed?.concurrency)` 接受 true→1、"3"→3、2.9→2(R1-P3-4)。
|
|
208
|
+
// 文件头契约写的是「形状不符 → 返回 undefined」,宽松转换与之矛盾。
|
|
209
|
+
// 注意 JSON.parse 出来的 number 与 JS number 同型,故 typeof 判定足够;
|
|
210
|
+
// 非整数(2.9)也拒 —— 路数没有小数语义,静默 floor 会让「写 2.9 读 2」
|
|
211
|
+
// 看起来像被接受了。
|
|
212
|
+
const value = parsed?.concurrency
|
|
213
|
+
if (typeof value !== 'number' || !Number.isInteger(value) || value < 1) return undefined
|
|
214
|
+
return {
|
|
215
|
+
concurrency: value,
|
|
216
|
+
updatedAt: String(parsed?.updatedAt ?? ''),
|
|
217
|
+
source: file,
|
|
218
|
+
layer,
|
|
219
|
+
}
|
|
220
|
+
} catch {
|
|
221
|
+
return undefined
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* 读并发偏好:工作区层优先,未命中回退全局层(读序:工作区 > 全局 > 无)。
|
|
227
|
+
* 读侧工作区定位允许 process.cwd() 兜底(宿主通常从工作区根启动;读失败无害)。
|
|
228
|
+
* @param {object} agent 调用方 agent(用于定位工作区层;可为 undefined)
|
|
229
|
+
* @returns {{concurrency: number, updatedAt: string, source: string, layer: string}|undefined}
|
|
230
|
+
*/
|
|
231
|
+
export function readConcurrencyPreference(agent) {
|
|
232
|
+
const explicit = workspacePreferencePath(agent)
|
|
233
|
+
const workspaceFile = explicit ?? (typeof process.cwd() === 'string' && process.cwd() !== ''
|
|
234
|
+
? join(process.cwd(), '.dsh', 'subroute.json')
|
|
235
|
+
: undefined)
|
|
236
|
+
if (workspaceFile !== undefined) {
|
|
237
|
+
const local = readPreferenceFile(workspaceFile, 'workspace')
|
|
238
|
+
if (local !== undefined) return local
|
|
239
|
+
}
|
|
240
|
+
return readPreferenceFile(globalPreferencePath(), 'global')
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 写并发偏好:**只写工作区层**(且仅当 agent 显式携带工作区路径)。
|
|
245
|
+
*
|
|
246
|
+
* ── 为什么不写全局层(刻意保守,N3 设计取舍 2026-11-25)──
|
|
247
|
+
* 全局层 = 用户主目录下的真实文件(~/.dsh/subroute.json)。确认卡上的一次点选
|
|
248
|
+
* 就写用户主目录,是带副作用的全局状态变更:插件测试套件(本仓库六个套件都走
|
|
249
|
+
* 真实确认链路)会把测试用的路数写进开发者机器的全局层,跨套件、跨项目地
|
|
250
|
+
* 改变后续确认卡的推荐项(实测污染:一轮试跑让四个冻结断言转红)。
|
|
251
|
+
* 因此:写侧只落**项目级**工作区层(作用域可见、可 git-ignore、可删);
|
|
252
|
+
* 全局层保留为**只读回退**(用户/运维手工放置,或未来宿主暴露可信工作区/
|
|
253
|
+
* 主目录信号后再接线写侧)。读序工作区 > 全局 > 不变;逐次问不变。
|
|
254
|
+
*
|
|
255
|
+
* 失败静默降级:目录不可建 / 磁盘只读 / JSON 序列化失败 → 返回 false,
|
|
256
|
+
* 绝不影响确认主流程。无显式工作区(裸 agent / 测试替身)→ 不写(返回 false)。
|
|
257
|
+
* @param {object} agent 调用方 agent(定位工作区层)
|
|
258
|
+
* @param {number} concurrency 用户确认的路数(非正数忽略)
|
|
259
|
+
* @returns {boolean} 写入成功
|
|
260
|
+
*/
|
|
261
|
+
export function saveConcurrencyPreference(agent, concurrency) {
|
|
262
|
+
// ── A-30(P3):严格数值判定,不再走 Number() 宽松转换 ──
|
|
263
|
+
//
|
|
264
|
+
// 旧实现 `Number(concurrency)` 静默接受 true→1、"3"→3、2.9→2、[3]→3,
|
|
265
|
+
// 与本模块明文契约「形状不符 → 放弃」不一致(R1-P3-4 实测四种脏值全部被采纳)。
|
|
266
|
+
// 修法:只认 number 类型本身 + 整数 + 正数。字符串 / 布尔 / 数组一律拒绝 ——
|
|
267
|
+
// 调用方(execute)传的是 confirmation.concurrency(内部算出的 number),
|
|
268
|
+
// 严格化对生产路径零影响,只是把「脏值静默接受」变成「脏值静默拒绝」。
|
|
269
|
+
if (typeof concurrency !== 'number' || !Number.isInteger(concurrency) || concurrency < 1) return false
|
|
270
|
+
const value = concurrency
|
|
271
|
+
const file = workspacePreferencePath(agent)
|
|
272
|
+
if (file === undefined) return false
|
|
273
|
+
// ── A-07:写侧护栏(主目录 / 盘符根 / 主目录祖先 → 拒写)──
|
|
274
|
+
const guard = preferenceWriteGuard(workspaceRootOf(agent))
|
|
275
|
+
if (guard.allowed !== true) return false
|
|
276
|
+
try {
|
|
277
|
+
const payload = JSON.stringify({ concurrency: Math.floor(value), updatedAt: new Date().toISOString() })
|
|
278
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
279
|
+
writeFileSync(file, payload, 'utf8')
|
|
280
|
+
return true
|
|
281
|
+
} catch {
|
|
282
|
+
// 静默降级:偏好写入绝不影响确认主流程
|
|
283
|
+
return false
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* ── A-02(P1):任务描述硬上限(码点)──
|
|
289
|
+
*
|
|
290
|
+
* 为什么必须有:`suggest_concurrency` 的 description 自己鼓励「描述越具体判断越准」,
|
|
291
|
+
* 粘贴目录树 / minified JS / base64 是**正常用法**;而 FILE_TOKEN_RE 的嵌套量词
|
|
292
|
+
* 在无分隔长 token 上是 O(n²)(修前 20KB 单正则 162–490ms,叠加 scoreTask 内部
|
|
293
|
+
* 三处重复扫描 ×3.5 即 R3 实测 86.6s / R2 实测 46.9s 的全局冻结)。插件与宿主
|
|
294
|
+
* 同进程,同步回溯期间整个 DSH 主进程停摆 —— 这不是「慢」,是全局失效。
|
|
295
|
+
*
|
|
296
|
+
* 20000 码点的选取:本仓最长的冻结语料 177 码点,真实任务描述极少过千;
|
|
297
|
+
* 20KB 是修前曲线开始进入百毫秒级的拐点。限长取「足够宽松到永不误伤正常用法」
|
|
298
|
+
* 而不是「刚好够用」,避免把性能防护变成功能阉割。
|
|
299
|
+
*/
|
|
300
|
+
const MAX_TASK_CHARS = 20000
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* V-INT:send_interrupt 的 reason 长度上限(码点)。
|
|
304
|
+
*
|
|
305
|
+
* 理由会**原样**进两个面:子代理的 tool result 提示文本 + 本会话 ledger.jsonl 的
|
|
306
|
+
* 一行中断记账。两个面都要求有界——提示文本会进模型上下文(超长理由等于替调用方
|
|
307
|
+
* 往子代理上下文里灌内容),记账行会无界增长(JSONL 是追加文件,一行一条,永不回收)。
|
|
308
|
+
* 1000 码点足够写清「为什么停」(真实用法通常一句话),又远小于任何上下文预算。
|
|
309
|
+
* 与 MAX_TASK_CHARS 同一套入口截断纪律:截断而非抛错,并在回执里如实标注已截断。
|
|
310
|
+
*/
|
|
311
|
+
const MAX_INTERRUPT_REASON_CHARS = 1000
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* A-02:截断标注。超长输入被入口截到 MAX_TASK_CHARS 后,输出尾部补一行提示,
|
|
315
|
+
* 让模型明确知道「这份建议只基于前 N 码点」,而不是误以为覆盖了全文。
|
|
316
|
+
*
|
|
317
|
+
* 只在真的截断时追加 —— 正常调用(99.99%)的输出逐字不变,
|
|
318
|
+
* 冻结 golden / 逐字结构断言(如 integration §16.10 的「覆盖版 6 行」)零影响。
|
|
319
|
+
* @param {string} text 报告正文
|
|
320
|
+
* @param {boolean} truncated 入口是否发生过截断
|
|
321
|
+
* @returns {string}
|
|
322
|
+
*/
|
|
323
|
+
function withTruncationNotice(text, truncated) {
|
|
324
|
+
if (truncated !== true) return text
|
|
325
|
+
return text + '\n\n(输入超长:任务描述已按前 ' + MAX_TASK_CHARS + ' 个字符评分,'
|
|
326
|
+
+ '建议仅基于这段开头;如需全文判断请拆成多条任务。)'
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** F14(P3):按码点安全截断,绝不劈开 UTF-16 代理对。 */
|
|
330
|
+
function truncateSafe(value, max) {
|
|
331
|
+
const chars = Array.from(String(value ?? ''))
|
|
332
|
+
return chars.length > max ? chars.slice(0, max).join('') + '…' : chars.join('')
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** F8(P2):把任务原文压成单行,避免多行任务污染「任务:」行。 */
|
|
336
|
+
function oneLine(value) {
|
|
337
|
+
return String(value ?? '').replace(/\s*[\r\n]+\s*/g, ' ').trim()
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/* ── 【变更 2026-11-28 plan-first 携带并发判断】/plan 种子文本 ──────────
|
|
341
|
+
*
|
|
342
|
+
* 背景:官方 /plan 命令(dsh-plan-mode lib/index.js:196-236)在 message 非空时
|
|
343
|
+
* 除了 set(agent, true) 还会 agent.steer(任务消息)——旧的空参 '/plan' 只开模式
|
|
344
|
+
* 不注入任务,模型进了计划模式却不知道「要计划什么 / 建议拆几路」。本构造器把
|
|
345
|
+
* 「原任务 + 并发建议 + 依据 + 结尾句」压成单行种子,随 '/plan ' 一起进命令入口。
|
|
346
|
+
*
|
|
347
|
+
* 纯函数(无 I/O、无状态):tools.mjs 计划闸与 test-plan-gate 单测共用同一实现,
|
|
348
|
+
* 避免测试复制格式导致漂移。导出仅为可测性(与 truncateSafe 同款纪律)——
|
|
349
|
+
* 「空 task 占位」分支经 execute 不可达(入口对空 task 先抛错),只能单测直达。
|
|
350
|
+
*/
|
|
351
|
+
|
|
352
|
+
/** 种子文本码点上限:整段(不含 '/plan ' 前缀)最多 2000 码点,超长截断不加省略号。 */
|
|
353
|
+
export const PLAN_SEED_MAX_CHARS = 2000
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* 构造 '/plan ' 种子行(纯函数)。四段:
|
|
357
|
+
* 原任务:{oneLine(task),空时占位「(用户未提供任务描述)」}
|
|
358
|
+
* 并发建议:{recConcurrency >= 2 ? 'N 路' : '串行(1 路)'}
|
|
359
|
+
* [推导:{brief0.derivation}] / [判定依据:{brief0.basisBrief}](有则拼上,没有省略)
|
|
360
|
+
* 请在计划中明确拆分(每路任务/写入范围)与验收标准。(结尾固定句)
|
|
361
|
+
* 整段 oneLine 化;超过 PLAN_SEED_MAX_CHARS 码点按码点截断(不劈代理对、不加省略号——
|
|
362
|
+
* 计划正文另有模型自查,截断只发生在极端超长任务,保开头部分即可)。
|
|
363
|
+
* @param {string} task 任务原文(调用点已经过入口 trim;空值在此占位兜底)
|
|
364
|
+
* @param {{recConcurrency: number, brief0?: {derivation?: string, basisBrief?: string}|null}} display
|
|
365
|
+
* resolvePreferredDisplay 的结果(推荐路数 + 任务简报,单一真相源)
|
|
366
|
+
* @returns {string} 以 '/plan ' 开头的完整命令行
|
|
367
|
+
*/
|
|
368
|
+
export function planSeedLineOf(task, display) {
|
|
369
|
+
const oneLined = oneLine(task)
|
|
370
|
+
const seedTask = oneLined === '' ? '(用户未提供任务描述)' : oneLined
|
|
371
|
+
const rec = display?.recConcurrency
|
|
372
|
+
const seedLanes = rec >= 2 ? '并发建议:' + rec + ' 路' : '并发建议:串行(1 路)'
|
|
373
|
+
const basisParts = []
|
|
374
|
+
if (typeof display?.brief0?.derivation === 'string' && display.brief0.derivation !== '') basisParts.push('推导:' + display.brief0.derivation)
|
|
375
|
+
if (typeof display?.brief0?.basisBrief === 'string' && display.brief0.basisBrief !== '') basisParts.push('判定依据:' + display.brief0.basisBrief)
|
|
376
|
+
const seedText = oneLine(
|
|
377
|
+
'原任务:' + seedTask
|
|
378
|
+
+ '。' + seedLanes
|
|
379
|
+
+ (basisParts.length > 0 ? '。' + basisParts.join(';') : '')
|
|
380
|
+
+ '。请在计划中明确拆分(每路任务/写入范围)与验收标准。',
|
|
381
|
+
)
|
|
382
|
+
const chars = Array.from(seedText)
|
|
383
|
+
const capped = chars.length > PLAN_SEED_MAX_CHARS ? chars.slice(0, PLAN_SEED_MAX_CHARS).join('') : seedText
|
|
384
|
+
return '/plan ' + capped
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/* ── 【变更 2026-11-28 死亡语义】T2:running 行的「疑似已死」渲染(tools 层) ──────────
|
|
388
|
+
*
|
|
389
|
+
* 背景(58 号报告):台账里 status==='running' 的行可能是**死亡残留**——宿主崩溃 /
|
|
390
|
+
* 事件丢失后没有 end 行,该 run 永远「运行中」。渲染层按静默时长追加「疑似已死」注:
|
|
391
|
+
* 超 45 分钟(复用 client.js STALE_RUNNING_MS 的同一条 45min 口径)→ 追加
|
|
392
|
+
* 「(超 X 分钟无收场,疑似已死/lost)」;不到 45 分钟不追加(真在跑保护,旧行零漂移)。
|
|
393
|
+
*
|
|
394
|
+
* 边界(测试钉死):恰好 45min(含)有注、44min59s(不含)无注——与 client 侧
|
|
395
|
+
* 「≥ STALE_RUNNING_MS 判陈旧」同一边界方向。
|
|
396
|
+
*
|
|
397
|
+
* now 注入(函数参数 now 可注入,默认 Date.now()):buildReport 生产路径传默认值;
|
|
398
|
+
* 测试 / 未来调用方传固定时刻即可复现边界(绝不动 Date.now 全局)。
|
|
399
|
+
*/
|
|
400
|
+
|
|
401
|
+
/** running 行陈旧阈值:45 分钟(与 client.js STALE_RUNNING_MS 数值一致,唯一真相各守其层)。 */
|
|
402
|
+
export const STALE_RUNNING_MS = 45 * 60 * 1000
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* 单行「疑似已死」注(纯函数):running 且 createdAt 有限、now - createdAt ≥ 阈值
|
|
406
|
+
* → 注文案;否则空串(未知 createdAt / 非 running / 阈值内一律不注)。
|
|
407
|
+
* @param {{status: string, createdAt: number|null}} row unfinishedAgentsOfSnapshot 的行
|
|
408
|
+
* @param {number} [now] 当前时刻(测试可注入;默认 Date.now())
|
|
409
|
+
* @returns {string}
|
|
410
|
+
*/
|
|
411
|
+
export function staleRunningNoteOf(row, now = Date.now()) {
|
|
412
|
+
try {
|
|
413
|
+
if (row === null || typeof row !== 'object') return ''
|
|
414
|
+
if (row.status !== 'running') return ''
|
|
415
|
+
const createdAt = typeof row.createdAt === 'number' && Number.isFinite(row.createdAt) ? row.createdAt : null
|
|
416
|
+
if (createdAt === null) return ''
|
|
417
|
+
const elapsed = now - createdAt
|
|
418
|
+
if (elapsed < STALE_RUNNING_MS) return ''
|
|
419
|
+
return `(超 ${Math.floor(elapsed / 60000)} 分钟无收场,疑似已死/lost)`
|
|
420
|
+
} catch { return '' }
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** 报告时钟(默认 Date.now;仅测试经 __setReportNow 注入,生产恒为真实时钟)。 */
|
|
424
|
+
let reportNow = () => Date.now()
|
|
425
|
+
|
|
426
|
+
/** 测试接缝:注入报告时钟(buildReport 的 stale 计算用)。非函数入参忽略。 */
|
|
427
|
+
export function __setReportNow(fn) {
|
|
428
|
+
if (typeof fn === 'function') reportNow = fn
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** 测试接缝:恢复真实时钟。 */
|
|
432
|
+
export function __restoreReportNow() {
|
|
433
|
+
reportNow = () => Date.now()
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** buildReport 内部取当前时刻(经注入点,便于测试钉 44/46min 边界)。 */
|
|
437
|
+
function reportNowOf() {
|
|
438
|
+
try { return reportNow() } catch { return Date.now() }
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** 组装自检报告文本。 */
|
|
442
|
+
async function buildReport(ctx, agent, args) {
|
|
443
|
+
const lines = []
|
|
444
|
+
const team = teamEnabled(ctx, agent)
|
|
445
|
+
const resolved = resolveChildRoute(agent, args)
|
|
446
|
+
// 与自动注入读同一份数据:策略 / 父路由回退都在 allowedRoutesFrom 里
|
|
447
|
+
const policy = await readAllowedModels(ctx, agent)
|
|
448
|
+
// 展示用的「白名单」保持原语义:undefined = 本会话未记录策略
|
|
449
|
+
const allowed = policy
|
|
450
|
+
const allowedRoutes = allowedRoutesFrom(agent, policy)
|
|
451
|
+
const discovery = hasDiscoveryTool(ctx, agent)
|
|
452
|
+
lines.push('子代理开工自检')
|
|
453
|
+
lines.push('────────────────────────')
|
|
454
|
+
lines.push(`Team 模式: ${team === undefined ? '(不可用)' : team ? '已开启' : '未开启'}`)
|
|
455
|
+
lines.push(`当前主代理路由: ${formatRoute(parentRouteOf(agent))}`)
|
|
456
|
+
|
|
457
|
+
// 用户在界面上的选择(插件未装时为 undefined)。
|
|
458
|
+
// X13:读**当前会话**的界面选择——check_subagent_selection 的 agent
|
|
459
|
+
// 就是当前会话的调用方,sessionKeyOf 取其会话 id。
|
|
460
|
+
const uiSelection = readUiSelection(ctx, sessionKeyOf(agent))
|
|
461
|
+
if (uiSelection === undefined) {
|
|
462
|
+
lines.push('界面选择: (未安装 dsh-subagent-route-badge,或服务不可用)')
|
|
463
|
+
} else if (uiSelection === null) {
|
|
464
|
+
lines.push('界面选择: 继承父代理路由')
|
|
465
|
+
} else {
|
|
466
|
+
lines.push(`界面选择: ${uiSelection.provider}/${uiSelection.model}`)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// ── 预测本次委派的最终路由 ──
|
|
470
|
+
//
|
|
471
|
+
// 自检通常在**委派前**调用,此时自动注入还没发生。若只按 args 推断,
|
|
472
|
+
// 会得出「继承主代理」的错误结论,与随后实际委派的结果不一致
|
|
473
|
+
// (用户看到的就是「自检说 A、开工提示说 B」)。
|
|
474
|
+
//
|
|
475
|
+
// 因此这里不自己推断,而是调用与注入 / 提示同一个函数。
|
|
476
|
+
const decision = resolveEffectiveRoute({
|
|
477
|
+
agent,
|
|
478
|
+
args,
|
|
479
|
+
uiSelection,
|
|
480
|
+
allowedRoutes,
|
|
481
|
+
autoInjected: false,
|
|
482
|
+
})
|
|
483
|
+
|
|
484
|
+
lines.push(`本次将使用: ${formatRoute(decision.route)}(${decision.reason})`)
|
|
485
|
+
if (resolved.effortCleared) lines.push(' effort 已清除,使用所选模型自身默认值')
|
|
486
|
+
// 诚实标注(t3-问题2):本插件从 tools.definitions() 读不到工具实例的 config.agentOptions,
|
|
487
|
+
// 若实际被调用的委派工具是带 agentOptions 的 - insert: 实例(如 subagent_glm),其真实路由
|
|
488
|
+
// 以实例配置为准。说「不知道」远好于给出确定但错误的「继承主代理」。
|
|
489
|
+
lines.push(' 注:若实际调用的委派工具实例配置了 agentOptions(- insert: 实例),实际路由以实例配置为准,本插件读不到')
|
|
490
|
+
|
|
491
|
+
if (allowed === undefined) {
|
|
492
|
+
lines.push('白名单路由: (本会话未记录策略)')
|
|
493
|
+
lines.push(' 显式选择仅限父代理当前路由')
|
|
494
|
+
} else {
|
|
495
|
+
lines.push(`白名单路由: ${allowed.length} 条`)
|
|
496
|
+
for (const route of allowed) lines.push(` ${route.provider}/${route.model}`)
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if (discovery !== undefined) {
|
|
500
|
+
lines.push(`list_subagent_models: ${discovery ? '已注册 ✓' : '未注册 ✗(preset 未启用 modelDiscovery)'}`)
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// 白名单是「可跨安装复用的便携子集」,条数少是刻意设计而非缺陷,
|
|
504
|
+
// 因此不对「白名单 < providers 总数」报警。只报真正会失败或易误解的情况。
|
|
505
|
+
const problems = []
|
|
506
|
+
// 显式路由校验必须用**有效白名单** allowedRoutes,而不是原始策略 allowed(P1-1)。
|
|
507
|
+
// 无策略(policy === undefined)时官方只允许父代理当前路由;allowedRoutes 已按此
|
|
508
|
+
// 回退为 [父路由],若仍用 allowed 就会在「无策略」时直接跳过校验,
|
|
509
|
+
// 把必被官方拒绝的显式非父路由误报成「✅ 可以委派」。
|
|
510
|
+
if (resolved.explicit && allowedRoutes !== undefined) {
|
|
511
|
+
const hit = allowedRoutes.some((route) =>
|
|
512
|
+
route.provider === resolved.route.provider && route.model === resolved.route.model)
|
|
513
|
+
if (!hit) problems.push(`本次显式指定的 ${formatRoute(resolved.route)} 不在白名单内,委派会被拒绝`)
|
|
514
|
+
}
|
|
515
|
+
// 继承路由不在白名单内是合法的(不传即继承),但模型若「多此一举」把同一个
|
|
516
|
+
// 路由显式重述一遍就会被拒 —— 这是最容易踩的坑,值得点出来。
|
|
517
|
+
if (allowed !== undefined) {
|
|
518
|
+
const parent = parentRouteOf(agent)
|
|
519
|
+
if (parent.provider !== undefined && parent.model !== undefined) {
|
|
520
|
+
const parentInList = allowed.some((route) =>
|
|
521
|
+
route.provider === parent.provider && route.model === parent.model)
|
|
522
|
+
if (!parentInList && !resolved.explicit) {
|
|
523
|
+
problems.push(`当前主代理路由 ${formatRoute(parent)} 不在白名单内:继承可用,但显式重述同一路由会被拒绝`)
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
// 官方硬性约束(dsh-tool-subagent/src/model-selection.ts):
|
|
528
|
+
// · 字段存在即算「请求了路由」(hasDelegationModelRequest :76-80),与 route.mjs
|
|
529
|
+
// hasExplicitRouteArgs 的「字段存在」判定同源 —— 故这里用 !== undefined 判断字段是否给出(不看值)。
|
|
530
|
+
// · provider / model 必须成对给出,否则抛 "must be supplied together"(:114-118);
|
|
531
|
+
// · 给出空串会被 assertNonEmpty 抛 "must be non-empty"(:83-87、:111-113)。
|
|
532
|
+
// · reasoning_effort 必须与 provider+model 同时给出(:124-132、README:231),否则抛
|
|
533
|
+
// "child LLM reasoning_effort requires both provider and model"(P1-2)。
|
|
534
|
+
// t3-问题9(V2 残留#5,M2a/M3 双向指认收口):{provider:'',model:''} / 仅 provider 此前无 problem
|
|
535
|
+
// → 误报「✅ 可以委派」,与官方拒绝语义不一致。按官方「成对 + 非空」补齐 problem。
|
|
536
|
+
const providerProvided = args?.provider !== undefined
|
|
537
|
+
const modelProvided = args?.model !== undefined
|
|
538
|
+
const hasProviderArg = typeof args?.provider === 'string' && args.provider.length > 0
|
|
539
|
+
const hasModelArg = typeof args?.model === 'string' && args.model.length > 0
|
|
540
|
+
if (providerProvided !== modelProvided) {
|
|
541
|
+
problems.push('provider / model 必须成对给出(只给其中一个,官方会拒绝:must be supplied together)')
|
|
542
|
+
}
|
|
543
|
+
if ((providerProvided && !hasProviderArg) || (modelProvided && !hasModelArg)) {
|
|
544
|
+
problems.push('provider / model 给出时不能是空字符串(官方会拒绝:must be non-empty)')
|
|
545
|
+
}
|
|
546
|
+
if (args?.reasoning_effort !== undefined && !(hasProviderArg && hasModelArg)) {
|
|
547
|
+
problems.push('reasoning_effort 必须与 provider+model 同时给出,否则官方会拒绝')
|
|
548
|
+
}
|
|
549
|
+
if (discovery === false) problems.push('list_subagent_models 未注册,模型无法查询可选路由')
|
|
550
|
+
|
|
551
|
+
lines.push('────────────────────────')
|
|
552
|
+
if (problems.length === 0) {
|
|
553
|
+
lines.push('结论: ✅ 可以委派')
|
|
554
|
+
} else {
|
|
555
|
+
lines.push('结论: ⚠ 有需要留意的项')
|
|
556
|
+
for (const problem of problems) lines.push(` • ${problem}`)
|
|
557
|
+
}
|
|
558
|
+
// ── 【变更 2026-11-28 启动恢复检查】未完成子代理段(可选,只在有未完成时输出)──
|
|
559
|
+
//
|
|
560
|
+
// 背景(用户发现 b):新会话 / 宿主重启后,主模型开工前应检查上次派发的子代理
|
|
561
|
+
// 是否有未完成的(运行中 / 报错 / 被中止),并向用户确认是否继续或重新派发。
|
|
562
|
+
// 数据源:发起方会话(当前 agent 的会话)的磁盘台账快照;判定唯一真相在
|
|
563
|
+
// gate.unfinishedAgentsOfSnapshot(running / error / ended+aborted|error)。
|
|
564
|
+
// 无未完成 → 本段整体缺席,报告逐字等于旧输出(既有 golden / 逐字断言零漂移)。
|
|
565
|
+
// 读取失败(无会话键 / 台账禁用)→ 同样缺席:自检不因增强信息失败而失败。
|
|
566
|
+
try {
|
|
567
|
+
const snapshot = readLedgerSnapshotByKey(sessionKeyOf(agent))
|
|
568
|
+
const unfinished = unfinishedAgentsOfSnapshot(snapshot)
|
|
569
|
+
if (unfinished.length > 0) {
|
|
570
|
+
lines.push('')
|
|
571
|
+
lines.push('未完成子代理:')
|
|
572
|
+
for (const row of unfinished) {
|
|
573
|
+
// 状态标签:running=运行中、error=报错、ended+aborted+中断记账=手动中止、
|
|
574
|
+
// ended+aborted(无中断记账)=意外中止、其余 ended=已结束(stopReason 原样示出)。
|
|
575
|
+
// 【变更 2026-11-28 死亡语义】ended+max-tokens=达上限未完成、ended+refusal=已拒接
|
|
576
|
+
// (G1 判定集扩展的渲染面;未知判词仍落「已结束」兜底标签)。
|
|
577
|
+
const statusLabel = row.status === 'running' ? '运行中'
|
|
578
|
+
: row.status === 'error' ? '报错'
|
|
579
|
+
: row.status === 'ended' && row.stopReason === 'aborted' && row.interruptAt != null ? '手动中止'
|
|
580
|
+
: row.status === 'ended' && row.stopReason === 'aborted' ? '意外中止'
|
|
581
|
+
: row.status === 'ended' && row.stopReason === 'max-tokens' ? '达上限未完成'
|
|
582
|
+
: row.status === 'ended' && row.stopReason === 'refusal' ? '已拒接'
|
|
583
|
+
: '已结束'
|
|
584
|
+
const stopNote = row.stopReason === null ? '' : `(stopReason=${row.stopReason})`
|
|
585
|
+
lines.push(` - ${row.name}:${statusLabel}${stopNote}${staleRunningNoteOf(row, reportNowOf())}`)
|
|
586
|
+
}
|
|
587
|
+
lines.push('⚠ 请先向用户确认是否继续/重新派发这些任务,再开始新的派发。')
|
|
588
|
+
}
|
|
589
|
+
} catch { /* 台账读取失败 → 本段缺席(增强信息绝不影响自检主流程) */ }
|
|
590
|
+
return lines.join('\n')
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/** 手写 ToolDefinition(零依赖,等价 defineTool 的最小形态)。 */
|
|
594
|
+
function makeCheckTool(ctx) {
|
|
595
|
+
return {
|
|
596
|
+
name: 'check_subagent_selection',
|
|
597
|
+
description:
|
|
598
|
+
'在启动子代理或使用 Team 之前调用,返回本会话的子代理模型选择自检:'
|
|
599
|
+
+ 'Team 是否开启、当前主代理路由、本次委派将使用的 provider/model/effort、'
|
|
600
|
+
+ '白名单路由数量与内容,以及是否存在配置漂移。'
|
|
601
|
+
+ '若本会话台账有未完成的子代理(运行中 / 报错 / 被中止),报告会附「未完成子代理」段。'
|
|
602
|
+
+ '把结果原样告知用户并确认子代理选择是否正确。'
|
|
603
|
+
+ '只读查询:会读取 settings 服务(describe),可能触及其缓存/事件;不改变插件或会话状态。',
|
|
604
|
+
parameters: {
|
|
605
|
+
type: 'object',
|
|
606
|
+
additionalProperties: false,
|
|
607
|
+
properties: {
|
|
608
|
+
provider: {
|
|
609
|
+
type: 'string',
|
|
610
|
+
description: '可选。你打算显式指定的子代理 provider;与 model 一起给出时校验是否在白名单内。',
|
|
611
|
+
},
|
|
612
|
+
model: {
|
|
613
|
+
type: 'string',
|
|
614
|
+
description: '可选。你打算显式指定的子代理 model;需与 provider 一起给出。',
|
|
615
|
+
},
|
|
616
|
+
reasoning_effort: {
|
|
617
|
+
type: 'string',
|
|
618
|
+
description: '可选。你打算指定的推理强度。',
|
|
619
|
+
},
|
|
620
|
+
},
|
|
621
|
+
},
|
|
622
|
+
output: {
|
|
623
|
+
schema: { type: 'string' },
|
|
624
|
+
render: (_args, value) => [{ type: 'text', text: String(value) }],
|
|
625
|
+
},
|
|
626
|
+
// 并发安全:本工具只读(不注册/注销投影单元、不改 settings),
|
|
627
|
+
// 因此显式声明可并发。早先在 readAllowedModels 里有懒注册兜底,
|
|
628
|
+
// 工具执行期会 register 共享投影单元 —— 那是 parent-owned state 变更,
|
|
629
|
+
// 与 isConcurrencySafe 的声明矛盾(P1-2);该懒注册已删除,读路径不写状态。
|
|
630
|
+
isConcurrencySafe: () => true,
|
|
631
|
+
async execute(args, exec) {
|
|
632
|
+
const agent = exec.agent
|
|
633
|
+
if (agent === undefined) throw new Error('check_subagent_selection 需要调用方 Agent')
|
|
634
|
+
// 不接受任何「自报界面选择」的参数:
|
|
635
|
+
// 本工具的全部价值是让用户看到真相,若审计对象(模型)能单方面让它说假话,
|
|
636
|
+
// 就属信任链错位。界面选择只能从真实 settings namespace 读。
|
|
637
|
+
return buildReport(ctx, agent, args)
|
|
638
|
+
},
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* 并发建议工具 —— 按任务描述判断该不该并发、并发几路、用哪个模型档。
|
|
644
|
+
*
|
|
645
|
+
* 设计来源:dispatch SKILL.md 的四维评分体系。
|
|
646
|
+
* 评分逻辑在 lib/concurrency.mjs(纯函数,可独立测试)。
|
|
647
|
+
*
|
|
648
|
+
* 职责边界:**只给建议 + 请用户确认,不执行派发**。
|
|
649
|
+
* 派发由模型自己调 subagent(官方已支持同 step 多次调用即并发)。
|
|
650
|
+
*
|
|
651
|
+
* ⚠️ 2026-11-19 重要变更:**默认向用户提问确认**。
|
|
652
|
+
*
|
|
653
|
+
* 早先这个工具只返回一段文本给模型看,用户很可能只看到折叠的工具块,
|
|
654
|
+
* 而且没有确认环节 —— 等于「建议只存在于模型脑子里」。
|
|
655
|
+
*
|
|
656
|
+
* 现在默认走官方 `ctx.userQuestions` seam(与 `ask_user_question` 同一套)
|
|
657
|
+
* 向用户展示一张选项卡:
|
|
658
|
+
* 「按建议执行:并发 4 路(推荐)」/「改为串行」/「不派发,主会话直接做」
|
|
659
|
+
* 用户点选后,答案回到模型上下文,模型按用户选择执行。
|
|
660
|
+
*
|
|
661
|
+
* 降级:若当前环境没有 userQuestions(无 UI answerer、或本工具被 subagent
|
|
662
|
+
* 调用 —— 官方明确拒绝 DELEGATED_CALLER),则不阻塞,改为在报告里
|
|
663
|
+
* 明确写「未能获取用户确认,请转述给用户后再继续」,绝不静默继续。
|
|
664
|
+
*
|
|
665
|
+
* 与 Team 模式的关系:
|
|
666
|
+
* Team 开启时,并发派发可用共享任务板协调;
|
|
667
|
+
* 未开启时并发仍可行(各自独立),但没有任务板依赖管理。
|
|
668
|
+
*/
|
|
669
|
+
/**
|
|
670
|
+
* NEW-1 第二条 fail-open(P0 补口)/ NEW-4:解析「计划批准记录」的授权路数与披露标记。
|
|
671
|
+
*
|
|
672
|
+
* 背景:tools.mjs 的批准分支旧实现是
|
|
673
|
+
* lanes = Number.isFinite(approval.lanes) && approval.lanes > 0 ? approval.lanes : display.recConcurrency
|
|
674
|
+
* —— 批准记录 lanes 非法 / 缺失时回退到**推荐路数**,与 NEW-1「推荐值永不进入授权路径」
|
|
675
|
+
* 的 P0 纪律正面冲突(推荐 8 路就会授权 8 路)。现在保守兜底 1 路。
|
|
676
|
+
*
|
|
677
|
+
* lanesSource 由 plan-gate 单点判定('explicit' | 'serial' | 'unparsed'),本函数只读:
|
|
678
|
+
* · 'unparsed' → 文案必须披露「计划正文未解析到并发路数,按 1 路保守执行」(NEW-4);
|
|
679
|
+
* · 缺失且 lanes 也非法(旧版本记录 / 外部写入)→ 同样披露(无法证明解析成功)。
|
|
680
|
+
* @param {{lanes?: number, lanesSource?: string}|undefined} approval readPlanApproval 的结果
|
|
681
|
+
* @returns {{lanes: number, valid: boolean, lanesSource: string, unparsed: boolean}}
|
|
682
|
+
*/
|
|
683
|
+
export function approvalLanesOf(approval) {
|
|
684
|
+
const raw = Number(approval?.lanes)
|
|
685
|
+
const valid = Number.isFinite(raw) && raw > 0
|
|
686
|
+
const lanesSource = typeof approval?.lanesSource === 'string' ? approval.lanesSource : ''
|
|
687
|
+
const unparsed = lanesSource === 'unparsed' || (lanesSource === '' && valid === false)
|
|
688
|
+
return { lanes: valid ? Math.floor(raw) : 1, valid, lanesSource, unparsed }
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/** suggest_concurrency 的工具描述(W4-24:从工具定义里抽出,逐字未改)。 */
|
|
692
|
+
const CONCURRENCY_TOOL_DESCRIPTION =
|
|
693
|
+
'在决定是否把任务拆成多个子代理并发执行前调用。'
|
|
694
|
+
+ '传入任务描述,会做四维评分(可并行性 / 依赖关系 / 思考难度 / 规模),'
|
|
695
|
+
+ '并**直接向用户展示选项卡片请其确认**:按建议执行 / 改为串行 / 不派发。'
|
|
696
|
+
+ '返回用户的选择结果。'
|
|
697
|
+
// 42-模型档第二问:同一张卡的第二问,回执会带上模型档 / 派发参数。
|
|
698
|
+
+ '确认卡同时问「模型档」(强模型 / 快速模型 / 指定模型);'
|
|
699
|
+
+ '用户手填模型名时按本会话白名单校验,命中就在回执里给出 provider/model,'
|
|
700
|
+
+ '未命中会回退强模型档并在回执写明(不会静默替换)。'
|
|
701
|
+
+ '本工具不派发任何子代理、不改变插件状态;默认会等待用户回答,'
|
|
702
|
+
+ '因此默认不与其他工具并发执行(ask:false 的纯咨询调用才可并发)。'
|
|
703
|
+
+ '注意:这是启发式建议,任务描述越具体(做什么、几个、有无先后)判断越准;'
|
|
704
|
+
+ '置信度低时如实告知不确定性。'
|
|
705
|
+
+ '若报告里出现「未能获取用户确认」,必须先把建议转述给用户并等待确认,再执行派发。'
|
|
706
|
+
|
|
707
|
+
/** suggest_concurrency 的参数 schema(W4-24:从工具定义里抽出,逐字未改)。 */
|
|
708
|
+
const CONCURRENCY_TOOL_PARAMETERS = {
|
|
709
|
+
type: 'object',
|
|
710
|
+
additionalProperties: false,
|
|
711
|
+
properties: {
|
|
712
|
+
task: {
|
|
713
|
+
type: 'string',
|
|
714
|
+
// ── A-02(P1)第 ① 步:schema 层限长,立即封堵 ──
|
|
715
|
+
//
|
|
716
|
+
// 宿主会按 JSON Schema 校验入参,超长直接拒绝,连 execute 都进不来。
|
|
717
|
+
// 20000 码点 ≈ 20KB:远超任何真实任务描述(本仓最长的冻结语料 177 码点),
|
|
718
|
+
// 而 20KB 恰好是「正则二次方」从毫秒级跨到百毫秒级的量级(实测修前
|
|
719
|
+
// 20KB 单正则 162–490ms)。限长放在 schema 与入口两处是刻意的双保险:
|
|
720
|
+
// schema 拦住合规调用方,入口 slice 兜住绕过 schema 的直调(测试 / 第三方)。
|
|
721
|
+
maxLength: MAX_TASK_CHARS,
|
|
722
|
+
description:
|
|
723
|
+
'任务描述原文。建议包含:要做什么、涉及多少个对象(如「10 个文件」)、'
|
|
724
|
+
+ '是否有先后依赖(如「先分析再重构」)。描述越具体,判断越准。',
|
|
725
|
+
},
|
|
726
|
+
ask: {
|
|
727
|
+
type: 'boolean',
|
|
728
|
+
description:
|
|
729
|
+
'是否向用户提问确认,默认 true。设为 false 只返回建议、不打扰用户'
|
|
730
|
+
+ '(仅在用户已明确说「不用问我,你决定」时使用)。',
|
|
731
|
+
},
|
|
732
|
+
},
|
|
733
|
+
required: ['task'],
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/**
|
|
737
|
+
* 计划模式优先路径(plan-first;【变更 2026-11-27】/【变更 2026-11-28】)。
|
|
738
|
+
*
|
|
739
|
+
* W4-24:从 execute 里整段抽出(90 行),行为逐字不变。判定顺序:
|
|
740
|
+
* ⓪ readPlanApproval 先行:有计划批准(installPlanGate 在 exit_plan_mode 提交
|
|
741
|
+
* 成功后写入)→ 消费批准(读后即焚),按计划路数返回放行回执——不再弹卡。
|
|
742
|
+
* 为什么提前:PTC profile 把 plan-mode 放在 isolate: planMode:true 组里,
|
|
743
|
+
* 插件根 ctx.get('planMode') 恒为 undefined —— 批准是 plan-gate 自有内存
|
|
744
|
+
* 事实,与 planMode 服务在不在无关,放行路径绝不能被服务缺席误杀。
|
|
745
|
+
* ①′ commands 优先:宿主 commands 服务可用 → 走官方命令入口并**携带种子文本**
|
|
746
|
+
* (原任务 + 并发建议 + 依据 + 结尾句,≤2000 码点)。commands 在但执行失败
|
|
747
|
+
* → 继续尝试下方 planMode 服务,再不行回退旧卡。
|
|
748
|
+
* ①″ planMode legacy 兜底(无 commands 或 commands 执行失败时保留原行为):
|
|
749
|
+
* · 已在计划模式(active/pending)→ 不弹卡,引导先提交计划;
|
|
750
|
+
* · 未在计划模式 → 记录计划意图(含推荐路数)并置 planMode.set(agent, true)。
|
|
751
|
+
* ② commands 与 planMode 都缺席 / 都失败 → 返回 { handled: false }(回退旧确认卡)。
|
|
752
|
+
* 全部读取 / 执行 try/catch 静默降级(可选服务 ctx.get?.() 同款纪律)。
|
|
753
|
+
*
|
|
754
|
+
* @param {object} ctx cordis context
|
|
755
|
+
* @param {object} agent 调用方 agent
|
|
756
|
+
* @param {string} task 任务原文(种子文本用)
|
|
757
|
+
* @param {object} display resolvePreferredDisplay 的结果(推荐路数 / 种子)
|
|
758
|
+
* @param {object} exec 工具执行上下文(取 abort signal)
|
|
759
|
+
* @returns {Promise<{handled: boolean, text?: string}>} handled=true 时 text 为最终回执
|
|
760
|
+
*/
|
|
761
|
+
async function enterPlanFirstMode(ctx, agent, task, display, exec) {
|
|
762
|
+
const approvalEarly = readPlanApproval(agent)
|
|
763
|
+
if (approvalEarly !== undefined) {
|
|
764
|
+
// ── NEW-1 第二条 fail-open(P0 补口)+ NEW-4:统一走 approvalLanesOf(唯一判定点)。
|
|
765
|
+
const approvalLanes = approvalLanesOf(approvalEarly)
|
|
766
|
+
const lanes = approvalLanes.lanes
|
|
767
|
+
const parseNote = approvalLanes.unparsed ? '(计划正文未解析到并发路数,按 1 路保守执行)' : ''
|
|
768
|
+
if (approvalLanes.valid === false) {
|
|
769
|
+
try { markConcurrencyConfirmed(agent, 1) } catch { /* 收紧失败也不影响回执(闸另有 fail-open 纪律) */ }
|
|
770
|
+
}
|
|
771
|
+
// 【变更 2026-11-27 badge 绑定并读】深度模型披露与档位解析同序合并:badge UI 绑定 → /tier 绑定。
|
|
772
|
+
const execKey = sessionKeyOf(agent)
|
|
773
|
+
const execDeep = { ...((await readTierBindings(ctx, execKey)) ?? {}), ...(readTierConfig(execKey) ?? {}) }.deep
|
|
774
|
+
const modelNote = execDeep ? ';深度思考模型:' + execDeep.provider + '/' + execDeep.model : ''
|
|
775
|
+
consumePlanApproval(agent)
|
|
776
|
+
return {
|
|
777
|
+
handled: true,
|
|
778
|
+
text: '计划已批准:按计划' + (lanes >= 2 ? '并发 ' + lanes + ' 路执行' : '串行执行') + parseNote + modelNote + '。',
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
// ── ①′ PTC 优先:commands 可用时优先(携带种子文本)──
|
|
782
|
+
// 判定顺序变更:官方 /plan 命令(dsh-plan-mode lib/index.js:196-236)在 message
|
|
783
|
+
// 非空时除了 set(agent, true) 还会 agent.steer(任务消息)——空参 '/plan' 只开模式
|
|
784
|
+
// 不注入任务,模型进计划模式后看不到「要计划什么 / 建议拆几路」。因此 commands
|
|
785
|
+
// 服务可用时**优先**走命令入口并携带种子文本(原任务 + 并发建议 + 依据 + 结尾句);
|
|
786
|
+
// planMode 服务仅作 legacy 兜底(没有 commands 的宿主,见下方 ①″)。
|
|
787
|
+
// ── W3-F-08(P2):可选服务读取一律 try/catch 静默降级(硬约束第 4 条)──
|
|
788
|
+
// 旧实现裸 ctx.get?.('commands'):服务 getter 抛错时整个 suggest_concurrency 失败,
|
|
789
|
+
// 而不是退化成「无 commands → planMode legacy 兜底 → 旧卡」。
|
|
790
|
+
let commands
|
|
791
|
+
try { commands = ctx.get?.('commands') } catch { commands = undefined }
|
|
792
|
+
if (commands !== undefined && commands !== null && typeof commands.execute === 'function') {
|
|
793
|
+
// 种子文本四段(原任务 / 并发建议 / 依据 / 结尾句)由 planSeedLineOf 统一构造
|
|
794
|
+
// (纯函数导出,test-plan-gate 单测同源;占位与截断规则见其 doc 注释)。
|
|
795
|
+
const seedLine = planSeedLineOf(task, display)
|
|
796
|
+
let planEntered = false
|
|
797
|
+
try {
|
|
798
|
+
const signal = exec?.signal ?? new AbortController().signal
|
|
799
|
+
const result = await commands.execute(agent, seedLine, [], signal)
|
|
800
|
+
planEntered = result !== undefined
|
|
801
|
+
} catch { planEntered = false }
|
|
802
|
+
if (planEntered === true) {
|
|
803
|
+
try { notePlanIntent(agent, display.recConcurrency) } catch {}
|
|
804
|
+
return {
|
|
805
|
+
handled: true,
|
|
806
|
+
text: '已进入计划模式:请先与用户确定计划(目标 / 拆分 / 每路写入范围 / 验收),用 exit_plan_mode 提交审核;批准后再执行并发(预计 ' + display.recConcurrency + ' 路)。',
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
// commands 在但执行失败 → 继续往下尝试 planMode 服务(legacy 兜底),再不行回退旧卡。
|
|
810
|
+
}
|
|
811
|
+
// ── ①″ legacy 兜底:无 commands(或 commands 执行失败)且宿主提供 planMode 服务 ──
|
|
812
|
+
// 直接置 planMode.set(agent, true):此路径没有命令入口可携带种子文本,
|
|
813
|
+
// 计划意图(含推荐路数)已由 notePlanIntent 记录,供 exit_plan_mode 提交后回退。
|
|
814
|
+
{
|
|
815
|
+
// W3-F-08:同款 try/catch 降级 —— planMode getter 抛错 = 该服务不可用。
|
|
816
|
+
let planMode
|
|
817
|
+
try { planMode = ctx.get?.('planMode') } catch { planMode = undefined }
|
|
818
|
+
if (planMode !== undefined && planMode !== null && typeof planMode.get === 'function' && typeof planMode.set === 'function') {
|
|
819
|
+
const planState = planMode.get(agent)
|
|
820
|
+
if (planState?.active === true || planState?.pending === true) {
|
|
821
|
+
return { handled: true, text: '当前处于计划模式:请先在对话中完成计划,并用 exit_plan_mode 提交审核;批准后再执行并发。' }
|
|
822
|
+
}
|
|
823
|
+
try { notePlanIntent(agent, display.recConcurrency) } catch {}
|
|
824
|
+
try { planMode.set(agent, true) } catch {}
|
|
825
|
+
return {
|
|
826
|
+
handled: true,
|
|
827
|
+
text: '已进入计划模式:请先与用户确定计划(目标 / 拆分 / 每路写入范围 / 验收),用 exit_plan_mode 提交审核;批准后再执行并发(预计 ' + display.recConcurrency + ' 路)。',
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
return { handled: false }
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* 把确认结果落到闸 / 轮纪元 / 并发偏好(W4-24:从 execute 整段抽出,行为逐字不变)。
|
|
836
|
+
*
|
|
837
|
+
* M13 + v10(SEC-B-01)**同 tick 三步写序(顺序刻意固定)**:
|
|
838
|
+
* ① recordDecisionEpoch:任何 answered === true 的真实决策都推进轮次纪元
|
|
839
|
+
* (面板据此清空旧轮);与闸解锁解耦,串行 / 不派发 / 改选同样写。
|
|
840
|
+
* ② v10 撤销:answered 且该答复**不构成并发同意**时,同轮可能存在早前的并发授权
|
|
841
|
+
* (如先确认并发 2 路、随后改选「只派 1 路」)—— 撤销 confirmedSessions 条目,
|
|
842
|
+
* 让用户「降并发 / 改串行」的意图在闸层生效:第 2 路起重新被拦。只撤授权:
|
|
843
|
+
* 纪元(①已写)与在跑计数都不动(见 gate.mjs revoke 注释)。
|
|
844
|
+
* ③ markConcurrencyConfirmed:仅 confirmed && concurrencyApproved 时解锁(条件一字不动);
|
|
845
|
+
* 顺带同刻写纪元(seq 沿用,①②与③共用同一张表),并把用户确认的路数偏好落盘。
|
|
846
|
+
* @param {object} agent 调用方 agent
|
|
847
|
+
* @param {object} confirmation askUserToConfirmConcurrency 的结构化结果
|
|
848
|
+
*/
|
|
849
|
+
function applyConfirmationToGate(agent, confirmation) {
|
|
850
|
+
if (confirmation.answered === true) recordDecisionEpoch(agent)
|
|
851
|
+
if (confirmation.answered === true && confirmation.concurrencyApproved !== true) {
|
|
852
|
+
revokeConcurrencyConfirmation(agent)
|
|
853
|
+
}
|
|
854
|
+
if (confirmation.confirmed === true && confirmation.concurrencyApproved === true) {
|
|
855
|
+
markConcurrencyConfirmed(agent, confirmation.concurrency)
|
|
856
|
+
saveConcurrencyPreference(agent, confirmation.concurrency)
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* suggest_concurrency 的 execute 实现(W4-24:从工具定义里抽出,行为逐字不变)。
|
|
862
|
+
*
|
|
863
|
+
* 编排:入口截断 → 四维评分 / 报告 → 计划模式优先 → 确认卡 → 闸/偏好落账 → 回执。
|
|
864
|
+
* @param {object} ctx cordis context
|
|
865
|
+
* @param {object} tierPolicy 具名档位策略表(registerTools(config.tierPolicy) 透传)
|
|
866
|
+
* @param {object} args 工具入参 { task, ask }
|
|
867
|
+
* @param {object} exec 工具执行上下文
|
|
868
|
+
* @returns {Promise<string>} 面向模型的报告文本
|
|
869
|
+
*/
|
|
870
|
+
async function runSuggestConcurrency(ctx, tierPolicy, args, exec) {
|
|
871
|
+
const agent = exec.agent
|
|
872
|
+
// ── A-02(P1)第 ② 步:入口截断(兜底)──
|
|
873
|
+
//
|
|
874
|
+
// schema 的 maxLength 拦得住合规调用方,拦不住**绕过 schema 的直调**
|
|
875
|
+
// (测试直接 execute、第三方复用工具定义、宿主未来放宽校验)。
|
|
876
|
+
// 这里先按码点截到 MAX_TASK_CHARS 再 trim:截断而非抛错,是为了让
|
|
877
|
+
// 「超长输入」退化成「按前 20KB 给建议」而不是整次调用失败 ——
|
|
878
|
+
// 用户粘贴了整份日志时,拿一个基于开头部分的建议远好过拿一个报错。
|
|
879
|
+
// 截断不静默:报告尾部的「输入超长已截断」提示由 buildReasoningReport 之外的
|
|
880
|
+
// 调用点补(见下方 truncated 标记),确保模型知道自己给的不是全文。
|
|
881
|
+
const rawTask = String(args?.task ?? '')
|
|
882
|
+
const truncated = Array.from(rawTask).length > MAX_TASK_CHARS
|
|
883
|
+
const task = (truncated ? Array.from(rawTask).slice(0, MAX_TASK_CHARS).join('') : rawTask).trim()
|
|
884
|
+
if (task === '') throw new Error('suggest_concurrency 需要非空的 task 描述')
|
|
885
|
+
|
|
886
|
+
const { dimensions, verdict } = scoreTask(task)
|
|
887
|
+
const team = teamEnabled(ctx, agent)
|
|
888
|
+
const policy = await readAllowedModels(ctx, agent)
|
|
889
|
+
const report = buildReasoningConcurrencyReport(task, dimensions, verdict, team, policy)
|
|
890
|
+
// ── F2(P1-3):确认卡与报告正文统一按 plan.mode 渲染 ──
|
|
891
|
+
// 报告正文(buildReasoningReport)按 plan.mode,确认卡(askUserToConfirmConcurrency)
|
|
892
|
+
// 旧实现直接吃 verdict.mode;二者分叉时出现「卡说不派发、正文建议 2 路」。
|
|
893
|
+
// 这里取「与 plan 完全同 mode」的 effectiveVerdict 供**确认卡/覆盖版/闸同意语义**使用:
|
|
894
|
+
// plan 与 verdict 同 mode 时沿用 verdict(保持逐字旧卡),分叉时改用 planVerdictOf。
|
|
895
|
+
// 审计修法二选一,本实现取 (b):卡与正文统一按 plan.mode 渲染(最小改动面,
|
|
896
|
+
// 不触碰 scoreTask 冻结语料与既有 verdict 消费者)。
|
|
897
|
+
const plan = reasoningPlanOf(task, { dimensions, verdict })
|
|
898
|
+
// ── W3-F-01(P1):比较键必须同时含 mode **与 concurrency** ──
|
|
899
|
+
// 旧实现只比 mode:用户显式「最多 3 路」被评分器读成 8 路时(同为 parallel),
|
|
900
|
+
// plan.concurrency=3 被丢弃 → 卡片/闸/报告全按 8 路走(实测卡片写
|
|
901
|
+
// 「评定:建议 8 路同时做(10 件事,最多 3 路)」)。任一不同即改用 planVerdictOf。
|
|
902
|
+
const samePlan = plan.mode === verdict.mode && plan.concurrency === verdict.concurrency
|
|
903
|
+
const effectiveVerdict = samePlan
|
|
904
|
+
? verdict
|
|
905
|
+
: planVerdictOf(task, { dimensions, verdict })
|
|
906
|
+
|
|
907
|
+
// ask 默认 true:默认就要用户拍板
|
|
908
|
+
//
|
|
909
|
+
// round-7b:ask:false **不再**自动解锁并发闸(第七轮审计 R3-01 发现)。
|
|
910
|
+
//
|
|
911
|
+
// 初版让 ask:false 直接 markConcurrencyConfirmed,且拦截文案还教模型
|
|
912
|
+
// "传 ask:false 就能继续"——等于闸自己写出了绕过方法:模型只要每次都传
|
|
913
|
+
// ask:false,就能在用户零参与的情况下无限并发派发,闸形同虚设。
|
|
914
|
+
//
|
|
915
|
+
// 现在 ask:false 的语义严格限定为「不弹确认卡片、只返回建议文本」,
|
|
916
|
+
// **不解锁**。要真正解除限制,用户必须通过卡片做出选择(拿到答复)。
|
|
917
|
+
// 用户口头说"你决定"时,正确做法是模型照常弹卡片并在卡片里体现用户已授权,
|
|
918
|
+
// 而不是自己给自己发许可。
|
|
919
|
+
if (args?.ask === false) return withTruncationNotice(report, truncated)
|
|
920
|
+
|
|
921
|
+
// ── 子项四(自有设计③):并发偏好两层持久化 ──
|
|
922
|
+
// 读序:工作区 .dsh/subroute.json > 全局 ~/.dsh/subroute.json > 无。
|
|
923
|
+
// 读到偏好时确认卡的推荐路数取偏好值(卡片正文标注来源),**仍逐次问不免除**
|
|
924
|
+
// (历史设计教训:会话级沿用 = 长会话里默认档被静默固化、绕过用户)。
|
|
925
|
+
// 读写全部 Node fs try/catch 静默降级 —— 偏好只是推荐项的取值来源,
|
|
926
|
+
// 任何 I/O 失败都不能影响确认卡 / 闸的本体行为。
|
|
927
|
+
const preference = readConcurrencyPreference(agent)
|
|
928
|
+
// R-01:偏好 → 显示用推荐路数(卡片 / 报告 / 回执 / 闸单一真相源)。传给卡片复用,
|
|
929
|
+
// 避免下面重建报告时再算一遍 brief(同一份计算,逐字同源)。
|
|
930
|
+
const display = resolvePreferredDisplay(effectiveVerdict, dimensions, task, preference)
|
|
931
|
+
|
|
932
|
+
// ── 并发确认卡之上的官方计划闸(plan-first;判定顺序见 enterPlanFirstMode)──
|
|
933
|
+
const planFirst = await enterPlanFirstMode(ctx, agent, task, display, exec)
|
|
934
|
+
if (planFirst.handled === true) return withTruncationNotice(planFirst.text, truncated)
|
|
935
|
+
|
|
936
|
+
// 【档位路由显式化 2026-11-27】把档位策略传进确认卡(第 8 参 tierPolicy)。
|
|
937
|
+
const confirmation = await askUserToConfirmConcurrency(ctx, agent, effectiveVerdict, dimensions, task, preference, display, tierPolicy)
|
|
938
|
+
// round-7:只有**真正拿到用户答复**才解锁并发闸。
|
|
939
|
+
// 用显式 confirmed 字段判定,不再依赖 text 前缀——
|
|
940
|
+
// 前缀是文案,文案一改就会静默失效(这正是本轮要消除的那类脆弱耦合)。
|
|
941
|
+
//
|
|
942
|
+
// R1/P1(第二轮三层审计 L2-C1):**答复 ≠ 同意并发**。早先只要拿到答复就写
|
|
943
|
+
// confirmed: true(哪怕用户选的是「不派发」「改为串行」或写了句自定义输入),
|
|
944
|
+
// 于是闸把「用户拒绝并发」读成「用户授权并发」,本会话永久放行后续并发派发。
|
|
945
|
+
// 现在解锁要求两个结构化字段同时成立:concurrencyApproved 由「用户选择 vs
|
|
946
|
+
// 建议 mode」的**选项语义表**得出(见 receiptOfConcurrencyAnswer),
|
|
947
|
+
// confirmed 保留为同义兼容字段 —— 两者都读,任一为 false 都不解锁。
|
|
948
|
+
// v10(SEC-B-01 / 仲裁 ARB-P3-05):**非并发答复还要撤销同轮既有授权**。
|
|
949
|
+
// v8(t3 问题4 授权粒度):把本选项授权的路数上限 N 传给闸。
|
|
950
|
+
// 写序与理由见 applyConfirmationToGate 的注释(M13 职责分离)。
|
|
951
|
+
applyConfirmationToGate(agent, confirmation)
|
|
952
|
+
if (confirmation.override !== true) {
|
|
953
|
+
// ── R-01:偏好生效且用户按建议确认并发时,完整报告正文的并发数也要与卡片/闸同源 ──
|
|
954
|
+
// 旧实现 report 在偏好生效前按 verdict.concurrency(默认 4 路)生成,模型读到
|
|
955
|
+
// 「建议 4 路 / 这 4 路必须在同一条消息里发起」会派 4 路,第 4 路被上限 3 拦(矛盾)。
|
|
956
|
+
// 这里用 display.recConcurrency 重建整个报告(推理段 headline / 附录「建议:并发 N 路」/
|
|
957
|
+
// 执行要点「这 N 路必须…」全部同源)。无偏好 / 非并行 / 未获并发授权时沿用原 report
|
|
958
|
+
// (逐字不变,冻结用例零影响——它们都无偏好)。concurrencyRange.max 同步为推荐值,
|
|
959
|
+
// 使报告「可下调至 min 路(推荐 N 路)」与结论一致,不再残留旧的「推荐 4 路」。
|
|
960
|
+
const applyPreferred = display.preferredUsable === true
|
|
961
|
+
&& effectiveVerdict.mode === 'parallel'
|
|
962
|
+
&& confirmation.concurrencyApproved === true
|
|
963
|
+
// ── W3-F-02(P1):整份重建必须用**同一个最终 verdict** ──
|
|
964
|
+
// 旧实现 hand-roll 只覆盖 concurrency / concurrencyRange 两个字段:batches /
|
|
965
|
+
// efficiencyGain / facts 全是旧 verdict 的,报告出现「建议 3 路 + 共需 2 批
|
|
966
|
+
// (每批 3 路)」(10 件按 3 路实为 4 批)与「效率:高…约快 5 倍」。
|
|
967
|
+
// display.finalVerdict 由 resolvePreferredDisplay 单点重建(verdictForConcurrency),
|
|
968
|
+
// 卡片 / 报告 / 闸授权(confirmation.concurrency)读同一份数字。
|
|
969
|
+
const body = applyPreferred && display.finalVerdict !== undefined
|
|
970
|
+
? buildReasoningConcurrencyReport(task, dimensions, display.finalVerdict, team, policy)
|
|
971
|
+
: report
|
|
972
|
+
return withTruncationNotice(`${body}\n\n${confirmation.text}`, truncated)
|
|
973
|
+
}
|
|
974
|
+
// 用户明确改选(非推荐项):原报告里的并行执行指令必须整段丢弃,
|
|
975
|
+
// 否则「建议并发 N 路…这 N 路必须在同一条消息里发起」会和用户选择的
|
|
976
|
+
// 「改为串行 / 不派发」同框矛盾(P1-3)。覆盖版只保留任务、确认与原建议摘要。
|
|
977
|
+
return withTruncationNotice(buildOverrideReport(task, effectiveVerdict, confirmation), truncated)
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
/**
|
|
981
|
+
* suggest_concurrency 工具定义(W4-24:273 行 → 工具定义 + runSuggestConcurrency 编排)。
|
|
982
|
+
* 只保留「定义 / 注册面」,全部执行逻辑在 runSuggestConcurrency。
|
|
983
|
+
* 【档位路由显式化 2026-11-27】tierPolicy:registerTools(config.tierPolicy) 透传,
|
|
984
|
+
* 经 askUserToConfirmConcurrency → resolveModelTierOf 进入具名档位解析(缺省纯启发式)。
|
|
985
|
+
*/
|
|
986
|
+
function makeConcurrencyTool(ctx, tierPolicy = defaultTierPolicy()) {
|
|
987
|
+
return {
|
|
988
|
+
name: 'suggest_concurrency',
|
|
989
|
+
description: CONCURRENCY_TOOL_DESCRIPTION,
|
|
990
|
+
parameters: CONCURRENCY_TOOL_PARAMETERS,
|
|
991
|
+
output: {
|
|
992
|
+
schema: { type: 'string' },
|
|
993
|
+
render: (_args, value) => [{ type: 'text', text: String(value) }],
|
|
994
|
+
},
|
|
995
|
+
// 会阻塞等待用户回答,不能与其它工具并发(避免多个确认卡互相干扰)。
|
|
996
|
+
// 只有显式 ask:false 的纯咨询调用才是只读可并发的。
|
|
997
|
+
isConcurrencySafe: (args) => args?.ask === false,
|
|
998
|
+
async execute(args, exec) {
|
|
999
|
+
return runSuggestConcurrency(ctx, tierPolicy, args, exec)
|
|
1000
|
+
},
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
|
|
1005
|
+
/**
|
|
1006
|
+
* verdict → 人类可读的执行方式(本模块内部调用;导入自 messages.mjs,见文件顶部)。
|
|
1007
|
+
*
|
|
1008
|
+
* W3-F-18:这里曾用 re-export 兼容「潜在外部引用」—— 全仓 grep 无任何模块从
|
|
1009
|
+
* tools.mjs 导入该符号(定义与消费都在 messages.mjs / tools.mjs 内部),属死导出,
|
|
1010
|
+
* 已删除。保留本注释说明迁移历史(要引用请直接从 messages.mjs 导入)。
|
|
1011
|
+
*/
|
|
1012
|
+
|
|
1013
|
+
/**
|
|
1014
|
+
* 用户改选后的覆盖版报告(P1-3)。
|
|
1015
|
+
*
|
|
1016
|
+
* 原报告按「建议」生成,含「执行要点(官方要求):这 N 路必须在同一条消息里
|
|
1017
|
+
* 一起发起」等并行执行指令。用户明确改选「改为串行 / 不派发」后继续返回
|
|
1018
|
+
* 原报告就是同框矛盾——模型可能一边看到「按用户选择执行」一边看到「并发 N 路」。
|
|
1019
|
+
* 因此覆盖版只保留任务、用户确认与原建议摘要,不含任何并行执行指令。
|
|
1020
|
+
*
|
|
1021
|
+
* @param {string} task 原始任务描述
|
|
1022
|
+
* @param {object} verdict 原建议(仅用于生成「原建议」摘要)
|
|
1023
|
+
* @param {{text: string}} confirmation askUserToConfirmConcurrency 的结构化结果
|
|
1024
|
+
* @returns {string}
|
|
1025
|
+
*/
|
|
1026
|
+
function buildOverrideReport(task, verdict, confirmation) {
|
|
1027
|
+
const lines = []
|
|
1028
|
+
lines.push('并发建议(已按用户选择覆盖)')
|
|
1029
|
+
lines.push('────────────────────────')
|
|
1030
|
+
lines.push(`任务:${truncateSafe(oneLine(task), 60)}`)
|
|
1031
|
+
lines.push('')
|
|
1032
|
+
lines.push(confirmation.text)
|
|
1033
|
+
lines.push(`原建议:${concurrencyModeLabel(verdict)}(已覆盖)`)
|
|
1034
|
+
return lines.join('\n')
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* 并发数来源的短语版(卡片「评定:」段用)。
|
|
1039
|
+
*
|
|
1040
|
+
* 【人话化变更 2026-11-26】来源文案已由 concurrency.mjs 的 derivationOfBrief
|
|
1041
|
+
* 直接输出人话(如「8 件事,最多 4 路」),这里只做**超预算改写**。
|
|
1042
|
+
*
|
|
1043
|
+
* 【截断病修复 2026-11-26】旧实现 value.length > 20 → slice(0,19) + '…':
|
|
1044
|
+
* 卡面「评定:建议 4 路同时做(8 件事,最多 4…)」这种半句就是这么来的。现按用户口径
|
|
1045
|
+
* 「超预算时丢括注只留主句」:
|
|
1046
|
+
* ① 装得下(≤ DERIVATION_MAX 20 字)→ 原样;
|
|
1047
|
+
* ② 超了 → 先去掉括注「(…)」整块;
|
|
1048
|
+
* ③ 还超 → 只留主句(第一个「,」前的部分)。
|
|
1049
|
+
* 全程不 slice,不留省略号。
|
|
1050
|
+
*/
|
|
1051
|
+
const DERIVATION_MAX = 20
|
|
1052
|
+
function derivationBrief(text) {
|
|
1053
|
+
const value = String(text ?? '').replace(/\s+/gu, ' ').trim()
|
|
1054
|
+
if (Array.from(value).length <= DERIVATION_MAX) return value
|
|
1055
|
+
const withoutNote = value.replace(/([^)]*)/gu, '').trim()
|
|
1056
|
+
if (Array.from(withoutNote).length <= DERIVATION_MAX) return withoutNote
|
|
1057
|
+
return withoutNote.split(',')[0].trim()
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
/**
|
|
1061
|
+
* 把并发方案做成一张选项卡,请用户确认。
|
|
1062
|
+
*
|
|
1063
|
+
* 用官方 `ctx.userQuestions` seam —— 与 `ask_user_question` 工具同一套机制,
|
|
1064
|
+
* 因此 Web 端会渲染成标准的可点选卡片,而不是纯文本。
|
|
1065
|
+
*
|
|
1066
|
+
* @param {import('@deepseek-ai/cordis').Context} ctx
|
|
1067
|
+
* @param {object|undefined} agent
|
|
1068
|
+
* @param {object} verdict scoreTask 的决策结果
|
|
1069
|
+
* @param {object} [dimensions] scoreTask 的四维评分(用于卡片里的「依据」摘要)
|
|
1070
|
+
* @returns {Promise<{
|
|
1071
|
+
* text: string,
|
|
1072
|
+
* selected: string,
|
|
1073
|
+
* recommended: string,
|
|
1074
|
+
* override: boolean,
|
|
1075
|
+
* answered: boolean,
|
|
1076
|
+
* concurrencyApproved: boolean,
|
|
1077
|
+
* confirmed: boolean,
|
|
1078
|
+
* }>}
|
|
1079
|
+
* text:供报告尾部追加的「确认结果」段落;
|
|
1080
|
+
* selected:用户选中的选项 label(空串表示未选或自定义输入);
|
|
1081
|
+
* recommended:推荐项 label;
|
|
1082
|
+
* override:用户选了非推荐项(true 时必须按用户选择执行,并抑制原并行指令);
|
|
1083
|
+
* answered:用户**真的作答过**(选了某个选项或写了自定义输入)。
|
|
1084
|
+
* 未作答 / 超时 / 环境无提问能力 / 提问抛错 → false;
|
|
1085
|
+
* concurrencyApproved:该答复**是否构成「同意并发」**(唯一解锁依据)。
|
|
1086
|
+
* 由「用户选择 vs 建议 mode」的结构化语义表算出,不解析文案:
|
|
1087
|
+
* · 按建议执行 且建议 mode === 'parallel' → true;
|
|
1088
|
+
* · 明确改选为「并发 N 路」 → true;
|
|
1089
|
+
* · single 模式改选「改为派发子代理」(副标题「由你决定拆几路」)→ true
|
|
1090
|
+
* (L3:文案把拆分决定权交给执行方,本身就是并发授权,旗标必须同步);
|
|
1091
|
+
* · 改为串行 / 不派发 / 「改为串行派发」/ 自定义输入 / 未作答 → false。
|
|
1092
|
+
* confirmed:concurrencyApproved 的同义兼容别名(调用点与既有审计断言读它;
|
|
1093
|
+
* 语义自 R1 起收紧为「答复了,且答复本身就是同意并发」)。
|
|
1094
|
+
*
|
|
1095
|
+
* 导出说明:本函数是 P1 修复的核心(确认语义),导出供测试**直接**断言每个字段,
|
|
1096
|
+
* 而不是靠源码正则或间接推断 —— 修复必须能被单元测试钉死,否则下次重构会静默失效。
|
|
1097
|
+
*/
|
|
1098
|
+
/**
|
|
1099
|
+
* 三档并发选项(v10 子项四;自有设计②「下限/中位/上限三档,推荐下限」)。
|
|
1100
|
+
*
|
|
1101
|
+
* 区间来自 verdict.concurrencyRange(经 brief 对齐后的 range0):
|
|
1102
|
+
* min = 可下调下限(保证至少摊薄派发成本),max = 推荐值(单批纪律上限)。
|
|
1103
|
+
* 推荐档 preferred(默认 = min 下限;偏好覆盖)在 label 上标「(推荐)」;
|
|
1104
|
+
* 与推荐档不同且 delta > 1 的档位补「±k」后缀(delta ≤ 1 省略,避免「下限 2 路(+0)」噪声)。
|
|
1105
|
+
*
|
|
1106
|
+
* @param {number} min 区间下限(可下调下限)
|
|
1107
|
+
* @param {number} max 区间上限(推荐值)
|
|
1108
|
+
* @param {number} preferred 推荐路数(偏好或下限)
|
|
1109
|
+
* @param {string} sourceNote 偏好来源标注(无偏好为空串)
|
|
1110
|
+
* @param {string} taskNote 本任务适配短注(B2-4:如「本任务 3 个单元」;旧调用点不传 = 不追加)
|
|
1111
|
+
* @returns {Array<{label: string, description: string, concurrency: number}>}
|
|
1112
|
+
*/
|
|
1113
|
+
export function concurrencyTierOptions(min, max, preferred, sourceNote = '', taskNote = '') {
|
|
1114
|
+
const note = String(sourceNote ?? '')
|
|
1115
|
+
// ── R-02(2026-11-25 P1 收口):窄区间三档去重 ──
|
|
1116
|
+
// 候选档 [下限, 中位, 上限] 在窄区间会重复同一数值:
|
|
1117
|
+
// range 2-3 → [2, floor(5/2)=2, 3] = [2,2,3]
|
|
1118
|
+
// range 2-2 → [2,2,2]
|
|
1119
|
+
// 旧实现逐项 push,range 2-3 会产出两个完全相同的「改为并发 2 路(推荐)」,
|
|
1120
|
+
// range 2-2 会产出三个重复档(仲裁 R-02 实测)。先按数值去重(同值合并为单档),
|
|
1121
|
+
// 再逐档生成;去重后不足三档时在 description 标注「区间过窄」,如实告知可选档被合并
|
|
1122
|
+
// (不静默吞掉信息)。range 2-4 仍是完整三档,不受影响。
|
|
1123
|
+
const candidates = [min, Math.floor((min + max) / 2), max].filter((value) => Number.isFinite(value))
|
|
1124
|
+
const values = [...new Set(candidates)]
|
|
1125
|
+
const narrow = values.length < 3
|
|
1126
|
+
const tiers = []
|
|
1127
|
+
for (const value of values) {
|
|
1128
|
+
const delta = value - preferred
|
|
1129
|
+
const deltaNote = delta === 0
|
|
1130
|
+
? ''
|
|
1131
|
+
: Math.abs(delta) > 1
|
|
1132
|
+
? '(相对推荐 ' + (delta > 0 ? '+' : '') + delta + ' 路)'
|
|
1133
|
+
: ''
|
|
1134
|
+
const recommendNote = delta === 0 ? '(推荐)' + note : ''
|
|
1135
|
+
const narrowNote = narrow ? '(区间过窄,可选档不足三档)' : ''
|
|
1136
|
+
// B2-4:把「本任务有几个单元」写进档位描述(适配理由),不加功能介绍。
|
|
1137
|
+
const taskPart = String(taskNote ?? '') !== '' ? ';' + String(taskNote) : ''
|
|
1138
|
+
tiers.push({
|
|
1139
|
+
label: `改为并发 ${value} 路${recommendNote}${deltaNote}`,
|
|
1140
|
+
description: `${value} 路子代理并发执行${delta === 0 && note !== '' ? ',' + note.replace(/[()]/g, '') : ''}${narrowNote}${taskPart}`,
|
|
1141
|
+
concurrency: value,
|
|
1142
|
+
})
|
|
1143
|
+
}
|
|
1144
|
+
return tiers
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
/**
|
|
1148
|
+
* 偏好 → 推荐路数(v10 子项四)。
|
|
1149
|
+
*
|
|
1150
|
+
* 只在偏好值能安全落进本任务的并发区间 [min, max] 时采纳(越界不采纳——偏好是
|
|
1151
|
+
* 「路数习惯」,不能覆盖单批纪律上限与规模下限);并行模式外(serial/single)不采纳。
|
|
1152
|
+
* @param {{concurrency: number, source: string}|undefined} preference readConcurrencyPreference 的结果
|
|
1153
|
+
* @param {{min: number, max: number}} range 并发区间
|
|
1154
|
+
* @returns {{value: number, source: string|undefined}} value = 推荐路数;source 未采纳时 undefined
|
|
1155
|
+
*/
|
|
1156
|
+
export function preferredConcurrency(preference, range) {
|
|
1157
|
+
const value = Number(preference?.concurrency)
|
|
1158
|
+
const usable = Number.isFinite(value) && value >= 1
|
|
1159
|
+
&& range?.max !== undefined && value >= range.min && value <= range.max
|
|
1160
|
+
return usable
|
|
1161
|
+
? { value: Math.floor(value), source: String(preference.source ?? '偏好') }
|
|
1162
|
+
: { value: range?.min ?? 1, source: undefined }
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
1166
|
+
// 42-模型档第二问(2026-11-26):确认卡第二问「模型档选什么?」+ 答复按 id 分流
|
|
1167
|
+
// ═══════════════════════════════════════════════════════════════════════════
|
|
1168
|
+
//
|
|
1169
|
+
// 需求(用户点名):并发方案的确认卡后面再跟一问「模型档」,三选项
|
|
1170
|
+
// 强模型(推荐)/ 快速模型 / 指定模型(手填模型名,走 custom 通道)。
|
|
1171
|
+
// 答复回来后**按 question id 分流**(不再只读 answers[0]):
|
|
1172
|
+
// · 'concurrency-plan' → 走既有并发语义表(一字不动);
|
|
1173
|
+
// · 'model-tier' → 只影响「派给子代理用什么模型」,**不参与**并发授权判定
|
|
1174
|
+
// (模型档是资源选择,不是「同意并发」的表态 —— 两者绝不能互相污染,
|
|
1175
|
+
// 否则用户随便点个「快速模型」就把闸解锁了)。
|
|
1176
|
+
//
|
|
1177
|
+
// 手填模型名的校验链复用 check_subagent_selection 的既有链路(不新开一套):
|
|
1178
|
+
// ① hasDiscoveryTool —— 「list_subagent_models」对当前 agent 可见性(README:356 的同一条);
|
|
1179
|
+
// ② readAllowedModels —— 共享投影 subagentModelSelectionPolicy(官方白名单唯一真相源);
|
|
1180
|
+
// ③ allowedRoutesFrom —— 无记录策略时回退「父代理当前路由」(与官方 assertAllowedModelSelection 同义)。
|
|
1181
|
+
// 合法 → 把 provider/model 作为**派发参数**写进回执;不合法 → 回退强模型档并写明原因。
|
|
1182
|
+
// **禁止静默替换**:任何回退都必须出现在回执文字里(回执是模型读的那份报告)。
|
|
1183
|
+
|
|
1184
|
+
/** 第一问(并发方案)的 question id —— 构造与分流共用的唯一真相源。 */
|
|
1185
|
+
export const PLAN_QUESTION_ID = 'concurrency-plan'
|
|
1186
|
+
|
|
1187
|
+
/** 第二问(模型档)的 question id —— 构造与分流共用的唯一真相源。 */
|
|
1188
|
+
export const MODEL_TIER_QUESTION_ID = 'model-tier'
|
|
1189
|
+
|
|
1190
|
+
/**
|
|
1191
|
+
* 模型档三选项的 label(**构造选项与分流答复共用的唯一真相源**)。
|
|
1192
|
+
* 改文案必须同步改这里:分流只认这三条 label(不解析前缀,避免「改一个字就静默失效」)。
|
|
1193
|
+
*/
|
|
1194
|
+
export const MODEL_TIER_LABELS = Object.freeze({
|
|
1195
|
+
strong: '强模型(推荐)',
|
|
1196
|
+
fast: '快速模型',
|
|
1197
|
+
custom: '指定模型',
|
|
1198
|
+
})
|
|
1199
|
+
|
|
1200
|
+
/** 未作答 / 缺答 / 退化的统一缺省:强模型档,且**不**编造任何回执文字。 */
|
|
1201
|
+
function emptyModelTier() {
|
|
1202
|
+
return { name: 'strong', answered: false, route: undefined, notice: '', suffix: '' }
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
/** 取一条答复的 selected[0](非字符串 / 缺失一律空串,不猜)。 */
|
|
1206
|
+
function firstSelectedOf(answer) {
|
|
1207
|
+
const selected = Array.isArray(answer?.selected) ? answer.selected : []
|
|
1208
|
+
return typeof selected[0] === 'string' ? selected[0] : ''
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
/** 取一条答复的 custom(空串视为未填,与既有 custom 通道契约同款)。 */
|
|
1212
|
+
function customOf(answer) {
|
|
1213
|
+
return typeof answer?.custom === 'string' && answer.custom.length > 0 ? answer.custom : ''
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
/**
|
|
1217
|
+
* ── 答复按 id 分流(42 核心)──
|
|
1218
|
+
*
|
|
1219
|
+
* 官方返回形状(本机 app.asar 内 dsh-tool-ask-user/src/index.ts + dsh-user-questions/
|
|
1220
|
+
* src/types.ts):`{ answers: [{ id, selected: string[], custom?: string }] }`,一条问题
|
|
1221
|
+
* 一条答复、id 由调用方给定并被原样回传。因此**按 id 认领**是唯一不依赖顺序的读法。
|
|
1222
|
+
*
|
|
1223
|
+
* 兼容旧替身 / 第三方 answerer:只回一条、且 id 不是模型档时,按旧行为当作并发方案
|
|
1224
|
+
* 答复(旧实现就是 `answers[0]`;没有这条兜底,第三方 UI 会被当成「用户未作答」)。
|
|
1225
|
+
*
|
|
1226
|
+
* @param {{answers?: Array<object>}|undefined} answer
|
|
1227
|
+
* @returns {{plan: object|undefined, tier: object|undefined}}
|
|
1228
|
+
*/
|
|
1229
|
+
export function splitAnswerBatch(answer) {
|
|
1230
|
+
const list = Array.isArray(answer?.answers) ? answer.answers : []
|
|
1231
|
+
const byId = new Map()
|
|
1232
|
+
for (const item of list) {
|
|
1233
|
+
if (item === null || typeof item !== 'object') continue
|
|
1234
|
+
const id = item.id
|
|
1235
|
+
if (typeof id !== 'string' || id === '') continue
|
|
1236
|
+
if (byId.has(id)) continue // 同一 id 重复 → 只认第一条(不合并,避免猜)
|
|
1237
|
+
byId.set(id, item)
|
|
1238
|
+
}
|
|
1239
|
+
let plan = byId.get(PLAN_QUESTION_ID)
|
|
1240
|
+
if (plan === undefined && list.length === 1 && byId.size === 1) {
|
|
1241
|
+
const only = list[0]
|
|
1242
|
+
if (only !== null && typeof only === 'object' && only.id !== MODEL_TIER_QUESTION_ID) plan = only
|
|
1243
|
+
}
|
|
1244
|
+
return { plan, tier: byId.get(MODEL_TIER_QUESTION_ID) }
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
/**
|
|
1248
|
+
* 手填模型名 → 白名单路由(复用 check_subagent_selection 的三步链路)。
|
|
1249
|
+
*
|
|
1250
|
+
* 只做**校验与解析**,不派发:命中就返回该条白名单路由(provider + model),
|
|
1251
|
+
* 供回执写进派发参数。
|
|
1252
|
+
*
|
|
1253
|
+
* @param {object} ctx cordis context
|
|
1254
|
+
* @param {object} agent 调用方 agent
|
|
1255
|
+
* @param {string} typed 用户手填的模型名(可写 `model` 或 `provider/model`)
|
|
1256
|
+
* @returns {Promise<{ok: true, route: {provider: string, model: string}}
|
|
1257
|
+
* | {ok: false, reason: 'empty'|'no-discovery'|'no-whitelist'|'not-listed',
|
|
1258
|
+
* available?: Array<{provider: string, model: string}>}>}
|
|
1259
|
+
*/
|
|
1260
|
+
async function typedRouteOf(ctx, agent, typed) {
|
|
1261
|
+
const name = String(typed ?? '').trim().replace(/^`+|`+$/g, '')
|
|
1262
|
+
if (name === '') return { ok: false, reason: 'empty' }
|
|
1263
|
+
// ① list_subagent_models 可见性:注册=false 时模型无从查询可选路由(README:356 原文)。
|
|
1264
|
+
const discovery = hasDiscoveryTool(ctx, agent)
|
|
1265
|
+
if (discovery === false) return { ok: false, reason: 'no-discovery' }
|
|
1266
|
+
// ② 本会话白名单(共享投影;读路径不注册,见 projection.mjs P1-2)。
|
|
1267
|
+
const policy = await readAllowedModels(ctx, agent)
|
|
1268
|
+
// ③ 无记录策略 → 官方只允许父代理当前路由(allowedRoutesFrom 已按此回退)。
|
|
1269
|
+
const routes = allowedRoutesFrom(agent, policy)
|
|
1270
|
+
if (!Array.isArray(routes) || routes.length === 0) return { ok: false, reason: 'no-whitelist' }
|
|
1271
|
+
const lowered = name.toLowerCase()
|
|
1272
|
+
const hit = routes.find((route) =>
|
|
1273
|
+
String(route.model).toLowerCase() === lowered
|
|
1274
|
+
|| `${route.provider}/${route.model}`.toLowerCase() === lowered)
|
|
1275
|
+
if (hit === undefined) return { ok: false, reason: 'not-listed', available: routes }
|
|
1276
|
+
return { ok: true, route: { provider: hit.provider, model: hit.model } }
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
/** 白名单的可选路由短列(写进回退回执,帮用户知道「该填什么」;超 5 条只说条数)。 */
|
|
1280
|
+
function availableRoutesNote(routes) {
|
|
1281
|
+
const shown = routes.slice(0, 5).map((route) => `${route.provider}/${route.model}`)
|
|
1282
|
+
const more = routes.length > shown.length ? ` 等 ${routes.length} 条` : ''
|
|
1283
|
+
return shown.join('、') + more
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
/**
|
|
1287
|
+
* 具名档位 → 白名单路由(【档位路由显式化 2026-11-27】)。
|
|
1288
|
+
*
|
|
1289
|
+
* 纯函数、零 I/O:把 tier-policy.resolveTierRoute 的映射结果收敛成两种结局,
|
|
1290
|
+
* 供强/快档分支与测试共用(「不复制第二份逻辑」纪律 —— 映射规则只活在 tier-policy):
|
|
1291
|
+
* · 解析成功 → { tier, route }(route 含 provider/model,白名单条目带 effort 才附带);
|
|
1292
|
+
* · 解析失败(no-whitelist / no-match 等失败 reason)→ { tier, route: undefined }。
|
|
1293
|
+
*
|
|
1294
|
+
* 档位映射:强模型档 → 'deep';快速模型档 → 'cheap'(TIER_IDS 的 'standard' 属评分器域,
|
|
1295
|
+
* 第二问没有对应选项,不走此函数)。失败不编造:绝不拿别的模型顶上。
|
|
1296
|
+
*
|
|
1297
|
+
* @param {'deep'|'cheap'} tier TIER_IDS 中的具名档位
|
|
1298
|
+
* @param {Array<{provider: string, model: string, effort?: string}>} allowedRoutes 本会话白名单
|
|
1299
|
+
* @param {object} [policy] 档位策略(缺省 defaultTierPolicy(),仅白名单启发式)
|
|
1300
|
+
* @returns {{tier: 'deep'|'cheap', route: {provider: string, model: string, effort?: string}|undefined}}
|
|
1301
|
+
*/
|
|
1302
|
+
export function resolveNamedTierOf(tier, allowedRoutes, policy = defaultTierPolicy()) {
|
|
1303
|
+
const verdict = resolveTierRoute(tier, allowedRoutes, policy)
|
|
1304
|
+
return verdict.ok === true
|
|
1305
|
+
? { tier, route: verdict.route }
|
|
1306
|
+
: { tier, route: undefined }
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
/**
|
|
1310
|
+
* ── 模型档答复 → 结构化结论 + 回执文字(42;【档位路由显式化 2026-11-27】升级)──
|
|
1311
|
+
*
|
|
1312
|
+
* 三种结局各自带到回执里(**绝不静默替换**):
|
|
1313
|
+
* · 强模型 / 快速模型:写明选中的档;**本会话白名单可解析时**还给出对应的
|
|
1314
|
+
* provider/model 派发参数(回执写「→ provider/model」与原样可抄的传参写法);
|
|
1315
|
+
* 白名单解析不出(no-whitelist / no-match)→ 如实写「沿用徽标/父路由」,
|
|
1316
|
+
* route 保持 undefined —— 绝不拿别的模型顶上,也绝不编造一条路由;
|
|
1317
|
+
* · 指定模型 + 命中白名单:写明「已按你指定的模型 X 派发(provider=…, model=…)」;
|
|
1318
|
+
* · 指定模型 + 未命中/无法校验:**回退强模型档**,并写明具体原因与可选路由
|
|
1319
|
+
* (回退语义仍是「沿用父路由」,不按档位解析路由 —— 「绝不拿别的模型顶上」不变式保持)。
|
|
1320
|
+
*
|
|
1321
|
+
* @param {object} ctx cordis context
|
|
1322
|
+
* @param {object} agent 调用方 agent
|
|
1323
|
+
* @param {object|undefined} tierAnswer 第二问的答复(可能缺失)
|
|
1324
|
+
* @param {object} [policy] 档位策略(缺省 defaultTierPolicy();测试注入点)
|
|
1325
|
+
* @returns {Promise<{name: 'strong'|'fast'|'custom', answered: boolean,
|
|
1326
|
+
* route: {provider: string, model: string}|undefined, notice: string, suffix: string}>}
|
|
1327
|
+
*/
|
|
1328
|
+
async function resolveModelTierOf(ctx, agent, tierAnswer, policy = defaultTierPolicy()) {
|
|
1329
|
+
// 【变更 2026-11-27 档位配置】会话档位绑定优先:用户经 /tier 显式编辑过的档位
|
|
1330
|
+
// (如 /tier 深度 → 某 provider/model)必须压过白名单关键词启发式 ——
|
|
1331
|
+
// 合并序 { ...默认策略, ...会话绑定 },显式绑定覆盖同档缺省(空串启发式),
|
|
1332
|
+
// 未绑定的档位维持缺省。readTierConfig 失败/未设置 → undefined → 纯缺省,行为逐字不变。
|
|
1333
|
+
// 【变更 2026-11-27 badge 绑定并读】badgeBindings 是 01 徽标「深度思考代理模型」
|
|
1334
|
+
// 二级菜单写下的 UI 绑定(readTierBindings 读徽标宿主会话表,async、读不到 →
|
|
1335
|
+
// undefined);tier-config(/tier 命令)仍优先于它 —— 合并序 badge → tier-config。
|
|
1336
|
+
const sessionKey = sessionKeyOf(agent)
|
|
1337
|
+
const badgeBindings = await readTierBindings(ctx, sessionKey)
|
|
1338
|
+
const sessionPolicy = { ...(badgeBindings ?? {}), ...(readTierConfig(sessionKey) ?? {}) }
|
|
1339
|
+
const effectivePolicy = { ...(policy ?? defaultTierPolicy()), ...(sessionPolicy ?? {}) }
|
|
1340
|
+
const selected = firstSelectedOf(tierAnswer)
|
|
1341
|
+
const custom = customOf(tierAnswer)
|
|
1342
|
+
if (selected === '' && custom === '') return emptyModelTier()
|
|
1343
|
+
if (selected === MODEL_TIER_LABELS.strong) {
|
|
1344
|
+
// 前缀刻意用「派发模型档」而不是「模型档」:报告正文里已有「模型档:深度档」
|
|
1345
|
+
// (= 评分器推荐的档位),两者同时出现时用不同前缀才分得清「建议」与「用户的选择」。
|
|
1346
|
+
const notice = '派发模型档:强模型(更仔细、更慢、更贵)'
|
|
1347
|
+
// 【档位路由显式化 2026-11-27】白名单可解析 → 按深度档给出具体派发路由;
|
|
1348
|
+
// 解析不出(no-whitelist / no-match)→ route 保持 undefined,回执如实写沿用父路由。
|
|
1349
|
+
// 【变更 2026-11-27 档位配置】policy → effectivePolicy:/tier 绑定的深度模型优先。
|
|
1350
|
+
const resolved = resolveNamedTierOf('deep', allowedRoutesFrom(agent, await readAllowedModels(ctx, agent)), effectivePolicy)
|
|
1351
|
+
if (resolved.route === undefined) {
|
|
1352
|
+
const suffix = notice + ';本会话白名单无匹配路由,沿用徽标/父路由'
|
|
1353
|
+
return { name: 'strong', answered: true, route: undefined, notice, suffix }
|
|
1354
|
+
}
|
|
1355
|
+
const route = resolved.route
|
|
1356
|
+
const suffix = `${notice} → ${formatRoute(route)}(派发时传 provider: "${route.provider}", model: "${route.model}")`
|
|
1357
|
+
return { name: 'strong', answered: true, route, notice, suffix }
|
|
1358
|
+
}
|
|
1359
|
+
if (selected === MODEL_TIER_LABELS.fast) {
|
|
1360
|
+
const notice = '派发模型档:快速模型(挑白名单里更快更便宜的)'
|
|
1361
|
+
// 【档位路由显式化 2026-11-27】与强模型档同一条解析链(同一函数、同一 policy),
|
|
1362
|
+
// 只是档位映射不同:快速模型档 → 'cheap'。
|
|
1363
|
+
// 【变更 2026-11-27 档位配置】policy → effectivePolicy:/tier 绑定的快速模型优先。
|
|
1364
|
+
const resolved = resolveNamedTierOf('cheap', allowedRoutesFrom(agent, await readAllowedModels(ctx, agent)), effectivePolicy)
|
|
1365
|
+
if (resolved.route === undefined) {
|
|
1366
|
+
const suffix = notice + ';本会话白名单无匹配路由,沿用徽标/父路由'
|
|
1367
|
+
return { name: 'fast', answered: true, route: undefined, notice, suffix }
|
|
1368
|
+
}
|
|
1369
|
+
const route = resolved.route
|
|
1370
|
+
const suffix = `${notice} → ${formatRoute(route)}(派发时传 provider: "${route.provider}", model: "${route.model}")`
|
|
1371
|
+
return { name: 'fast', answered: true, route, notice, suffix }
|
|
1372
|
+
}
|
|
1373
|
+
// 用户**没有**点任何选项、只写了自定义文本:按「手填模型名」同等对待
|
|
1374
|
+
// (与既有 custom 通道契约一致:selected 空 + custom 非空 = 用户自己写的答案)。
|
|
1375
|
+
const typed = selected === MODEL_TIER_LABELS.custom ? custom : (selected === '' ? custom : '')
|
|
1376
|
+
if (selected !== MODEL_TIER_LABELS.custom && selected !== '') {
|
|
1377
|
+
// 未登记的 label(宿主 / 第三方 UI 回填)→ 不猜档位,按缺省强模型档并如实标注。
|
|
1378
|
+
const notice = `⚠ 未识别的模型档选项「${selected}」,已回退强模型档`
|
|
1379
|
+
return { name: 'strong', answered: true, route: undefined, notice, suffix: notice }
|
|
1380
|
+
}
|
|
1381
|
+
const verdict = await typedRouteOf(ctx, agent, typed)
|
|
1382
|
+
if (verdict.ok === true) {
|
|
1383
|
+
const route = verdict.route
|
|
1384
|
+
const notice = `已按你指定的模型 ${formatRoute(route)} 派发`
|
|
1385
|
+
return {
|
|
1386
|
+
name: 'custom',
|
|
1387
|
+
answered: true,
|
|
1388
|
+
route,
|
|
1389
|
+
notice,
|
|
1390
|
+
suffix: `${notice}(派发时传 provider: "${route.provider}", model: "${route.model}")`,
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
const shownName = String(typed ?? '').trim() === '' ? '(空)' : `「${String(typed).trim()}」`
|
|
1394
|
+
const reasonText = verdict.reason === 'empty'
|
|
1395
|
+
? '选了「指定模型」但没填模型名'
|
|
1396
|
+
: verdict.reason === 'no-discovery'
|
|
1397
|
+
? `本会话没有 list_subagent_models,无法校验手填模型${shownName}`
|
|
1398
|
+
: verdict.reason === 'no-whitelist'
|
|
1399
|
+
? `读不到本会话白名单,无法校验手填模型${shownName}`
|
|
1400
|
+
: `填的模型${shownName}不在白名单`
|
|
1401
|
+
const options = Array.isArray(verdict.available) && verdict.available.length > 0
|
|
1402
|
+
? `(可选:${availableRoutesNote(verdict.available)})`
|
|
1403
|
+
: ''
|
|
1404
|
+
const notice = `⚠ ${reasonText},已回退强模型档${options}`
|
|
1405
|
+
return { name: 'strong', answered: true, route: undefined, notice, suffix: notice }
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
/**
|
|
1409
|
+
* 构造第二问「模型档选什么?」。
|
|
1410
|
+
*
|
|
1411
|
+
* 强模型档的描述写**本任务适配理由**(读 verdict.modelTier / dimensions.deep 同一次判定,
|
|
1412
|
+
* 不新判):推理重 → 「{任务性质}重推理,更仔细更慢更贵」;其余 → 「稳一点更保险,更仔细更慢更贵」。
|
|
1413
|
+
* 描述预算 30 字(与既有选项短句同款,GUI 单行装得下)。
|
|
1414
|
+
*
|
|
1415
|
+
* @param {object} verdict 生效 verdict(读 modelTier,不重算)
|
|
1416
|
+
* @param {object} dimensions 四维评分(读 deep.level)
|
|
1417
|
+
* @param {object|null} narrative 同一份分析的文案素材(取 nature;缺失不编造)
|
|
1418
|
+
* @param {object} [tierConfig] 【变更 2026-11-27 档位配置】readTierConfig(sessionKey) 的结果
|
|
1419
|
+
* (deep/standard/cheap → {provider,model,effort?};未绑定/读取失败 = undefined)。
|
|
1420
|
+
* 只用于 detail 首句提示「当前深度思考模型」;缺失时逐字退回旧文案。
|
|
1421
|
+
* @returns {{id: string, header: string, question: string, detail: string,
|
|
1422
|
+
* options: Array<{label: string, description: string}>}}
|
|
1423
|
+
*/
|
|
1424
|
+
export function modelTierQuestionOf(verdict, dimensions, narrative, tierConfig) {
|
|
1425
|
+
const rawNature = narrative === null || narrative === undefined ? '' : String(narrative.nature ?? '')
|
|
1426
|
+
// natureNote 不再按 12 字切片(半句话的另一个来源):整词装得下才带,装不下就不带。
|
|
1427
|
+
const natureNote = rawNature === '' ? '' : `${rawNature}类`
|
|
1428
|
+
const deepLevel = dimensions?.deep?.level
|
|
1429
|
+
const heavy = verdict?.modelTier === 'deep' || deepLevel === 'high'
|
|
1430
|
+
// 【截断病修复 2026-11-26】描述超 30 字时不再 clipSegment(会把「更仔细更慢更贵」砍半句),
|
|
1431
|
+
// 改为整句候选降级:带性质 → 不带性质 → 短句。
|
|
1432
|
+
const strongReason = heavy
|
|
1433
|
+
? pickWholeOf([
|
|
1434
|
+
`${natureNote}重推理,更仔细更慢更贵`,
|
|
1435
|
+
'重推理,更仔细更慢更贵',
|
|
1436
|
+
'稳一点更保险,更仔细更慢更贵',
|
|
1437
|
+
], CARD_SEGMENT_MAX)
|
|
1438
|
+
: '稳一点更保险,更仔细更慢更贵'
|
|
1439
|
+
return {
|
|
1440
|
+
id: MODEL_TIER_QUESTION_ID,
|
|
1441
|
+
header: '模型档',
|
|
1442
|
+
question: '模型档选什么?',
|
|
1443
|
+
// 【变更 2026-11-27 档位配置】detail 首句按本会话深度绑定分两种形态(披露当前深度模型):
|
|
1444
|
+
// · tierConfig?.deep 有值 → 「当前深度思考模型:provider/model。」
|
|
1445
|
+
// · 未绑定 → 「深度思考模型未设置(可先 /tier 深度 选择)。」
|
|
1446
|
+
// 后半句「手填模型名会先查本会话白名单…」逐字保留(既有断言钉「白名单/悄悄」)。
|
|
1447
|
+
// 选项顺序 / 标签 / 描述一字不动。
|
|
1448
|
+
detail: (tierConfig !== null && typeof tierConfig === 'object' && tierConfig.deep !== undefined
|
|
1449
|
+
? `当前深度思考模型:${String(tierConfig.deep.provider ?? '')}/${String(tierConfig.deep.model ?? '')}。`
|
|
1450
|
+
: '深度思考模型未设置(可先 /tier 深度 选择)。')
|
|
1451
|
+
+ '手填模型名会先查本会话白名单;不在白名单就回退强模型档,不会悄悄换成别的模型。',
|
|
1452
|
+
options: [
|
|
1453
|
+
{ label: MODEL_TIER_LABELS.strong, description: strongReason },
|
|
1454
|
+
{ label: MODEL_TIER_LABELS.fast, description: '便宜快,适合机械操作型' },
|
|
1455
|
+
{ label: MODEL_TIER_LABELS.custom, description: '手填模型名' },
|
|
1456
|
+
],
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
/**
|
|
1461
|
+
* ── R-01(2026-11-25 P1 收口):偏好 → 显示用推荐路数(卡片 / 报告 / 回执 / 闸单一真相源)──
|
|
1462
|
+
*
|
|
1463
|
+
* 仲裁 R-01:偏好=3 时,卡面/问题/选项/闸授权都是 3 路,但「评定行 / 完整报告 / 回执」
|
|
1464
|
+
* 仍按 verdict.concurrency(默认 4 路)渲染 → 模型按报告派 4 路,第 4 路被上限 3 拦,
|
|
1465
|
+
* 自相矛盾。根因是这三处各自用未叠加偏好的 verdict 生成。
|
|
1466
|
+
*
|
|
1467
|
+
* 修法:把「范围 → 偏好 → 推荐路数 → 来源标注」抽成**唯一实现**,execute(重建报告)
|
|
1468
|
+
* 与 askUserToConfirmConcurrency(评定行 / 回执)共用,绝不复制计算。无偏好时
|
|
1469
|
+
* preferredUsable=false,modeLabel 仍逐字等于旧版(冻结兼容),绝不改写默认卡片。
|
|
1470
|
+
*
|
|
1471
|
+
* @param {object} verdict 用于卡片/闸的 effectiveVerdict(plan 与 verdict 同 mode)
|
|
1472
|
+
* @param {object} dimensions 四维评分
|
|
1473
|
+
* @param {string} task 任务原文(空串则跳过任务特定区间,只用 verdict.concurrencyRange)
|
|
1474
|
+
* @param {object|undefined} preference readConcurrencyPreference 的结果
|
|
1475
|
+
* @returns {{detailTask0: string, brief0: object|null, range0: {min:number,max:number},
|
|
1476
|
+
* preferred: object, preferredUsable: boolean, recConcurrency: number, recSourceNote: string}}
|
|
1477
|
+
*
|
|
1478
|
+
* 【变更 2026-11-30 v1.0.0】导出供 /dispatch 命令复用:命令入口与 suggest_concurrency
|
|
1479
|
+
* 必须走同一份「偏好 → 推荐路数 → 简报」计算,绝不复制第二份公式(避免口径漂移)。
|
|
1480
|
+
*/
|
|
1481
|
+
export function resolvePreferredDisplay(verdict, dimensions, task, preference) {
|
|
1482
|
+
const detailTask0 = typeof task === 'string' ? task.trim() : ''
|
|
1483
|
+
// W3-F-22:整张卡片只做一次结构解析(brief / 叙事 / 批次都复用同一份 analysis)。
|
|
1484
|
+
const analysis0 = detailTask0 === '' ? null : analyzeTask(detailTask0)
|
|
1485
|
+
const briefBase = detailTask0 === ''
|
|
1486
|
+
? null
|
|
1487
|
+
: buildReasoningBrief(detailTask0, { dimensions, verdict, analysis: analysis0 })
|
|
1488
|
+
const range0 = { min: briefBase !== null ? briefBase.rangeMin : verdict.concurrencyRange.min,
|
|
1489
|
+
max: briefBase !== null ? briefBase.rangeMax : verdict.concurrencyRange.max }
|
|
1490
|
+
const preferred = preferredConcurrency(preference, range0)
|
|
1491
|
+
const preferredUsable = preferred.source !== undefined
|
|
1492
|
+
const recConcurrency = preferredUsable ? preferred.value : verdict.concurrency
|
|
1493
|
+
// ── W3-F-02(P1):偏好生效时先重建**最终 verdict**,卡片/报告/闸共用同一份数字 ──
|
|
1494
|
+
// 旧实现只覆写 concurrency / concurrencyRange:batches 仍是旧值(10 件按 3 路应为 4 批,
|
|
1495
|
+
// 却写「要分 2 批」)、efficiencyGain 仍是按 8 路算的 5×(卡片「8 路同跑,约快 5 倍」)。
|
|
1496
|
+
// finalVerdict 是唯一重建点(concurrency.mjs 的 verdictForConcurrency),下面所有
|
|
1497
|
+
// 展示字段(brief0 / 效率行 / 批次行 / 报告正文)都从它派生。
|
|
1498
|
+
// 有效子任务数:显式单元任务(①…②…)的 scale.count 可能为 0,与 buildStructureVerdict
|
|
1499
|
+
// 同一口径用 analyzeTask().unitCount 兜底(批次/效率必须按真实件数算)。
|
|
1500
|
+
const subtasksOfTask = detailTask0 === '' ? 0 : analyzeTask(detailTask0).unitCount
|
|
1501
|
+
const finalVerdict = preferredUsable && verdict.mode === 'parallel'
|
|
1502
|
+
? verdictForConcurrency(verdict, dimensions, recConcurrency, subtasksOfTask)
|
|
1503
|
+
: verdict
|
|
1504
|
+
const brief0 = detailTask0 === ''
|
|
1505
|
+
? null
|
|
1506
|
+
: (finalVerdict === verdict
|
|
1507
|
+
? briefBase
|
|
1508
|
+
: buildReasoningBrief(detailTask0, { dimensions, verdict: finalVerdict, analysis: analysis0 }))
|
|
1509
|
+
// 来源标注:[人话化 2026-11-26] 「(偏好:工作区层)」→「(按你上次选的)」——
|
|
1510
|
+
// 普通用户只需要知道「这是照你以前的习惯」,层名是插件内部概念;完整来源
|
|
1511
|
+
// 仍在 preference.source / layer 里供诊断。
|
|
1512
|
+
const recSourceNote = preferredUsable ? '(按你上次选的)' : ''
|
|
1513
|
+
return { detailTask0, brief0, analysis0, range0, preferred, preferredUsable, recConcurrency, recSourceNote, finalVerdict }
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1517
|
+
// B2-4(2026-11-26):确认卡文案深化 —— 四维评分对照研究的三处差距
|
|
1518
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1519
|
+
//
|
|
1520
|
+
// 依据:36-批次2-立项/01-卡片优化-B2-4.md。N3 批已吸收「判断先行 / 不派发主动推荐 /
|
|
1521
|
+
// 三档选项 / 偏好两层」,参照设计仍领先的三处(本区要补的):
|
|
1522
|
+
// ① 人话推导 vs 标签拼接:它的问句是「散乱度中高、依赖低、规模小、难度高(安全闸
|
|
1523
|
+
// 审计)→ 推荐 heavy 分身扇出」—— 四维结论带**本任务上下文**(括注任务性质),
|
|
1524
|
+
// 推荐带一句**为什么**(从命中词推出来)。我们的评定行只有标量标签。
|
|
1525
|
+
// ② option description 写的是**本任务适配理由**(引用拆解出的单元内容),不是通道
|
|
1526
|
+
// 功能介绍(「操作型并发,结论要求更稳」这种 = 已被点名的反模式)。
|
|
1527
|
+
// ③ 劝退项(不派发 / 不改并发)要从**任务规模信号**说「这次为什么不划算」。
|
|
1528
|
+
//
|
|
1529
|
+
// 素材纪律(N3「不复制第二份逻辑」):
|
|
1530
|
+
// 四维 / 拆解单元 / facts 命中词全部取自 resolveReasoning 的**同源产物** ——
|
|
1531
|
+
// brief(buildReasoningBrief)、analyzeTask(resolveReasoning 内部同一次结构分析)、
|
|
1532
|
+
// verdict.facts(评分器产出的结构化事实)。本区只做「取材 → 压缩 → 排版」,
|
|
1533
|
+
// 不新增任何判定阈值、不重算评分。
|
|
1534
|
+
//
|
|
1535
|
+
// 硬预算(宿主卡片 + 冻结测试):
|
|
1536
|
+
// · detail 每个列表项 ≤ 30 字(test.mjs [34]「每段 ≤ 30 字(短句预算)」)——
|
|
1537
|
+
// 所以「一句话推导」在本卡片上**拆成三项连读**:
|
|
1538
|
+
// 评定行「**评定:{模式}{推导}|{任务性质}**」
|
|
1539
|
+
// → 结论行「结论:{四维}({任务性质})→ 推荐{X}」
|
|
1540
|
+
// → 理由行「理由:{facts 命中词}」
|
|
1541
|
+
// 括注优先放结论行(与这一句式同构);结论行放不下时退到评定行尾(|性质)。
|
|
1542
|
+
// · **例外:「拆解:」行** —— 单元只写**标题**(≤10 字宽,见 unitTitleOf),4 个标题连排
|
|
1543
|
+
// 可 > 30 字(多行 Markdown 完整渲染);test [34]/[64] 的 ≤30 断言显式豁免它。
|
|
1544
|
+
// 【截断病修复 2026-11-26】这里过去写的是「完整单元文本不截断」(当时口径是为了躲
|
|
1545
|
+
// 选项行的省略号),结果卡面被 4 条单元长句撑成一大段。用户口径改为:**卡片列标题,
|
|
1546
|
+
// 全文留在工具返回的推理报告里**(报告通道一字未动)。
|
|
1547
|
+
// · 首行仍必须是以粗体「评定:」开头(integration §22b.14 冻结);
|
|
1548
|
+
// · **选项描述只留通道短句(≤ 30 字)**:旧实现把「本任务拆解:①…②…③…;通道句」
|
|
1549
|
+
// 整段塞进 option.description(每单元还截 15 字),三段拼起来仍超 GUI 单行宽 →
|
|
1550
|
+
// 宿主加省略号(用户两次截图投诉)。现完整拆解只在 detail「拆解:」行,选项行短。
|
|
1551
|
+
//
|
|
1552
|
+
// 任务为空(直接调本函数、传合成 verdict 的单测)时 narrative = null:
|
|
1553
|
+
// 所有「本任务」文案**逐字退回旧行为**,冻结的选项文案/顺序零影响。
|
|
1554
|
+
|
|
1555
|
+
/** 卡片段预算:detail 单个列表项 ≤ 30 字(test [34] 冻结,含「- 」标签在内)。
|
|
1556
|
+
* 【截断病修复 2026-11-26】超预算的处理**只有一条路:源头改写**(换说法 / 丢括注 /
|
|
1557
|
+
* 退标题),不再有 clipSegment 切半句这道兜底。 */
|
|
1558
|
+
const CARD_SEGMENT_MAX = 30
|
|
1559
|
+
/** 选项描述里的通道短句预算:30 - 5(「一件件做:」这类前缀)= 25。 */
|
|
1560
|
+
const CARD_CHANNEL_MAX = 25
|
|
1561
|
+
/** 「理由:」段的正文预算:30 - 3(标签)= 27。 */
|
|
1562
|
+
const CARD_REASON_MAX = CARD_SEGMENT_MAX - 3
|
|
1563
|
+
/** 任务性质全文上限:退到评定行尾时的兜底(不参与结论行预算)。 */
|
|
1564
|
+
const CARD_NATURE_MAX = 18
|
|
1565
|
+
/**
|
|
1566
|
+
* detail「拆解:」行最多点名几个单元(其后的以「…共 N 个」收口)。
|
|
1567
|
+
* 【截断病修复 2026-11-26】3 → 4:用户点名的形态是「①结构审计A ②结构审计B ③安全审计A
|
|
1568
|
+
* ④安全审计B」——4 个标题全列(标题化后每个 ≤10 字,一行装得下),不再出现「…共 N 个」。
|
|
1569
|
+
*/
|
|
1570
|
+
const UNIT_SUMMARY_SHOWN = 4
|
|
1571
|
+
|
|
1572
|
+
/**
|
|
1573
|
+
* 单元标题的字数预算(≈10 字)。
|
|
1574
|
+
* 「字」按**显示宽度**计:CJK 2 列、ASCII 1 列 —— 因为标题里常带文件名
|
|
1575
|
+
* (「shared.mjs 修复」= 10 列英文 + 2 汉字,字面 13 字符但视觉只占 7 个汉字宽)。
|
|
1576
|
+
* 用户给的样例「修改 lib/shared.mjs 里的 parse 函数 → shared.mjs 修复」正好落在 20 列内。
|
|
1577
|
+
*/
|
|
1578
|
+
const UNIT_TITLE_WIDTH_MAX = 20
|
|
1579
|
+
|
|
1580
|
+
/** 中文内容词判定:'format' / 'design' 这类 ASCII 命中词是标识符,不当「任务性质」。 */
|
|
1581
|
+
const CJK_TEXT_RE = /[\u3400-\u9fff]/
|
|
1582
|
+
|
|
1583
|
+
/**
|
|
1584
|
+
* 单元标号字形表(①…⑩)。
|
|
1585
|
+
* 只借字形做排版;标号语义(第 N 个单元)仍由 concurrency.mjs 的 unitLabel 决定 ——
|
|
1586
|
+
* 这里不 import 内部函数(concurrency.mjs 未导出 unitLabel),也不定义新规则。
|
|
1587
|
+
*/
|
|
1588
|
+
const UNIT_MARK_GLYPHS = [
|
|
1589
|
+
'①', '②', '③', '④', '⑤', '⑥', '⑦', '⑧', '⑨', '⑩',
|
|
1590
|
+
// 【截断病修复】11 个以后过去退化成「11.」,拼进标号串会粘成「⑨1.」「11.…共 60 个」
|
|
1591
|
+
// 这种读不通的怪物(病态输入实测)。补 ⑪…⑳ 后标号串恒为单字形。
|
|
1592
|
+
'⑪', '⑫', '⑬', '⑭', '⑮', '⑯', '⑰', '⑱', '⑲', '⑳',
|
|
1593
|
+
]
|
|
1594
|
+
function unitMarkAt(index) {
|
|
1595
|
+
return UNIT_MARK_GLYPHS[index] ?? String(index + 1) + '.'
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
// ── 卡片层不允许字符级截断(2026-11-26「截断病」修复)───────────────────
|
|
1599
|
+
// 旧实现在这里有一个 clipSegment(text, max):「超 max 字就切到 max-1 字再加 …」。
|
|
1600
|
+
// 它把用户可见文案砍成半句(卡面实例:「(把项目里那份特别长的变更说…)」、
|
|
1601
|
+
// 「①③⑤⑦⑨⑪…等 60 个 都要改 lib/a…」、「理由:①③⑤⑦⑨11.…共 60 个 改的是同一个文件,分…」)。
|
|
1602
|
+
// 用户原则:**用户可见文案禁止字符级截断,超预算必须源头改写文案,不许补省略号了事**。
|
|
1603
|
+
// 因此该函数已删除,卡片的每个组装点改为「整句候选里挑一句装得下的」:
|
|
1604
|
+
// · pickWholeOf(本文件):给若干种说法,取第一个装得下的,一件都不切;
|
|
1605
|
+
// · sentBriefOf(concurrency.mjs):同上,用于瓶颈 / 判据两段。
|
|
1606
|
+
// 想加新文案时请沿用「改写候选」而不是「截断」——这里刻意不留兜底截断工具,
|
|
1607
|
+
// 就是为了让截断病没有回来的路。
|
|
1608
|
+
// (报告通道仍用 truncateSafe / quoteOf:那是给模型读的正文,不在本纪律范围内。)
|
|
1609
|
+
|
|
1610
|
+
/**
|
|
1611
|
+
* 从 verdict.facts 取一条事实(形状见 docs/FACTS-CONTRACT.md §3;缺失返回 undefined)。
|
|
1612
|
+
* 防御式读取:facts 可能被合成 verdict 省略,读不到就当作「没有这条事实」。
|
|
1613
|
+
*/
|
|
1614
|
+
function factOf(verdict, code) {
|
|
1615
|
+
const facts = Array.isArray(verdict?.facts) ? verdict.facts : []
|
|
1616
|
+
return facts.find((fact) => fact !== null && typeof fact === 'object' && fact.code === code)
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
/** 取某条事实的中文命中词(前 limit 个):渲染层只展示,不改判定。 */
|
|
1620
|
+
function cjkWordsOf(fact, limit = 2) {
|
|
1621
|
+
const words = Array.isArray(fact?.words) ? fact.words : []
|
|
1622
|
+
return words
|
|
1623
|
+
.filter((word) => typeof word === 'string' && word !== '' && CJK_TEXT_RE.test(word))
|
|
1624
|
+
.slice(0, limit)
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
/**
|
|
1628
|
+
* 任务原文首句切片(性质兜底素材):去掉枚举标号与「帮我」类口头语,取第一个分句。
|
|
1629
|
+
* 只做排版净化,不做语义改写 —— 切片始终是原文子串。
|
|
1630
|
+
*/
|
|
1631
|
+
function taskHeadOf(taskText) {
|
|
1632
|
+
const first = String(taskText ?? '')
|
|
1633
|
+
.split(/[,,。;;::\n]/)
|
|
1634
|
+
.map((part) => part.trim())
|
|
1635
|
+
.find((part) => part !== '') ?? ''
|
|
1636
|
+
const withoutMark = first
|
|
1637
|
+
// 圈号标号(①②)本身就是标记,可直接剥;
|
|
1638
|
+
.replace(/^[①②③④⑤⑥⑦⑧⑨⑩]+\s*/u, '')
|
|
1639
|
+
// 阿拉伯数字只有**带分隔符**时才是标号(「1. 」「2、」「3)」「4:」)——
|
|
1640
|
+
// 否则「21 项审计」会被剥成「项审计」(B2-4 回归实测)。
|
|
1641
|
+
.replace(/^[0-9]+\s*[.、.))::]\s*/u, '')
|
|
1642
|
+
return withoutMark
|
|
1643
|
+
.replace(/^(帮我|请帮我|麻烦你?|请)\s*/u, '')
|
|
1644
|
+
.trim()
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
/**
|
|
1648
|
+
* 任务领域词表:任务原文里没有可引用的首句(太长)时,用**领域词改写**出短性质标签。
|
|
1649
|
+
* 与 facts 命中词同一用途(「这卡是给哪类活做建议」),只是素材换成任务原文本身。
|
|
1650
|
+
*/
|
|
1651
|
+
const TASK_THEME_RULES = [
|
|
1652
|
+
[/重构|重写/u, '重构'],
|
|
1653
|
+
[/架构/u, '架构'],
|
|
1654
|
+
[/设计|方案/u, '设计'],
|
|
1655
|
+
[/性能|优化|加速|提速/u, '性能'],
|
|
1656
|
+
[/测试|用例|覆盖率/u, '测试'],
|
|
1657
|
+
[/文档|说明|注释|readme/iu, '文档'],
|
|
1658
|
+
[/变更|发布|上线|版本/u, '变更'],
|
|
1659
|
+
[/迁移|升级|切换/u, '迁移'],
|
|
1660
|
+
[/审计|审查|检查|复核/u, '审查'],
|
|
1661
|
+
[/分析|诊断|排查/u, '分析'],
|
|
1662
|
+
[/修复|修改/u, '修改'],
|
|
1663
|
+
[/安全|权限|鉴权/u, '安全'],
|
|
1664
|
+
[/配置|参数/u, '配置'],
|
|
1665
|
+
[/数据|日志|报表/u, '数据'],
|
|
1666
|
+
[/接口|api/iu, '接口'],
|
|
1667
|
+
[/样式|布局|界面|ui/u, '界面'],
|
|
1668
|
+
[/函数|模块|类|代码/u, '代码'],
|
|
1669
|
+
]
|
|
1670
|
+
|
|
1671
|
+
/**
|
|
1672
|
+
* 领域标签(性质兜底素材):从任务原文认领域词,最多取 2 个拼「文档·变更」。
|
|
1673
|
+
* **不切字**:装不下就少取一个词,再装不下就返回空串(卡片宁可不写括注,也不留半句)。
|
|
1674
|
+
*/
|
|
1675
|
+
function taskThemeOf(taskText, maxLength = CARD_NATURE_MAX) {
|
|
1676
|
+
const text = String(taskText ?? '')
|
|
1677
|
+
if (text === '') return ''
|
|
1678
|
+
const words = []
|
|
1679
|
+
for (const [pattern, word] of TASK_THEME_RULES) {
|
|
1680
|
+
if (words.includes(word)) continue
|
|
1681
|
+
if (pattern.test(text)) words.push(word)
|
|
1682
|
+
if (words.length === 2) break
|
|
1683
|
+
}
|
|
1684
|
+
for (const candidate of [words.join('·'), words[0] ?? '']) {
|
|
1685
|
+
if (candidate !== '' && Array.from(candidate).length <= maxLength) return candidate
|
|
1686
|
+
}
|
|
1687
|
+
return ''
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
/**
|
|
1691
|
+
* 本任务性质(「这卡是在给哪类活做建议」)—— 素材序(全部同源,不新判):
|
|
1692
|
+
* ① verdict.facts 里的中文命中词(tier.deep / tier.cheap / mode.*):「架构·设计」
|
|
1693
|
+
* ② 显式拆解出的单元:「①②③④ 共 5 项」(数量本身来自 analyzeTask)
|
|
1694
|
+
* ③ 任务原文首句(整句装得下才用):「看看这个函数」
|
|
1695
|
+
* ④ 领域词改写:「文档·变更」(首句太长时,例如「请把项目里那份特别长的变更说明第
|
|
1696
|
+
* 3 节到第 9 节重新组织一下顺序…」)
|
|
1697
|
+
* 【截断病修复 2026-11-26】旧实现 ③ 是 clipSegment(首句) —— 卡面上出现
|
|
1698
|
+
* 「(把项目里那份特别长的变更说…)」这种**半句话 + 省略号**。现改为「整句装得下就用,
|
|
1699
|
+
* 装不下就换说法(领域词)」:超预算靠改写,不靠截断。
|
|
1700
|
+
* @returns {string} 必定 ≤ maxLength 且无省略号;任务为空/说不清时为空串
|
|
1701
|
+
*/
|
|
1702
|
+
function taskNatureOf(analysis, verdict, taskText, maxLength = CARD_NATURE_MAX) {
|
|
1703
|
+
const len = (text) => Array.from(text).length
|
|
1704
|
+
const FIT = (text) => text !== '' && len(text) <= maxLength
|
|
1705
|
+
const FACT_CODES = ['tier.deep', 'tier.cheap', 'mode.parallel.signal', 'mode.serial.explicit', 'mode.serial.dependent']
|
|
1706
|
+
for (const code of FACT_CODES) {
|
|
1707
|
+
const words = cjkWordsOf(factOf(verdict, code), 2)
|
|
1708
|
+
if (words.length === 0) continue
|
|
1709
|
+
const both = words.join('·')
|
|
1710
|
+
if (FIT(both)) return both
|
|
1711
|
+
if (FIT(words[0])) return words[0]
|
|
1712
|
+
}
|
|
1713
|
+
const explicit = analysis.units.filter((unit) => !unit.synthetic)
|
|
1714
|
+
if (explicit.length >= 2) {
|
|
1715
|
+
const marks = explicit.slice(0, UNIT_SUMMARY_SHOWN).map((_, index) => unitMarkAt(index)).join('')
|
|
1716
|
+
const withMarks = marks + ' 共 ' + explicit.length + ' 项'
|
|
1717
|
+
return FIT(withMarks) ? withMarks : '共 ' + explicit.length + ' 项'
|
|
1718
|
+
}
|
|
1719
|
+
const head = taskHeadOf(taskText)
|
|
1720
|
+
if (FIT(head)) return head
|
|
1721
|
+
return taskThemeOf(taskText, maxLength)
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
/**
|
|
1725
|
+
* 单元标号串(有界):前 shown 个 +「…共 N 个」。
|
|
1726
|
+
* A-22 纪律:冲突单元的个数由输入长度决定(同一文件被上百个单元写时标号串上千字符,
|
|
1727
|
+
* 会把选项描述撑爆)—— 与 concurrency.mjs labelListText 同一上界语义,这里只负责渲染。
|
|
1728
|
+
*/
|
|
1729
|
+
function markListOf(indexes, shown = 6) {
|
|
1730
|
+
const list = Array.isArray(indexes) ? indexes : []
|
|
1731
|
+
const marks = list.slice(0, shown).map((index) => unitMarkAt(index)).join('')
|
|
1732
|
+
return list.length > shown ? marks + '…共 ' + list.length + ' 个' : marks
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
/**
|
|
1736
|
+
* 单元动作词表:从单元原文里认出「要干什么」,用来拼**短标题**(改写,不切字)。
|
|
1737
|
+
* 有序匹配(先命中先用)——「修改」排在「审查」前,因为「修改…的第 N 节」这类
|
|
1738
|
+
* 文本里两种动词都可能出现,取第一动作更贴近单元主旨。
|
|
1739
|
+
*/
|
|
1740
|
+
const UNIT_ACTION_RULES = [
|
|
1741
|
+
[/修改|修复|修正|改成|改为|改掉/, '修复'],
|
|
1742
|
+
[/新增|添加|增加|新建|补充/, '新增'],
|
|
1743
|
+
[/删除|移除|删掉/, '删除'],
|
|
1744
|
+
[/重命名|改名|换个名字/, '重命名'],
|
|
1745
|
+
[/重构|重写/, '重构'],
|
|
1746
|
+
[/审查|检查|核对|评审|复核/, '审查'],
|
|
1747
|
+
[/审计/, '审计'],
|
|
1748
|
+
[/分析|诊断|排查|定位/, '分析'],
|
|
1749
|
+
[/测试|用例|覆盖率/, '测试'],
|
|
1750
|
+
[/文档|说明|注释|readme/i, '文档'],
|
|
1751
|
+
[/设计|方案/, '设计'],
|
|
1752
|
+
[/调研|评估|对比/, '调研'],
|
|
1753
|
+
[/迁移|升级|切换/, '迁移'],
|
|
1754
|
+
[/合并|收敛/, '合并'],
|
|
1755
|
+
[/配置|参数/, '配置'],
|
|
1756
|
+
[/优化|加速|提速/, '优化'],
|
|
1757
|
+
]
|
|
1758
|
+
|
|
1759
|
+
/** 标题显示宽度:「字」按视觉列数算(CJK 2 列 / ASCII 1 列),见 UNIT_TITLE_WIDTH_MAX。 */
|
|
1760
|
+
function titleWidthOf(text) {
|
|
1761
|
+
let width = 0
|
|
1762
|
+
for (const ch of String(text ?? '')) width += ch.codePointAt(0) > 0xff ? 2 : 1
|
|
1763
|
+
return width
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
/** 单元标题:只给「这是哪一件」,完整原文留在工具返回的推理报告里(报告通道不动)。
|
|
1767
|
+
*
|
|
1768
|
+
* 【截断病修复 2026-11-26】用户给的原则:「用户可见文案禁止字符级截断,超预算必须源头
|
|
1769
|
+
* 改写文案」。所以这里**绝不切字**,而是按「改写候选」降级挑选:
|
|
1770
|
+
* ① 原文「:」前的标题部分(「结构审计A:检查 a 模块」→「结构审计A」);
|
|
1771
|
+
* ② 「文件名 + 动作」(「修改 lib/shared.mjs 里的 parse 函数」→「shared.mjs 修复」——
|
|
1772
|
+
* 用户点名要的形态:从原文提炼,去掉目录与修饰语);
|
|
1773
|
+
* ③ 去掉扩展名的文件名 + 动作(「formatter.mjs 修复」超宽时退「formatter 修复」);
|
|
1774
|
+
* ④ 原文本身就够短(≤10 字宽)→ 整句照用;
|
|
1775
|
+
* ⑤ 只剩动作(「修复」);
|
|
1776
|
+
* ⑥ 兜底「第 N 件」(保证一定有短标题,且不同单元可区分)。
|
|
1777
|
+
* 全部是「换一种说法」,没有一处 slice/截断。
|
|
1778
|
+
*/
|
|
1779
|
+
function unitTitleOf(unit, index = 0, used = undefined) {
|
|
1780
|
+
const text = oneLine(unit?.text).replace(/\s+$/u, '')
|
|
1781
|
+
const fallback = '第 ' + (index + 1) + ' 件'
|
|
1782
|
+
if (text === '') return fallback
|
|
1783
|
+
// 「装得下」+「与前面已列的标题不重复」(同一文件的 parse / format 两个单元都在
|
|
1784
|
+
// lib/shared.mjs 上时,只靠「文件名 + 动作」会拼出两个一模一样的标题)。
|
|
1785
|
+
const fits = (candidate) => candidate !== ''
|
|
1786
|
+
&& titleWidthOf(candidate) <= UNIT_TITLE_WIDTH_MAX
|
|
1787
|
+
&& !(used instanceof Set && used.has(candidate))
|
|
1788
|
+
const candidates = []
|
|
1789
|
+
const colonAt = text.search(/[::]/u)
|
|
1790
|
+
const heading = colonAt > 0 ? text.slice(0, colonAt).trim() : ''
|
|
1791
|
+
if (heading !== '') candidates.push(heading)
|
|
1792
|
+
const action = UNIT_ACTION_RULES.find(([pattern]) => pattern.test(text))?.[1] ?? ''
|
|
1793
|
+
const file = Array.isArray(unit?.files) && unit.files.length > 0 ? String(unit.files[0]) : ''
|
|
1794
|
+
const base = file.split(/[\\/]/u).pop() ?? ''
|
|
1795
|
+
if (action !== '' && base !== '') {
|
|
1796
|
+
candidates.push(base + ' ' + action)
|
|
1797
|
+
// 「…里的 parse 函数」→ 用宾语名区分同文件的两个单元(「parse 修复」/「format 修复」)。
|
|
1798
|
+
const object = text.match(/(?:里的|中的|的)\s*([A-Za-z_$][\w$.]*)/u)?.[1] ?? ''
|
|
1799
|
+
if (object !== '') candidates.push(object + ' ' + action)
|
|
1800
|
+
const stem = base.replace(/\.[A-Za-z0-9]+$/u, '')
|
|
1801
|
+
if (stem !== base && stem !== '') candidates.push(stem + ' ' + action)
|
|
1802
|
+
}
|
|
1803
|
+
candidates.push(text)
|
|
1804
|
+
if (action !== '') candidates.push(action)
|
|
1805
|
+
return candidates.find(fits) ?? fallback
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1808
|
+
/**
|
|
1809
|
+
* 拆解摘要:「①shared.mjs 修复 ②formatter.mjs 修复」——**只列标题,不列全文**。
|
|
1810
|
+
*
|
|
1811
|
+
* 【截断病修复 2026-11-26】用户口径(有意推翻 [65] 当时的「单元文本零截断」断言):
|
|
1812
|
+
* 那张卡上单元全文是「修改 lib/alpha/parser.mjs 里的 parseExpression 函数」这种长句,
|
|
1813
|
+
* 4 个连排 100+ 字,GUI 里读不成样;卡片需要的是**标题**(「哪几件事」),全文属于
|
|
1814
|
+
* 给模型看的报告通道(buildReasoningReport 已逐字列出,一字未动)。
|
|
1815
|
+
* 形态:UNIT_SUMMARY_SHOWN(4)个标题全列,超过才以「…共 N 个」收口;没有显式单元时为空串。
|
|
1816
|
+
*/
|
|
1817
|
+
function unitDigestOf(units) {
|
|
1818
|
+
const list = Array.isArray(units) ? units : []
|
|
1819
|
+
if (list.length === 0) return ''
|
|
1820
|
+
// 逐个生成并去重:标题重复时退到更具体的说法(宾语名 / 序号)。
|
|
1821
|
+
const used = new Set()
|
|
1822
|
+
const shown = list
|
|
1823
|
+
.slice(0, UNIT_SUMMARY_SHOWN)
|
|
1824
|
+
.map((unit, index) => {
|
|
1825
|
+
const title = unitTitleOf(unit, index, used)
|
|
1826
|
+
used.add(title)
|
|
1827
|
+
return unitMarkAt(index) + title
|
|
1828
|
+
})
|
|
1829
|
+
.join(' ')
|
|
1830
|
+
return list.length > UNIT_SUMMARY_SHOWN ? shown + ' …共 ' + list.length + ' 个' : shown
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
/**
|
|
1834
|
+
* 一句理由(评定区「理由:」段):为什么给这个建议。
|
|
1835
|
+
* 素材 = analysis.conflicts(同一次结构分析)或 verdict.facts 命中词/数字 —— 不新判。
|
|
1836
|
+
*
|
|
1837
|
+
* 【人话化变更 2026-11-26】旧文案「命中『并发、同时』并发信号 / 规模 21 项超单批上限 8 /
|
|
1838
|
+
* 命中『然后』依赖信号」是评分器内部说法。现在改成直接把用户原话与结论说出来:
|
|
1839
|
+
* 「你说过『并发、同时』,可以同时做」「21 件事,一次最多同时做 8 件」。
|
|
1840
|
+
* facts 的 code / payload 零改动,取材路径与原来完全相同。
|
|
1841
|
+
*/
|
|
1842
|
+
/**
|
|
1843
|
+
* 整句候选里挑一句装得下的(**绝不切字符**):先取第一个 ≤ max 的候选,
|
|
1844
|
+
* 都装不下就取最短的那句原样返回(宁可长一格,也不留「半句话 + …」)。
|
|
1845
|
+
* 与 concurrency.mjs 的 sentBriefOf 同一纪律:超预算靠**换说法**,不靠截断。
|
|
1846
|
+
*/
|
|
1847
|
+
function pickWholeOf(candidates, max = CARD_REASON_MAX) {
|
|
1848
|
+
const list = (Array.isArray(candidates) ? candidates : [candidates])
|
|
1849
|
+
.filter((text) => typeof text === 'string' && text !== '')
|
|
1850
|
+
if (list.length === 0) return ''
|
|
1851
|
+
const len = (text) => Array.from(text).length
|
|
1852
|
+
return list.find((text) => len(text) <= max) ?? list.reduce((best, text) => (len(text) < len(best) ? text : best))
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
function taskReasonOf(analysis, verdict) {
|
|
1856
|
+
// 【截断病修复 2026-11-26】每个分支都给「带标号/带引用」与「不带」两种说法:
|
|
1857
|
+
// 带标号的装不下时退**改写过**的那句(不是把前半句砍掉加省略号)。
|
|
1858
|
+
if (analysis.conflicts.length > 0) {
|
|
1859
|
+
const conflict = analysis.conflicts[0]
|
|
1860
|
+
return pickWholeOf([
|
|
1861
|
+
markListOf(conflict.unitIndexes) + ' 改的是同一个文件,分不开',
|
|
1862
|
+
'这几件事改的是同一个文件,分不开',
|
|
1863
|
+
])
|
|
1864
|
+
}
|
|
1865
|
+
if (verdict?.mode === 'parallel') {
|
|
1866
|
+
const signalWords = cjkWordsOf(factOf(verdict, 'mode.parallel.signal'), 2)
|
|
1867
|
+
if (signalWords.length > 0) {
|
|
1868
|
+
return pickWholeOf([
|
|
1869
|
+
'你说过“' + signalWords.join('、') + '”,可以同时做',
|
|
1870
|
+
'你说过要同时做,可以分开派',
|
|
1871
|
+
])
|
|
1872
|
+
}
|
|
1873
|
+
const overCap = factOf(verdict, 'concurrency.scaleOverCap')
|
|
1874
|
+
if (overCap !== undefined && typeof overCap.subtasks === 'number') {
|
|
1875
|
+
// W3-F-02 续:说「一次最多同时做几件」必须用**最终 verdict 的路数**,不能拿
|
|
1876
|
+
// 契约常量 cap(MAX_CONCURRENCY=8)——「最多 3 路」任务会变成
|
|
1877
|
+
// 「10 件事,一次最多同时做 8 件」,与卡片其余 3 路数字自相矛盾。
|
|
1878
|
+
const concurrentNow = typeof verdict?.concurrency === 'number' && Number.isFinite(verdict.concurrency)
|
|
1879
|
+
? verdict.concurrency
|
|
1880
|
+
: overCap.cap
|
|
1881
|
+
return pickWholeOf([
|
|
1882
|
+
overCap.subtasks + ' 件事,一次最多同时做 ' + String(concurrentNow ?? '') + ' 件',
|
|
1883
|
+
overCap.subtasks + ' 件事得分批做',
|
|
1884
|
+
])
|
|
1885
|
+
}
|
|
1886
|
+
const scale = factOf(verdict, 'mode.parallel.scale')
|
|
1887
|
+
if (scale !== undefined && typeof scale.subtasks === 'number') {
|
|
1888
|
+
return scale.subtasks + ' 件事能分开做'
|
|
1889
|
+
}
|
|
1890
|
+
return '几件事互不干扰,能同时做'
|
|
1891
|
+
}
|
|
1892
|
+
if (verdict?.mode === 'serial') {
|
|
1893
|
+
const explicitWords = cjkWordsOf(factOf(verdict, 'mode.serial.explicit'), 2)
|
|
1894
|
+
if (explicitWords.length > 0) {
|
|
1895
|
+
return pickWholeOf([
|
|
1896
|
+
'你说过“' + explicitWords.join('、') + '”,得按顺序',
|
|
1897
|
+
'你说过要按顺序做',
|
|
1898
|
+
])
|
|
1899
|
+
}
|
|
1900
|
+
const dependentWords = cjkWordsOf(factOf(verdict, 'mode.serial.dependent'), 2)
|
|
1901
|
+
if (dependentWords.length > 0) {
|
|
1902
|
+
return pickWholeOf([
|
|
1903
|
+
'“' + dependentWords.join('、') + '”说明后一步要等前一步',
|
|
1904
|
+
'后一步要等前一步做完',
|
|
1905
|
+
])
|
|
1906
|
+
}
|
|
1907
|
+
return verdict?.efficiency === 'low' ? '同时开太多,协调就把好处吃掉了' : '后一步要用前一步的结果'
|
|
1908
|
+
}
|
|
1909
|
+
return ''
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
/**
|
|
1913
|
+
* 劝退项的**规模理由**(B2-4 ③):从任务规模信号生成,说清「这次为什么不划算」。
|
|
1914
|
+
* · 单主体(1 个单元)→「单文件直改量小」/「无独立单元可拆」(立项点名的两条);
|
|
1915
|
+
* · 多单元 →「N 个单元需 K 批,全进主上下文噪声大」(阅读噪音论点,
|
|
1916
|
+
* 数字取自 analyzeTask.unitCount 与 verdict.batches,不另算)。
|
|
1917
|
+
*/
|
|
1918
|
+
function scaleNoteOf(analysis, verdict) {
|
|
1919
|
+
const units = analysis.unitCount
|
|
1920
|
+
const files = analysis.fileCount
|
|
1921
|
+
// 【人话化变更 2026-11-26】「无独立单元可拆 / 单文件直改量小」改为普通用户说法。
|
|
1922
|
+
if (files === 1 && units <= 1) return '就一个文件,改起来快'
|
|
1923
|
+
if (units <= 1) return analysis.scale.count >= 2 ? '有 ' + analysis.scale.count + ' 项活,但拆不出能分开的' : '拆不出能分开的活'
|
|
1924
|
+
const batches = Number(verdict?.batches) || 0
|
|
1925
|
+
return batches > 1 ? units + ' 件事要分 ' + batches + ' 批' : units + ' 件事不多'
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1928
|
+
/**
|
|
1929
|
+
* 通道适配句(选项描述的后半句):为什么这条路**在这个任务上**成立。
|
|
1930
|
+
* 只陈述已有结构(冲突 / 文件路径 / 依赖),不写「操作型并发更稳」这类功能介绍。
|
|
1931
|
+
*/
|
|
1932
|
+
function channelNoteOf(analysis, verdict) {
|
|
1933
|
+
// 【人话化变更 2026-11-26】选项描述里的通道适配句同样去术语:
|
|
1934
|
+
// 「同写一文件 / 每路先补 writeScopes / 执行主体」都换成大白话。
|
|
1935
|
+
// 【截断病修复 2026-11-26】病态冲突(60 个单元)下这句会撑到 32 字 → 选项行被宿主
|
|
1936
|
+
// 单行截断(截断病的原始病灶)。这里给它一个通道预算(30 - 「一件件做:」5 字 = 25),
|
|
1937
|
+
// 装不下就**改写说法**(先丢尾巴的结论句,再丢标号),不切字。
|
|
1938
|
+
if (analysis.conflicts.length > 0) {
|
|
1939
|
+
const marks = markListOf(analysis.conflicts[0].unitIndexes)
|
|
1940
|
+
return pickWholeOf([
|
|
1941
|
+
marks + ' 都改同一个文件,只能按顺序',
|
|
1942
|
+
marks + ' 都改同一个文件',
|
|
1943
|
+
'这些事都改同一个文件,只能按顺序',
|
|
1944
|
+
'这些事都改同一个文件',
|
|
1945
|
+
], CARD_CHANNEL_MAX)
|
|
1946
|
+
}
|
|
1947
|
+
if (verdict?.mode === 'parallel') {
|
|
1948
|
+
const withFiles = analysis.units.filter((unit) => unit.files.length > 0).length
|
|
1949
|
+
return withFiles > 0 ? '各改各的文件,不重叠' : '文件没说清,派发前我补上'
|
|
1950
|
+
}
|
|
1951
|
+
if (verdict?.mode === 'serial') {
|
|
1952
|
+
// unitCount 才是「有几个可独立派发的单元」(units.length 在数量词任务里恒为 1)。
|
|
1953
|
+
if (analysis.unitCount <= 1) return '就 1 条线'
|
|
1954
|
+
return '后一步要等前一步'
|
|
1955
|
+
}
|
|
1956
|
+
return '就 1 件事,拆不开'
|
|
1957
|
+
}
|
|
1958
|
+
|
|
1959
|
+
/**
|
|
1960
|
+
* 确认卡的「本任务文案素材包」——同一次分析产物,渲染层只读不算。
|
|
1961
|
+
* 任务原文为空时调用方不要构造(narrative = null → 全部退回旧文案)。
|
|
1962
|
+
*
|
|
1963
|
+
* @param {string} task 任务原文(已 trim)
|
|
1964
|
+
* @param {object} verdict effectiveVerdict(与卡片 mode 同源)
|
|
1965
|
+
* @returns {{
|
|
1966
|
+
* unitCount: number, fileCount: number, explicitCount: number,
|
|
1967
|
+
* conflicts: Array<object>, nature: string, reason: string,
|
|
1968
|
+
* scaleNote: string, channel: string, unitDigest: string,
|
|
1969
|
+
* }}
|
|
1970
|
+
*/
|
|
1971
|
+
function cardNarrativeOf(task, verdict, analysisInput) {
|
|
1972
|
+
// W3-F-22:优先复用调用方已算好的 analysis(同一次分析),不再重复解析。
|
|
1973
|
+
const analysis = analysisInput ?? analyzeTask(task)
|
|
1974
|
+
const explicit = analysis.units.filter((unit) => !unit.synthetic)
|
|
1975
|
+
return {
|
|
1976
|
+
unitCount: analysis.unitCount,
|
|
1977
|
+
fileCount: analysis.fileCount,
|
|
1978
|
+
explicitCount: explicit.length,
|
|
1979
|
+
conflicts: analysis.conflicts,
|
|
1980
|
+
nature: taskNatureOf(analysis, verdict, task),
|
|
1981
|
+
reason: taskReasonOf(analysis, verdict),
|
|
1982
|
+
scaleNote: scaleNoteOf(analysis, verdict),
|
|
1983
|
+
channel: channelNoteOf(analysis, verdict),
|
|
1984
|
+
unitDigest: unitDigestOf(explicit),
|
|
1985
|
+
}
|
|
1986
|
+
}
|
|
1987
|
+
|
|
1988
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1989
|
+
// 卡片人话化(2026-11-26 用户原话:你这玩意说那么多,谁看得懂。
|
|
1990
|
+
// 如果不是专业人士的话根本不知道你在说什么)
|
|
1991
|
+
// ═══════════════════════════════════════════════════════════════
|
|
1992
|
+
//
|
|
1993
|
+
// 边界(与 [64]/[34] 的既有约束一致):
|
|
1994
|
+
// · **只改展示层**:这些函数只把 verdict / dimensions / facts 的既有取值换个说法;
|
|
1995
|
+
// scoreTask / computeReasoningPlan / verdict.facts 的 code 与 payload 一字未动。
|
|
1996
|
+
// · 报告(buildReasoningReport / report.mjs)仍是给模型看的正文,措辞不变 ——
|
|
1997
|
+
// 卡片是给「拍板的普通用户」看的。
|
|
1998
|
+
// · 每段仍受 30 字预算(CARD_SEGMENT_MAX)约束,拆行优先保完整。
|
|
1999
|
+
|
|
2000
|
+
/** 模型档 → 人话(用户只知道「强 / 快 / 普通」三个概念,不需要知道档位名)。 */
|
|
2001
|
+
function plainTierOf(tier) {
|
|
2002
|
+
if (tier === 'deep') return '强模型(更仔细、更慢、更贵)'
|
|
2003
|
+
if (tier === 'cheap') return '快速模型(便宜、快,适合简单活)'
|
|
2004
|
+
return '普通模型(够用,不浪费)'
|
|
2005
|
+
}
|
|
2006
|
+
|
|
2007
|
+
/**
|
|
2008
|
+
* 四维评分 → 人话短句。
|
|
2009
|
+
* 映射(只换说法,不换阈值):
|
|
2010
|
+
* 散乱高 → 互不干扰(各干各的) / 依赖高 → 有先后顺序 / 推理高 → 每件都要认真分析
|
|
2011
|
+
* 操作高 → 都是照着改的活
|
|
2012
|
+
*/
|
|
2013
|
+
function plainDimPhrases(dimensions) {
|
|
2014
|
+
const levelOf = (key) => dimensions?.[key]?.level
|
|
2015
|
+
const pick = (key, high, medium, low) => {
|
|
2016
|
+
const level = levelOf(key)
|
|
2017
|
+
if (level === 'high') return high
|
|
2018
|
+
if (level === 'medium') return medium
|
|
2019
|
+
if (level === 'low') return low
|
|
2020
|
+
return ''
|
|
2021
|
+
}
|
|
2022
|
+
return {
|
|
2023
|
+
scatter: pick('parallel', '互不干扰', '大体互不干扰', '有点牵连'),
|
|
2024
|
+
dependent: pick('dependent', '有先后顺序', '有点先后', '顺序不严格'),
|
|
2025
|
+
deep: pick('deep', '每件都要认真分析', '也要动点脑', ''),
|
|
2026
|
+
mechanical: pick('mechanical', '都是照着改的活', '照着改就行', ''),
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
|
|
2030
|
+
/**
|
|
2031
|
+
* 结论句主干(「结论:」之后的半句):
|
|
2032
|
+
* parallel → 互不干扰,每件都要认真分析
|
|
2033
|
+
* serial → 两件事改同一个文件,只能排队 / 有先后顺序,…
|
|
2034
|
+
* single → 一体的活,拆不出第二件
|
|
2035
|
+
*/
|
|
2036
|
+
function plainConclusionBodyOf(verdict, dimsPlain, narrative) {
|
|
2037
|
+
if (verdict?.mode === 'parallel') {
|
|
2038
|
+
const parts = [dimsPlain.scatter, dimsPlain.deep || dimsPlain.mechanical]
|
|
2039
|
+
.filter((part) => part !== '')
|
|
2040
|
+
// 全 none 时也不能说「四维低」这种黑话:直接说「互不干扰,可以分开做」。
|
|
2041
|
+
return parts.length > 0 ? parts.join(',') : '互不干扰,可以分开做'
|
|
2042
|
+
}
|
|
2043
|
+
if (verdict?.mode === 'serial') {
|
|
2044
|
+
const conflicts = Array.isArray(narrative?.conflicts) ? narrative.conflicts : []
|
|
2045
|
+
if (conflicts.length > 0) return '两件事改同一个文件,只能排队'
|
|
2046
|
+
const parts = [dimsPlain.dependent, dimsPlain.deep].filter((part) => part !== '')
|
|
2047
|
+
return parts.length > 0 ? parts.join(',') : '后一步要用前一步的结果'
|
|
2048
|
+
}
|
|
2049
|
+
return '一体的活,拆不出第二件'
|
|
2050
|
+
}
|
|
2051
|
+
|
|
2052
|
+
/**
|
|
2053
|
+
* 效率 → 人话(把「加速比约 N.N×」翻成「约快 N 倍」)。
|
|
2054
|
+
* 口径仍是 verdict.efficiency / brief.efficiencyGain,不重算。
|
|
2055
|
+
*/
|
|
2056
|
+
function plainEfficiencyOf(verdict, brief) {
|
|
2057
|
+
const gain = brief !== null && typeof brief?.efficiencyGain === 'number' && Number.isFinite(brief.efficiencyGain)
|
|
2058
|
+
? brief.efficiencyGain
|
|
2059
|
+
: null
|
|
2060
|
+
if (verdict?.mode === 'parallel') {
|
|
2061
|
+
// W3-F-13:显式并发(trustMode)不降级时如实标注「按你的要求仍并行」,
|
|
2062
|
+
// 不让「建议 4 路」与「同时做不划算」同屏自相矛盾。
|
|
2063
|
+
if (verdict.efficiency === 'low') return '同时做不划算(按你的要求仍并行)'
|
|
2064
|
+
if (gain !== null) {
|
|
2065
|
+
const times = Number.isInteger(gain) ? String(gain) : gain.toFixed(1)
|
|
2066
|
+
return `${verdict.concurrency} 路同跑,约快 ${times} 倍`
|
|
2067
|
+
}
|
|
2068
|
+
return '同时做能省时间,但要看协调'
|
|
2069
|
+
}
|
|
2070
|
+
if (verdict?.efficiency === 'low') return '同时做不划算'
|
|
2071
|
+
if (verdict?.efficiency === 'high') return '同时做能快不少'
|
|
2072
|
+
if (verdict?.efficiency === 'moderate') return '同时做能快一些'
|
|
2073
|
+
return '分不分都一样'
|
|
2074
|
+
}
|
|
2075
|
+
|
|
2076
|
+
/**
|
|
2077
|
+
* W3-F-09(P2):确认卡「选项构造 + 选项语义表」的唯一构造点。
|
|
2078
|
+
*
|
|
2079
|
+
* 从 askUserToConfirmConcurrency 里原样抽出(行为保持重构):
|
|
2080
|
+
* · 输入全部是已决定好的展示值(模式 / 路数 / 偏好 / 文案素材),本函数不重算判定;
|
|
2081
|
+
* · 输出 options(顺序 = 用户看到的顺序)与 optionSemanticsByLabel(label → 是否同意并发 + 授权路数)。
|
|
2082
|
+
* 抽出理由:原主函数 557 行,选项语义表与文案分支混在一起,是 F-01/F-02 这类数字问题的藏身处。
|
|
2083
|
+
*/
|
|
2084
|
+
function buildConcurrencyOptionTable(input) {
|
|
2085
|
+
const {
|
|
2086
|
+
verdict, dimensions, recLabel, recConcurrency, preferredUsable,
|
|
2087
|
+
range0, recSourceNote, narrative,
|
|
2088
|
+
} = input
|
|
2089
|
+
const optionSemanticsByLabel = new Map()
|
|
2090
|
+
const options = []
|
|
2091
|
+
const addOption = (label, description, approvesConcurrency = false, concurrency = undefined) => {
|
|
2092
|
+
options.push({ label, description })
|
|
2093
|
+
const approves = approvesConcurrency === true
|
|
2094
|
+
const limit = approves
|
|
2095
|
+
? (typeof concurrency === 'number' && Number.isFinite(concurrency) && concurrency >= 1
|
|
2096
|
+
? Math.floor(concurrency)
|
|
2097
|
+
: Infinity)
|
|
2098
|
+
: 0
|
|
2099
|
+
optionSemanticsByLabel.set(label, { approves, concurrency: limit })
|
|
2100
|
+
}
|
|
2101
|
+
|
|
2102
|
+
// v10 三档(自有设计②):parallel 卡在**偏好生效**时把「按建议执行」细化为
|
|
2103
|
+
// 下限/中位/上限三档(推荐档 = 偏好值,label 标(推荐)与来源;其余两档 delta>1 补 ±k)。
|
|
2104
|
+
// 无偏好时不展开:integration §22.1/§22b.13 的选项文案/顺序冻结表按 3 项锁定,
|
|
2105
|
+
// 默认卡保持逐字旧形态;三档是偏好场景的增强,不是默认行为的改写。
|
|
2106
|
+
// 三个分支各自登记推荐项(label 相同、description 按分支语义),避免重复 push。
|
|
2107
|
+
// ── B2-4 ②③ + 2026-11-26 截断修复:完整拆解进 detail,选项只留通道短句 ──
|
|
2108
|
+
//
|
|
2109
|
+
// 素材全部来自 narrative(同一份分析):
|
|
2110
|
+
// · 显式单元 → 完整拆解写进 detail 的「拆解:①…②…③…」行(见下方 segments);
|
|
2111
|
+
// · 选项描述只留**通道适配短句**(≤30 字):并行「N 个单元并行摊派;{通道}」、
|
|
2112
|
+
// 串行「单路按序派发:{通道}」—— 不再塞整段拆解(那会被 GUI 单行省略号截断);
|
|
2113
|
+
// · 没有可引用的单元(规模来自数量词)→ 同上「N 个单元并行摊派」+ 通道句(保底不空);
|
|
2114
|
+
// · 任务为空(narrative = null)→ **逐字**沿用旧的通道通用语义(冻结表兼容)。
|
|
2115
|
+
// 劝退项(不派发 / 不改并发)额外带**规模理由**(scaleNote,B2-4 ③):
|
|
2116
|
+
// 单主体 → 「单文件直改量小」/「无独立单元可拆」;多单元 → 「N 个单元需 K 批,噪声大」。
|
|
2117
|
+
// 选项描述**只留通道适配短句**(完整拆解已在 detail「拆解:」行):
|
|
2118
|
+
// 旧实现把「本任务拆解:①…②…③…;通道句」整段塞进选项 → GUI 单行装不下 → 省略号截断
|
|
2119
|
+
// (用户两次截图投诉)。现在选项恒为 ≤30 字短句,完整信息一字不少地在 detail 里。
|
|
2120
|
+
// 【人话化变更 2026-11-26】选项描述同样去术语:「N 个单元并行摊派 / 单路按序派发 /
|
|
2121
|
+
// 主会话直接做 / 全进主上下文噪声大」全部换成普通用户说法(≤30 字,GUI 单行装得下)。
|
|
2122
|
+
const parallelRecDescription = narrative === null
|
|
2123
|
+
? '按上面的建议执行'
|
|
2124
|
+
: `${narrative.unitCount} 件事同时做;${narrative.channel}`
|
|
2125
|
+
// 三档描述的短注(偏好场景):点出本任务有几件事,不重复整段拆解。
|
|
2126
|
+
const tierTaskNote = narrative === null ? '' : `本任务 ${narrative.unitCount} 件事`
|
|
2127
|
+
const serialRecDescription = narrative === null
|
|
2128
|
+
? '按上面的建议执行'
|
|
2129
|
+
: `一件件做:${narrative.channel}`
|
|
2130
|
+
const serialFallbackDescription = narrative === null
|
|
2131
|
+
? '一件一件做,不分批'
|
|
2132
|
+
: `${narrative.unitCount} 件事一件件做,慢但最稳`
|
|
2133
|
+
const noneDescription = verdict.mode === 'serial'
|
|
2134
|
+
? (narrative === null
|
|
2135
|
+
? '这件事有先后顺序,我自己按顺序做就行'
|
|
2136
|
+
: `${narrative.unitCount > 1 ? `${narrative.unitCount} 件事要按顺序做` : '就 1 条线'},我自己按顺序做就行`)
|
|
2137
|
+
: (narrative === null
|
|
2138
|
+
? '任务不大时,我自己做更省事'
|
|
2139
|
+
: `${narrative.scaleNote},我自己做更省事`)
|
|
2140
|
+
if (verdict.mode === 'parallel') {
|
|
2141
|
+
addOption(recLabel, parallelRecDescription, true, recConcurrency)
|
|
2142
|
+
// 三档的 label 各自登记语义(路数 = 档位值);推荐档即首项 addOption(语义同源)。
|
|
2143
|
+
if (preferredUsable) {
|
|
2144
|
+
for (const tier of concurrencyTierOptions(range0.min, range0.max, recConcurrency, recSourceNote, tierTaskNote)) {
|
|
2145
|
+
if (tier.label === recLabel) continue
|
|
2146
|
+
// ── W3-F-06(P2):同一路数不得有两个「(推荐)」入口 ──
|
|
2147
|
+
// concurrencyTierOptions 的推荐档模板(「改为并发 N 路(推荐)(按你上次选的)」)
|
|
2148
|
+
// 与 recLabel(「按建议执行:并发 N 路(推荐)」)不同,旧跳过条件永不成立:
|
|
2149
|
+
// 偏好 3 时卡片同时出现两个「3 路(推荐)」,用户容易误选。
|
|
2150
|
+
// 冻结契约(integration §22.1 的 6 选项结构)要求三档都在,因此保留档位、
|
|
2151
|
+
// 只把与推荐档同值的档位改成明确的「同推荐档」表述(唯一(推荐)留给首项)。
|
|
2152
|
+
const sameAsRecommended = tier.concurrency === recConcurrency
|
|
2153
|
+
addOption(
|
|
2154
|
+
sameAsRecommended ? ('改为并发 ' + tier.concurrency + ' 路(同推荐档)') : tier.label,
|
|
2155
|
+
tier.description, true, tier.concurrency,
|
|
2156
|
+
)
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
// 【人话化】「改为串行(按序执行)」→「一件件做,不并发」;
|
|
2160
|
+
// 「不派发,主会话直接做」→「不用助手,我直接做」。
|
|
2161
|
+
addOption('一件件做,不并发', serialFallbackDescription)
|
|
2162
|
+
addOption('不用助手,我直接做', noneDescription)
|
|
2163
|
+
} else if (verdict.mode === 'serial') {
|
|
2164
|
+
// serial 分支的推荐项(= 串行,不构成并发同意 → approves=false)。
|
|
2165
|
+
// v10 重构时按分支登记推荐项:parallel / single 在各自分支内,serial 在这里。
|
|
2166
|
+
addOption(recLabel, serialRecDescription)
|
|
2167
|
+
// 明确写「并发 2 路」= 用户主动要求并发 → 属于同意并发(②),授权路数 N=2
|
|
2168
|
+
addOption('改为并发 2 路', narrative === null
|
|
2169
|
+
? '按最小并发起步,协调成本自己扛'
|
|
2170
|
+
: '不管顺序硬分 2 路,协调成本自己扛', true, 2)
|
|
2171
|
+
addOption('不用助手,我直接做', noneDescription)
|
|
2172
|
+
} else {
|
|
2173
|
+
// L3 修复(A 方案)+ V2 残留#1:single 分支补齐「只派 1 路」表达途径。
|
|
2174
|
+
// 「改为派发子代理」= 由你决定拆几路 → 把路数交给执行方 → 构成并发同意,N=Infinity;
|
|
2175
|
+
// description 明写「可能并发多路」(V2 建议),与「由你决定拆几路」是同一条契约。
|
|
2176
|
+
// 「只派 1 路子代理(不并发)」= 明确只派 1 路、不并发 → 旗标 false(不解锁);
|
|
2177
|
+
// 落闸后本轮只放行第 1 路、第 2 路仍被拦,行为上与「确认 N=1」一致(#1/#2 合并设计)。
|
|
2178
|
+
// v10(自有设计①):推荐项(= 不派发)的 description 写**本任务适配理由**:
|
|
2179
|
+
// 四维都低(可并行/依赖/难度全 none 且规模 < 2)→ 劝退文案(省额度省启动开销);
|
|
2180
|
+
// 否则按 single 细分形态(仅 1 个执行主体 / 兜底)。判据直接读 dimensions:
|
|
2181
|
+
// fact payload 是契约 §2 的冻结键白名单({code,words,subtasks,cap,gain,scatter,deep}),
|
|
2182
|
+
// 劝退标记不能进 payload(test.mjs §13 断言),只能由消费侧现算。
|
|
2183
|
+
const lowFour = dimensions?.parallel?.level === 'none'
|
|
2184
|
+
&& dimensions?.dependent?.level === 'none'
|
|
2185
|
+
&& dimensions?.deep?.level === 'none'
|
|
2186
|
+
&& (dimensions?.scale?.count ?? 0) < 2
|
|
2187
|
+
// single 分支内恒有「拆不出并行单元」这一事实(mode=single 的定义);
|
|
2188
|
+
// concurrency.singleUnit fact 只覆盖「规模=1」的特例,缺数量词的 single 任务
|
|
2189
|
+
// (如「深度分析这个函数」)没有它 —— 细分理由按「仅 1 个执行主体」兜底全部非劝退形态。
|
|
2190
|
+
const singleUnit = !lowFour
|
|
2191
|
+
// B2-4 ③:劝退项的**规模理由**(立项点名的「无独立单元可拆 / 单文件直改量小」),
|
|
2192
|
+
// 由 analyzeTask 的单元数与文件数生成;任务为空时 narrative = null → 逐字旧文案。
|
|
2193
|
+
const scaleNote = narrative === null ? '' : narrative.scaleNote
|
|
2194
|
+
// 【人话化变更 2026-11-26】劝退项同样说人话:「省下子代理额度与启动开销 /
|
|
2195
|
+
// 只有 1 个执行主体」→「我自己做更省事 / 派助手反而更慢」。
|
|
2196
|
+
const recDescription = lowFour
|
|
2197
|
+
? (scaleNote !== ''
|
|
2198
|
+
? `${scaleNote},我自己做更省事`
|
|
2199
|
+
: '拆不出能分开的活,我自己做更省事')
|
|
2200
|
+
: singleUnit
|
|
2201
|
+
? (scaleNote !== ''
|
|
2202
|
+
? `${scaleNote},派助手反而更慢`
|
|
2203
|
+
: '只有一件事,派助手反而更慢')
|
|
2204
|
+
: '按上面的建议执行'
|
|
2205
|
+
addOption(recLabel, recDescription)
|
|
2206
|
+
addOption('改为派发子代理', '仍然派发,可能并发多路,由你决定拆几路', true)
|
|
2207
|
+
addOption('只派 1 路子代理(不并发)', '仍派发,但只派 1 路,不并发', false)
|
|
2208
|
+
addOption('改为串行派发', '派发但按顺序执行')
|
|
2209
|
+
}
|
|
2210
|
+
return { options, optionSemanticsByLabel }
|
|
2211
|
+
}
|
|
2212
|
+
|
|
2213
|
+
/**
|
|
2214
|
+
* 确认卡正文(segments)的唯一构造点(W3-F-09)。
|
|
2215
|
+
*
|
|
2216
|
+
* 纯搬移:从 askUserToConfirmConcurrency 里原样抽出「卡片正文拼装」整段(行为保持不变)。
|
|
2217
|
+
* · 输入全部是**已决定好的展示值**(verdict / 文案素材 narrative / brief / 推荐路数 /
|
|
2218
|
+
* 偏好是否生效),本函数不重算任何判定;
|
|
2219
|
+
* · 输出 segments:首段落是粗体「**评定:…**」(宿主 detail 渲染契约),其余为列表项;
|
|
2220
|
+
* · 抽出理由:原主函数 457 行(审计 W3-F-09),正文拼装含大量排版预算判断
|
|
2221
|
+
* (CARD_SEGMENT_MAX 降级链),与「提问 / 回执」编排混在一起不可读。
|
|
2222
|
+
*
|
|
2223
|
+
* @param {object} input
|
|
2224
|
+
* @param {object} input.verdict 原始评分 verdict(mode / modelTier / concurrencyRange 取值)
|
|
2225
|
+
* @param {object} input.displayVerdict 展示用最终 verdict(偏好生效时按最终路数重算)
|
|
2226
|
+
* @param {object} input.dimensions 四维评分
|
|
2227
|
+
* @param {number} input.recConcurrency 推荐路数(卡片「路数:」段与评定行同源)
|
|
2228
|
+
* @param {string} input.recSourceNote 偏好来源标注(无偏好为空串)
|
|
2229
|
+
* @param {boolean} input.preferredUsable 偏好是否可用(决定评定行写来源还是推导)
|
|
2230
|
+
* @param {object|null} input.brief 同一次分析的推理摘要(null = 任务为空)
|
|
2231
|
+
* @param {object|null} input.narrative 本任务文案素材包(null = 任务为空)
|
|
2232
|
+
* @returns {string[]} detail 段落数组(segments[0] 为粗体评定行)
|
|
2233
|
+
*/
|
|
2234
|
+
function buildConcurrencyCardSegments(input) {
|
|
2235
|
+
const {
|
|
2236
|
+
verdict, displayVerdict, dimensions, recConcurrency, recSourceNote, preferredUsable, brief, narrative,
|
|
2237
|
+
} = input
|
|
2238
|
+
|
|
2239
|
+
// ── 人话化素材(2026-11-26 用户反馈「谁看得懂」)──
|
|
2240
|
+
// 全部是**读值 + 换说法**,不新增判定:mode / concurrency / efficiency /
|
|
2241
|
+
// modelTier / dimensions.level / facts 都还是原来的口径。
|
|
2242
|
+
const plainMode = verdict.mode === 'parallel'
|
|
2243
|
+
? `建议 ${recConcurrency} 路同时做`
|
|
2244
|
+
: verdict.mode === 'serial' ? '一件一件做' : '自己做最快'
|
|
2245
|
+
const tierPlain = plainTierOf(verdict.modelTier)
|
|
2246
|
+
const dimsPlain = plainDimPhrases(dimensions)
|
|
2247
|
+
const efficiencyPlain = plainEfficiencyOf(displayVerdict, brief)
|
|
2248
|
+
|
|
2249
|
+
const segments = []
|
|
2250
|
+
// ── v10 判断先行(自有设计④):四维结论一句话 + 推荐 + 理由,位置 = 正文最前 ──
|
|
2251
|
+
// 宿主卡片 detail 渲染约束:首行必须是粗体「评定:」(integration §22b.14 冻结
|
|
2252
|
+
// assert detail.startsWith('**评定:')),因此「结论」作为**评定行后的第一个列表项**
|
|
2253
|
+
// —— 它仍是正文首个信息项(「- 结论:…」排在模型档/效率/路数之前),且用列表项
|
|
2254
|
+
// 形态让用户在 Markdown 列表里第一眼读到。任务为空(纯合成 verdict 单测)时跳过,
|
|
2255
|
+
// 不输出残句;判定依据与 detail 其余段共用同一次 brief 分析(brief0/brief 同源)。
|
|
2256
|
+
const judgmentFirst = brief !== null
|
|
2257
|
+
if (judgmentFirst) {
|
|
2258
|
+
// ── 结论行:四维评分翻成一句人话(素材仍是同一次 analyzeTask + facts,不新判)──
|
|
2259
|
+
//
|
|
2260
|
+
// 【人话化变更 2026-11-26】旧文案是「结论:散乱低、推理高(架构·设计) → 推荐深度档×4」
|
|
2261
|
+
// —— 评分器内部标签堆叠,用户原话「谁看得懂」。现在读起来是一句普通话:
|
|
2262
|
+
// 结论:8 件事互不干扰,每件都要认真分析(架构·设计)
|
|
2263
|
+
// 拆行优先保完整:规模前缀 / 括注按 30 字预算逐级降级,四维人话永不被裁。
|
|
2264
|
+
const scaleCount = dimensions?.scale?.count ?? 0
|
|
2265
|
+
const plainScale = verdict.mode === 'parallel' && scaleCount > 0 ? `${scaleCount} 件事` : ''
|
|
2266
|
+
const plainBody = plainConclusionBodyOf(verdict, dimsPlain, narrative)
|
|
2267
|
+
const nature = narrative === null ? '' : narrative.nature
|
|
2268
|
+
const buildConclusion = (scale, note) =>
|
|
2269
|
+
`结论:${scale}${plainBody}${note === '' ? '' : `(${note})`}`
|
|
2270
|
+
let conclusion = buildConclusion(plainScale, nature)
|
|
2271
|
+
let headlineNature = ''
|
|
2272
|
+
if (narrative !== null && Array.from(conclusion).length > CARD_SEGMENT_MAX) {
|
|
2273
|
+
conclusion = buildConclusion('', nature)
|
|
2274
|
+
}
|
|
2275
|
+
if (narrative !== null && Array.from(conclusion).length > CARD_SEGMENT_MAX) {
|
|
2276
|
+
const room = CARD_SEGMENT_MAX - Array.from(buildConclusion('', '')).length - 2
|
|
2277
|
+
// 【截断病修复 2026-11-26】括注装不下时不再 clipSegment(nature, room)
|
|
2278
|
+
// —— 卡面「(①②③④ 共 5…)」这种半句就是这么来的。整块装不下就整块丢
|
|
2279
|
+
// (性质是装饰,主句才是结论),退到评定行尾;评定行也放不下就不写。
|
|
2280
|
+
if (room >= 3 && Array.from(nature).length <= room) {
|
|
2281
|
+
conclusion = buildConclusion('', nature)
|
|
2282
|
+
} else {
|
|
2283
|
+
conclusion = buildConclusion('', '')
|
|
2284
|
+
headlineNature = nature
|
|
2285
|
+
}
|
|
2286
|
+
}
|
|
2287
|
+
// R-01:偏好生效时只标来源,不再展示默认推导(推导基于 verdict.concurrency
|
|
2288
|
+
// 的默认口径,会与已改为推荐 3 的评定路数混淆);无偏好时亮出人话推导
|
|
2289
|
+
// (「8 件事,最多 4 路」),数字仍可追溯。
|
|
2290
|
+
const derivationPart = verdict.mode === 'parallel'
|
|
2291
|
+
? (preferredUsable ? recSourceNote : `(${derivationBrief(brief.derivation)})`)
|
|
2292
|
+
: ''
|
|
2293
|
+
// 判定区第一行:模式 + 推导,剩余预算给「|任务性质」。
|
|
2294
|
+
// 「|」是排版分隔符不是标签 → 不新增「XX:」段(integration §22b.15 每项必须有「:」)。
|
|
2295
|
+
const headlineBase = `评定:${plainMode}${derivationPart}`
|
|
2296
|
+
const headlineRoom = CARD_SEGMENT_MAX - Array.from(headlineBase).length - 1
|
|
2297
|
+
// 评定行尾的「|性质」同样**整块装得下才写**(不切半句)。
|
|
2298
|
+
const headlineTail = headlineNature === '' || Array.from(headlineNature).length + 1 > headlineRoom
|
|
2299
|
+
? ''
|
|
2300
|
+
: '|' + headlineNature
|
|
2301
|
+
segments.push(`**${headlineBase}${headlineTail}**`)
|
|
2302
|
+
segments.push(conclusion)
|
|
2303
|
+
// ── B2-4 ①(续):一句理由(facts 命中词)──
|
|
2304
|
+
// single 分支不重复:它的「原因:」段(紧随其后)已经承担同一句话。
|
|
2305
|
+
if (narrative !== null && verdict.mode !== 'single' && narrative.reason !== '') {
|
|
2306
|
+
segments.push(`理由:${narrative.reason}`)
|
|
2307
|
+
}
|
|
2308
|
+
// ── B2-4(2026-11-26 修复):完整拆解上移 detail 的「拆解:」行 ──
|
|
2309
|
+
// 单元文本一字不截(见 unitDigestOf),多行 Markdown 完整渲染;选项行只留短句。
|
|
2310
|
+
// 有显式单元才出该行(数量词任务没有可点名单元,不编造)。
|
|
2311
|
+
if (narrative !== null && narrative.unitDigest !== '') {
|
|
2312
|
+
segments.push(`拆解:${narrative.unitDigest}`)
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
if (brief !== null && verdict.mode === 'single') {
|
|
2316
|
+
// single:只保留评定 + 原因短语(X15 规则;两个短语都来自同一次分析)。
|
|
2317
|
+
// 【人话化】「整件事只有 1 件,分不开」+「自己做最快」。
|
|
2318
|
+
segments.push(`原因:${brief.basisBrief},自己做最快`)
|
|
2319
|
+
} else {
|
|
2320
|
+
// v10:判断先行分支已登记过「**评定:…**」首行 → 这里只补来源差异(de-dup)。
|
|
2321
|
+
if (!judgmentFirst) {
|
|
2322
|
+
const source = brief !== null && verdict.mode === 'parallel'
|
|
2323
|
+
? `(${derivationBrief(brief.derivation)})`
|
|
2324
|
+
: ''
|
|
2325
|
+
segments.push(`**评定:${plainMode}${source}**`)
|
|
2326
|
+
}
|
|
2327
|
+
// 【人话化】「模型档:深度档」→「模型:强模型(更仔细、更慢、更贵)」。
|
|
2328
|
+
segments.push(`模型:${tierPlain}`)
|
|
2329
|
+
// 【人话化】「效率:中,加速比约 4.0×」→「效率:4 路同跑,约快 4 倍」。
|
|
2330
|
+
segments.push(`效率:${efficiencyPlain}`)
|
|
2331
|
+
if (verdict.mode === 'parallel') {
|
|
2332
|
+
// 路数:说清区间与建议值(人话版;「写入未定取下限」这种内部口径已去掉,
|
|
2333
|
+
// 文件没写清爽 判据行直接说「派发前我补上」)。
|
|
2334
|
+
const lo = brief !== null ? brief.rangeMin : verdict.concurrencyRange.min
|
|
2335
|
+
const hi = brief !== null ? brief.rangeMax : verdict.concurrencyRange.max
|
|
2336
|
+
if (lo === hi) {
|
|
2337
|
+
segments.push(`路数:就 ${hi} 路,不用选`)
|
|
2338
|
+
} else if (recConcurrency === hi) {
|
|
2339
|
+
segments.push(`路数:${lo} 到 ${hi} 路都能选,建议 ${hi} 路`)
|
|
2340
|
+
} else {
|
|
2341
|
+
segments.push(`路数:${lo} 到 ${hi} 路都能选,建议 ${recConcurrency} 路${recSourceNote}`)
|
|
2342
|
+
}
|
|
2343
|
+
if (displayVerdict.batches > 1) segments.push(`批次:要分 ${displayVerdict.batches} 批做完`)
|
|
2344
|
+
}
|
|
2345
|
+
// 瓶颈 / 判据:与完整报告同一次分析的短语版(写冲突、未给文件路径都在这里说清)。
|
|
2346
|
+
if (brief !== null) {
|
|
2347
|
+
segments.push(`瓶颈:${brief.bottleneckBrief}`)
|
|
2348
|
+
segments.push(`判据:${brief.basisBrief}`)
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
// 置信度低时如实说(不把「未评估」当「验证过」):只在 low 时加一段,mid/high 不刷屏。
|
|
2352
|
+
// 【人话化】「信号 0/4」这种内部计数不进卡片,只说「线索少,仅供参考」。
|
|
2353
|
+
if (brief !== null && brief.confidence === 'low') {
|
|
2354
|
+
segments.push('置信度:低(线索少,仅供参考)')
|
|
2355
|
+
}
|
|
2356
|
+
return segments
|
|
2357
|
+
}
|
|
2358
|
+
|
|
2359
|
+
/**
|
|
2360
|
+
* 未获得答复的统一回执形状(无提问能力 / 提问失败共用)。
|
|
2361
|
+
*
|
|
2362
|
+
* 三个语义字段必须同时为 false:answered(用户真的答过?)、concurrencyApproved
|
|
2363
|
+
* (答复是否构成并发同意)、confirmed(兼容别名)。没有提问能力 ≠ 用户确认过。
|
|
2364
|
+
* @param {string} recLabel 推荐项 label
|
|
2365
|
+
* @param {string} text 回执正文
|
|
2366
|
+
* @returns {object} 确认结果(字段形状与 askUserToConfirmConcurrency 的返回一致)
|
|
2367
|
+
*/
|
|
2368
|
+
function unansweredReceiptOf(recLabel, text) {
|
|
2369
|
+
return {
|
|
2370
|
+
text,
|
|
2371
|
+
selected: '',
|
|
2372
|
+
recommended: recLabel,
|
|
2373
|
+
override: false,
|
|
2374
|
+
answered: false,
|
|
2375
|
+
concurrencyApproved: false,
|
|
2376
|
+
confirmed: false,
|
|
2377
|
+
concurrency: 0,
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
2380
|
+
|
|
2381
|
+
/**
|
|
2382
|
+
* 确认卡提问请求的唯一构造点(W3-F-09:从主函数整段抽出,行为逐字不变)。
|
|
2383
|
+
*
|
|
2384
|
+
* ── 42-模型档第二问:与并发方案**同一张卡**的第二问(官方支持多问)──
|
|
2385
|
+
* 官方契约(本机 app.asar 内 dsh-tool-ask-user/src/index.ts):questions 是数组,
|
|
2386
|
+
* 答复按 id 原样回传;卡片 UI(QuestionComposer)是「1 / N」向导,提交时
|
|
2387
|
+
* answers = questions.map(...) 一条问题一条答案。故这里直接加第二项。
|
|
2388
|
+
* ⚠️ 第一问的 id / 选项 / detail 一字不动(integration §22.1 / §22b.13 / §22b.14 冻结)。
|
|
2389
|
+
*
|
|
2390
|
+
* detail 结构(X17):首行粗体评定 → 空行(Markdown 段落)→ 每项「- 标签:…」。
|
|
2391
|
+
* 单个 \n 在 MarkdownText 里是软换行(会折叠),故列表项间用 \n 分项、
|
|
2392
|
+
* 首行与列表之间空行(\n\n)隔成独立段落。
|
|
2393
|
+
*
|
|
2394
|
+
* @param {object} input
|
|
2395
|
+
* @param {object} input.agent 调用方 agent
|
|
2396
|
+
* @param {object} input.verdict 评分 verdict(首句按 mode 渲染)
|
|
2397
|
+
* @param {number} input.recConcurrency 推荐路数(首句数字)
|
|
2398
|
+
* @param {string} input.recSourceNote 偏好来源标注(无偏好为空串)
|
|
2399
|
+
* @param {string[]} input.segments 卡片正文段落(buildConcurrencyCardSegments 的输出)
|
|
2400
|
+
* @param {Array<object>} input.options 选项(buildConcurrencyOptionTable 的输出)
|
|
2401
|
+
* @param {object} input.modelTierQuestion 第二问(modelTierQuestionOf 的输出)
|
|
2402
|
+
* @returns {object} 官方 userQuestions.ask 的 request
|
|
2403
|
+
*/
|
|
2404
|
+
function buildConcurrencyAskRequest(input) {
|
|
2405
|
+
const { agent, verdict, recConcurrency, recSourceNote, segments, options, modelTierQuestion } = input
|
|
2406
|
+
return {
|
|
2407
|
+
agent,
|
|
2408
|
+
questions: [{
|
|
2409
|
+
id: PLAN_QUESTION_ID,
|
|
2410
|
+
header: '并发方案确认',
|
|
2411
|
+
// 【人话化变更 2026-11-26】首句不再写「并发授权仅本轮有效」这种制度术语,
|
|
2412
|
+
// 改成「只对这次有效,下一轮要重新确认」(语义不变:授权按轮失效)。
|
|
2413
|
+
question: verdict.mode === 'parallel'
|
|
2414
|
+
? `这项任务建议 ${recConcurrency} 路同时做${recSourceNote}。按这个来吗?(只对这次有效,下一轮要重新确认)`
|
|
2415
|
+
: `这项任务建议${verdict.mode === 'serial' ? '一件一件做' : '不用助手,我自己做'}。按这个来吗?(只对这次有效,下一轮要重新确认)`,
|
|
2416
|
+
// X17:Markdown 列表 —— 首行粗体评定 + 空行 + 每项「- 」列表项。
|
|
2417
|
+
// v10:判断先行时 segments[1] 是「结论:…」,列表项形态保证「- 结论:…」
|
|
2418
|
+
// 是评定行后的第一个信息项(模型档/效率/路数之前)。
|
|
2419
|
+
detail: segments.length <= 1
|
|
2420
|
+
? (segments[0] ?? '')
|
|
2421
|
+
: `${segments[0]}\n\n${segments.slice(1).map((s) => `- ${s}`).join('\n')}`,
|
|
2422
|
+
options,
|
|
2423
|
+
}, modelTierQuestion],
|
|
2424
|
+
}
|
|
2425
|
+
}
|
|
2426
|
+
|
|
2427
|
+
/**
|
|
2428
|
+
* 答复 → 结构化回执(W3-F-09:四分支的唯一实现,行为逐字不变)。
|
|
2429
|
+
*
|
|
2430
|
+
* 「同意并发」只认两种(结构化语义表,绝不解析文案):
|
|
2431
|
+
* ① 按建议执行,且**建议本身**就是并发(verdict.mode === 'parallel');
|
|
2432
|
+
* ② 用户明确改选到一个把并发决定权 / 拆分路数交给执行方的选项。
|
|
2433
|
+
* 其余(改串行 / 不派发 / 自定义输入 / 未作答)一律不算同意 → 闸继续拦。
|
|
2434
|
+
*
|
|
2435
|
+
* @param {object} input
|
|
2436
|
+
* @param {string} input.selected 用户选中的 label(空串 = 未选或自定义输入)
|
|
2437
|
+
* @param {string} input.custom 自定义输入(空串 = 无)
|
|
2438
|
+
* @param {string} input.recLabel 推荐项 label
|
|
2439
|
+
* @param {string} input.modeLabel 展示用最终 verdict 的 mode 人话
|
|
2440
|
+
* @param {Map<string, {approves: boolean, concurrency: number}>} input.optionSemanticsByLabel 选项语义表
|
|
2441
|
+
* @returns {{text: string, selected: string, override: boolean, answered: boolean,
|
|
2442
|
+
* concurrencyApproved: boolean, confirmed: boolean, concurrency: number}}
|
|
2443
|
+
*/
|
|
2444
|
+
function receiptOfConcurrencyAnswer(input) {
|
|
2445
|
+
const { selected, custom, recLabel, modeLabel, optionSemanticsByLabel } = input
|
|
2446
|
+
if (custom.length > 0 && selected.length === 0) {
|
|
2447
|
+
// round-7:显式标记「用户真的答复过」,供并发闸判定。
|
|
2448
|
+
// R1/P1:**答复 ≠ 同意并发**。自定义文本没有任何结构化依据说明用户
|
|
2449
|
+
// 同意并发(用户完全可能写「不要并发」),所以两个语义字段都是 false:
|
|
2450
|
+
// 闸继续拦,直到用户通过某个**明确的**选项授权并发。
|
|
2451
|
+
return {
|
|
2452
|
+
text: `用户确认:${custom}(自定义)`,
|
|
2453
|
+
selected: '',
|
|
2454
|
+
override: false,
|
|
2455
|
+
answered: true,
|
|
2456
|
+
concurrencyApproved: false,
|
|
2457
|
+
confirmed: false,
|
|
2458
|
+
concurrency: 0,
|
|
2459
|
+
}
|
|
2460
|
+
}
|
|
2461
|
+
if (selected === recLabel) {
|
|
2462
|
+
// 「按建议执行」是否等于同意并发、授权几路,取决于**建议本身**(结构化比较,不解析文案)
|
|
2463
|
+
const semantics = optionSemanticsByLabel.get(recLabel) ?? { approves: false, concurrency: 0 }
|
|
2464
|
+
return {
|
|
2465
|
+
// #7:per-turn 语义落地后卡面/回执必须告知授权范围 —— 回执也标「本轮有效」。
|
|
2466
|
+
text: `用户确认:按建议执行 —— ${modeLabel}(本轮有效)`,
|
|
2467
|
+
selected,
|
|
2468
|
+
override: false,
|
|
2469
|
+
answered: true,
|
|
2470
|
+
concurrencyApproved: semantics.approves,
|
|
2471
|
+
// 兼容别名:语义已收紧为「答复且答复就是同意并发」(旧调用点 / 审计断言读它)
|
|
2472
|
+
confirmed: semantics.approves,
|
|
2473
|
+
// v8:授权路数上限 N(供 tools 传给 markConcurrencyConfirmed,「本轮在跑+在途 < N」放行)
|
|
2474
|
+
concurrency: semantics.concurrency,
|
|
2475
|
+
}
|
|
2476
|
+
}
|
|
2477
|
+
if (selected.length > 0) {
|
|
2478
|
+
// 改选:只按**构造时登记的结构化语义**判定,绝不解析文案。
|
|
2479
|
+
// 未登记的 label(宿主自定义 / 第三方 UI 回填)= 未知 → 不放行。
|
|
2480
|
+
const semantics = optionSemanticsByLabel.get(selected) ?? { approves: false, concurrency: 0 }
|
|
2481
|
+
return {
|
|
2482
|
+
// #7:改选也是一次授权(若构成并发同意)→ 回执同样标「本轮有效」,限定授权范围。
|
|
2483
|
+
text: `用户确认:${selected}(注意:与建议不同,请按用户选择执行)(本轮有效)`,
|
|
2484
|
+
selected,
|
|
2485
|
+
override: selected !== recLabel,
|
|
2486
|
+
answered: true,
|
|
2487
|
+
concurrencyApproved: semantics.approves,
|
|
2488
|
+
confirmed: semantics.approves,
|
|
2489
|
+
concurrency: semantics.concurrency,
|
|
2490
|
+
}
|
|
2491
|
+
}
|
|
2492
|
+
// 并发方案没作答(但模型档可能答了):并发语义仍按旧行为 = 未作答;
|
|
2493
|
+
// 模型档结论照样并进回执(用户点了「快速模型」这一事实不能丢)—— 由调用方 done() 追加。
|
|
2494
|
+
return {
|
|
2495
|
+
text: '⚠ 用户未选择任何选项:请重新询问,不要自行假定。',
|
|
2496
|
+
selected: '',
|
|
2497
|
+
override: false,
|
|
2498
|
+
// 用户没有作答 → 不算确认,闸必须继续拦
|
|
2499
|
+
answered: false,
|
|
2500
|
+
concurrencyApproved: false,
|
|
2501
|
+
confirmed: false,
|
|
2502
|
+
concurrency: 0,
|
|
2503
|
+
}
|
|
2504
|
+
}
|
|
2505
|
+
|
|
2506
|
+
/**
|
|
2507
|
+
* 提问失败的降级判据(W3-F-09:从 catch 分支抽出,行为逐字不变)。
|
|
2508
|
+
*
|
|
2509
|
+
* 官方错误码:NO_PROVIDER(无 UI answerer)/ DELEGATED_CALLER(子代理不能问)/
|
|
2510
|
+
* CALLER_NOT_LIVE(agent 已失效)/ ASK_ABORTED(被取消)/ ASK_TIMED_OUT(超时)。
|
|
2511
|
+
* 超时相关一律归到「提问超时」这一条:ASK_TIMED_OUT 是官方 askTimed 的码,
|
|
2512
|
+
* ASK_ABORTED 也可能由我们传下去的 signal 触发;TimeoutError / AbortError 是 DOM
|
|
2513
|
+
* 约定,旧实现或第三方 UI 会用它们表示「等不到了」。四种都表示「没拿到用户答复」。
|
|
2514
|
+
*
|
|
2515
|
+
* H1-04:只有**环境性**失败才标「确认不可用」(fail-open,否则死锁):
|
|
2516
|
+
* - DELEGATED_CALLER / NO_PROVIDER / CALLER_NOT_LIVE → 这个环境压根问不了;
|
|
2517
|
+
* - 超时 / 用户未作答 → 用户**有机会**回答但没答 → 继续拦(真实的「未确认」)。
|
|
2518
|
+
* 若把超时也标成 unavailable,闸就变成「等一会儿自动放行」,等于没有闸。
|
|
2519
|
+
* @param {unknown} error answerer 抛出的错误
|
|
2520
|
+
* @returns {{reason: string, environmentBlocked: boolean}}
|
|
2521
|
+
*/
|
|
2522
|
+
function describeAskFailure(error) {
|
|
2523
|
+
const code = error?.code ?? 'UNKNOWN'
|
|
2524
|
+
const name = error?.name ?? ''
|
|
2525
|
+
const timedOut = code === 'ASK_TIMED_OUT'
|
|
2526
|
+
|| code === 'ASK_TIMEOUT'
|
|
2527
|
+
|| code === 'ASK_ABORTED'
|
|
2528
|
+
|| name === 'TimeoutError'
|
|
2529
|
+
|| name === 'AbortError'
|
|
2530
|
+
const reason = timedOut
|
|
2531
|
+
? '提问超时'
|
|
2532
|
+
: code === 'DELEGATED_CALLER'
|
|
2533
|
+
? '本工具由子代理调用,官方不允许子代理向用户提问'
|
|
2534
|
+
: code === 'NO_PROVIDER'
|
|
2535
|
+
? '当前界面没有可用的提问能力'
|
|
2536
|
+
: `提问失败(${code})`
|
|
2537
|
+
const environmentBlocked = !timedOut
|
|
2538
|
+
&& (code === 'DELEGATED_CALLER' || code === 'NO_PROVIDER' || code === 'CALLER_NOT_LIVE')
|
|
2539
|
+
return { reason, environmentBlocked }
|
|
2540
|
+
}
|
|
2541
|
+
|
|
2542
|
+
/**
|
|
2543
|
+
* 记录用户真实答复过的「模型档」(tier-state 纯内存;失败静默)。
|
|
2544
|
+
*
|
|
2545
|
+
* 【档位路由显式化 2026-11-27】用户真实答复过第二问 → 按会话记录已确认档位
|
|
2546
|
+
* (tier-state.mjs 纯内存;inject.mjs 委派前读取,作第二优先级注入来源)。
|
|
2547
|
+
* 记录范围刻意收窄到**有效选择**:点名强/快档(无论白名单是否解析出路由,无路由
|
|
2548
|
+
* 也记档位名)与命中白名单的 custom(记其 route)。回退路径(未识别 label / 没填名 /
|
|
2549
|
+
* 不在白名单)不是用户的档位表态,不记录 —— 绝不把回退冒充成用户的选择。
|
|
2550
|
+
* 读取失败(拿不到会话键)不记录、不报错 —— 这是增强信息,不是授权链的必要环节。
|
|
2551
|
+
* @param {object} agent 调用方 agent
|
|
2552
|
+
* @param {object} split splitAnswerBatch 的结果(tier 段)
|
|
2553
|
+
* @param {object} modelTier resolveModelTierOf 的结果
|
|
2554
|
+
*/
|
|
2555
|
+
function recordTierChoiceOf(agent, split, modelTier) {
|
|
2556
|
+
if (modelTier.answered !== true) return
|
|
2557
|
+
try {
|
|
2558
|
+
const tierSelectedLabel = firstSelectedOf(split.tier)
|
|
2559
|
+
const choseNamed = tierSelectedLabel === MODEL_TIER_LABELS.strong || tierSelectedLabel === MODEL_TIER_LABELS.fast
|
|
2560
|
+
const choseCustom = modelTier.name === 'custom' && modelTier.route !== undefined
|
|
2561
|
+
if ((choseNamed || choseCustom) && sessionKeyOf(agent) !== undefined) {
|
|
2562
|
+
const confirmedTier = modelTier.name === 'custom' ? 'custom'
|
|
2563
|
+
: modelTier.name === 'fast' ? 'cheap'
|
|
2564
|
+
: 'deep'
|
|
2565
|
+
// route 传 modelTier.route:custom 命中 → 其路由;强/快档解析出 → 深度/快速档路由,
|
|
2566
|
+
// 没解析出 → undefined(tier-state 对非对象 route 按无路由记录,档位名仍在)。
|
|
2567
|
+
recordConfirmedTier(sessionKeyOf(agent), confirmedTier, modelTier.route)
|
|
2568
|
+
}
|
|
2569
|
+
} catch { /* 档位记录失败绝不影响确认卡回执与闸行为(静默降级纪律) */ }
|
|
2570
|
+
}
|
|
2571
|
+
|
|
2572
|
+
export async function askUserToConfirmConcurrency(ctx, agent, verdict, dimensions, task, preference = undefined, display = undefined, tierPolicy = defaultTierPolicy()) {
|
|
2573
|
+
// 【档位路由显式化 2026-11-27】尾参 tierPolicy:具名档位(强/快)解析白名单路由的策略表。
|
|
2574
|
+
// 尾部追加(既有 7 参调用点逐字兼容 —— 全部测试替身与旧调用点零改动)。
|
|
2575
|
+
//
|
|
2576
|
+
// ── W3-F-09(P2)拆分说明 ──
|
|
2577
|
+
// 原函数 457 行(选项构造 + 正文拼装 + 提问 + 回执四件事混在一起)。现在四层各自成函数:
|
|
2578
|
+
// · buildConcurrencyOptionTable —— 选项构造 + label→语义表(既有,本轮之前已抽)
|
|
2579
|
+
// · buildConcurrencyCardSegments —— 卡片正文 segments(纯搬移)
|
|
2580
|
+
// · buildConcurrencyAskRequest —— 官方 questions/request 组装(纯搬移)
|
|
2581
|
+
// · receiptOfConcurrencyAnswer —— 答复 → 结构化回执(四个分支的唯一实现)
|
|
2582
|
+
// · describeAskFailure / recordTierChoiceOf —— catch 判据 / 档位记账
|
|
2583
|
+
// 本函数只剩编排:取值 → 正文 → 提问 → 回执。行为零变更(纯搬移重构)。
|
|
2584
|
+
// ── R-01:范围 / 偏好 / 推荐路数 / 来源一次算清(与 execute 重建报告共用同一份)──
|
|
2585
|
+
// display 由 execute 传入以避免重复计算;直接调用(测试 / 旧调用点)时按需现算。
|
|
2586
|
+
const d = display ?? resolvePreferredDisplay(verdict, dimensions, task, preference)
|
|
2587
|
+
const detailTask0 = d.detailTask0
|
|
2588
|
+
const brief0 = d.brief0
|
|
2589
|
+
const range0 = d.range0
|
|
2590
|
+
const preferredUsable = d.preferredUsable
|
|
2591
|
+
const recConcurrency = d.recConcurrency
|
|
2592
|
+
const recSourceNote = d.recSourceNote
|
|
2593
|
+
// W3-F-02:卡片展示用的最终 verdict(偏好生效时为按最终路数重算过的完整 verdict)。
|
|
2594
|
+
// 效率 / 批次 / 评定行 / 路数行一律读它 —— 不再出现「3 路」与「8 路同跑 / 2 批」同屏。
|
|
2595
|
+
const displayVerdict = d.finalVerdict ?? verdict
|
|
2596
|
+
// 选项文案必须与最终 verdict 一致(不能用中间值);偏好生效时用推荐路数,使
|
|
2597
|
+
// 「评定行 / 回执」与卡片首选项 / 闸授权同源(R-01)。无偏好时逐字等于旧 modeLabel。
|
|
2598
|
+
const modeLabel = concurrencyModeLabel(displayVerdict)
|
|
2599
|
+
|
|
2600
|
+
// 推荐项 label:parallel 时按推荐档渲染;无偏好 → 恒 verdict.concurrency(冻结兼容),
|
|
2601
|
+
// 有偏好 → 偏好档(卡片「路数:」段与 question 首句同步标注来源)。
|
|
2602
|
+
// 【人话化变更 2026-11-26】「按建议执行:并发 N 路」保留(用户认得);
|
|
2603
|
+
// 串行 / 不派发两档不再写「串行(按序执行)/ 不派发,主会话直接做」,
|
|
2604
|
+
// 改成「一件一件做 / 我自己做」。
|
|
2605
|
+
const recLabel = verdict.mode === 'parallel'
|
|
2606
|
+
? `按建议执行:并发 ${recConcurrency} 路(推荐)`
|
|
2607
|
+
: verdict.mode === 'serial'
|
|
2608
|
+
? '按建议执行:一件一件做(推荐)'
|
|
2609
|
+
: '按建议执行:我自己做(推荐)'
|
|
2610
|
+
|
|
2611
|
+
// ── B2-4:本任务文案素材包(与 brief 同一次分析;渲染层只取材,不新判)──
|
|
2612
|
+
// 任务原文为空(合成 verdict 单测)→ narrative = null,下面所有「本任务」文案
|
|
2613
|
+
// 逐字退回旧行为(integration §22.1 / §22b.13 的选项文案冻结表零影响)。
|
|
2614
|
+
const narrative = detailTask0 === '' || brief0 === null ? null : cardNarrativeOf(detailTask0, displayVerdict, d.analysis0)
|
|
2615
|
+
|
|
2616
|
+
// ── 选项语义表:**构造时**就登记「这个选项是否等于同意并发」(R1/P1 修复)──
|
|
2617
|
+
//
|
|
2618
|
+
// 早先任何 selected 非空的答复都写 confirmed: true,于是「按建议执行:不派发」/
|
|
2619
|
+
// 「改为串行」/ 自定义输入全部被当成「用户确认了并发方案」→ gate 在本会话**永久**
|
|
2620
|
+
// 放行后续并发派发(第二轮三层审计 L2-C1 探针)。修法见 buildConcurrencyOptionTable
|
|
2621
|
+
// 的头注释:**不解析文案**,而是在造选项时把「是否构成同意并发 + 授权路数上限 N」
|
|
2622
|
+
// 写进结构化表;用户答复回来后按 label **精确**命中,命中不了(未登记 label)= 未知 → 不放行。
|
|
2623
|
+
// 消费侧唯一实现在 receiptOfConcurrencyAnswer(文案 ↔ 旗标核对表见其注释)。
|
|
2624
|
+
const { options, optionSemanticsByLabel } = buildConcurrencyOptionTable({
|
|
2625
|
+
verdict, dimensions, recLabel, recConcurrency, preferredUsable,
|
|
2626
|
+
range0, recSourceNote, narrative,
|
|
2627
|
+
})
|
|
2628
|
+
|
|
2629
|
+
// ── 卡片正文(X17 / 人话化 / B2-4):全部就读值与排版预算,不含新判定 ──
|
|
2630
|
+
// W3-F-09:整段抽到 buildConcurrencyCardSegments(纯搬移)。
|
|
2631
|
+
const segments = buildConcurrencyCardSegments({
|
|
2632
|
+
verdict, displayVerdict, dimensions, recConcurrency, recSourceNote, preferredUsable,
|
|
2633
|
+
brief: brief0, narrative,
|
|
2634
|
+
})
|
|
2635
|
+
|
|
2636
|
+
// W3-F-08:userQuestions getter 抛错按「无提问能力」降级(H1-04 同一分支),
|
|
2637
|
+
// 绝不让可选服务的异常把整张卡片路径打断。
|
|
2638
|
+
let questions
|
|
2639
|
+
try { questions = ctx.get?.('userQuestions') } catch { questions = undefined }
|
|
2640
|
+
if (questions === undefined || typeof questions.ask !== 'function') {
|
|
2641
|
+
// H1-04(第 10 次审计):本环境**根本没有提问能力**,不是"用户拒绝"。
|
|
2642
|
+
// 若闸坚持拦,模型永远无法获得确认 → 并发功能死锁(用户什么也做不了)。
|
|
2643
|
+
// 标记为「确认不可用」让闸降级放行——注意 confirmed 仍是 false(语义不混用)。
|
|
2644
|
+
markConcurrencyUnavailable(agent)
|
|
2645
|
+
return unansweredReceiptOf(recLabel,
|
|
2646
|
+
'⚠ 未能获取用户确认(当前环境无 userQuestions 服务):'
|
|
2647
|
+
+ '请先把上面的建议转述给用户并等待确认,再执行派发。')
|
|
2648
|
+
}
|
|
2649
|
+
|
|
2650
|
+
// ── 42-模型档第二问:读本会话档位绑定(/tier 编辑档位模型)──
|
|
2651
|
+
// 【变更 2026-11-27 档位配置】第二问 detail 披露当前深度思考模型;读取失败
|
|
2652
|
+
// (tier-config 不可用 / 无会话键)静默退回未绑定文案。
|
|
2653
|
+
// 【变更 2026-11-27 badge 绑定并读】与 resolveModelTierOf 同序合并:badge UI 绑定 →
|
|
2654
|
+
// /tier 绑定(tier-config 优先),detail 显示的就是强模型档将要解析的那条 deep 路由。
|
|
2655
|
+
const cardSessionKey = sessionKeyOf(agent)
|
|
2656
|
+
const cardTierConfig = { ...((await readTierBindings(ctx, cardSessionKey)) ?? {}), ...(readTierConfig(cardSessionKey) ?? {}) }
|
|
2657
|
+
const modelTierQuestion = modelTierQuestionOf(verdict, dimensions, narrative, cardTierConfig)
|
|
2658
|
+
|
|
2659
|
+
const request = buildConcurrencyAskRequest({
|
|
2660
|
+
agent, verdict, recConcurrency, recSourceNote, segments, options, modelTierQuestion,
|
|
2661
|
+
})
|
|
2662
|
+
|
|
2663
|
+
// 提问必须有 deadline,否则用户不点卡片时整个 turn 永久挂起(C3-08)。
|
|
2664
|
+
// 详见 facts-store.mjs 的 ASK_TIMEOUT_MS / askWithDeadline 注释。
|
|
2665
|
+
const asked = await askWithDeadline(questions, request, ASK_TIMEOUT_MS)
|
|
2666
|
+
if (asked.timedOut === true) {
|
|
2667
|
+
return {
|
|
2668
|
+
text: '⚠ 未能获取用户确认(提问超时):请先把上面的建议转述给用户并等待确认,再执行派发(下一轮可直接重新调用本工具再次弹卡)。',
|
|
2669
|
+
selected: '',
|
|
2670
|
+
recommended: recLabel,
|
|
2671
|
+
override: false,
|
|
2672
|
+
// 超时绝不能被当作已确认(round-7b 核心教训)→ 闸必须继续拦
|
|
2673
|
+
answered: false,
|
|
2674
|
+
concurrencyApproved: false,
|
|
2675
|
+
confirmed: false,
|
|
2676
|
+
concurrency: 0,
|
|
2677
|
+
}
|
|
2678
|
+
}
|
|
2679
|
+
|
|
2680
|
+
try {
|
|
2681
|
+
// answerer 自己抛的错(NO_PROVIDER / DELEGATED_CALLER / CALLER_NOT_LIVE /
|
|
2682
|
+
// ASK_ABORTED / ASK_TIMED_OUT ...)统一走下面的 catch 降级。
|
|
2683
|
+
if (asked.error !== undefined) throw asked.error
|
|
2684
|
+
// ── 42:答复**按 id 分流**(不再只读 answers[0])──
|
|
2685
|
+
// 'concurrency-plan' → 既有语义表一字不动;'model-tier' → 只决定派发模型档,
|
|
2686
|
+
// 绝不参与并发授权判定(模型档是资源选择,不是「同意并发」的表态)。
|
|
2687
|
+
const split = splitAnswerBatch(asked.answer)
|
|
2688
|
+
const picked = split.plan
|
|
2689
|
+
const selected = picked?.selected?.[0] ?? ''
|
|
2690
|
+
const custom = typeof picked?.custom === 'string' && picked.custom.length > 0 ? picked.custom : ''
|
|
2691
|
+
// 第二问的结论现算(只有用户真的选了「指定模型」才会去读白名单投影)。
|
|
2692
|
+
const modelTier = await resolveModelTierOf(ctx, agent, split.tier, tierPolicy)
|
|
2693
|
+
// 用户真实答复过第二问 → 按会话记录已确认档位(tier-state 纯内存)。
|
|
2694
|
+
recordTierChoiceOf(agent, split, modelTier)
|
|
2695
|
+
// 并进既有回执的**唯一**通道:不改任何并发语义字段,只在文字末尾追加一句
|
|
2696
|
+
// 「模型档 / 已按指定模型派发 / 已回退」。回执是模型读的那份报告 → 回退绝不静默。
|
|
2697
|
+
const done = (result) => ({
|
|
2698
|
+
...result,
|
|
2699
|
+
text: modelTier.suffix === '' ? result.text : `${result.text};${modelTier.suffix}`,
|
|
2700
|
+
modelTier: modelTier.name,
|
|
2701
|
+
modelRoute: modelTier.route,
|
|
2702
|
+
modelNotice: modelTier.notice,
|
|
2703
|
+
modelTierAnswered: modelTier.answered,
|
|
2704
|
+
})
|
|
2705
|
+
// 回执语义(推荐项 / 改选 / 自定义 / 未作答)由 receiptOfConcurrencyAnswer 单点判定。
|
|
2706
|
+
return done({
|
|
2707
|
+
...receiptOfConcurrencyAnswer({ selected, custom, recLabel, modeLabel, optionSemanticsByLabel }),
|
|
2708
|
+
recommended: recLabel,
|
|
2709
|
+
})
|
|
2710
|
+
} catch (error) {
|
|
2711
|
+
// 超时 / 环境性失败 / 提问抛错:文案与 fail-open 判据由 describeAskFailure 单点给出。
|
|
2712
|
+
const { reason, environmentBlocked } = describeAskFailure(error)
|
|
2713
|
+
if (environmentBlocked) markConcurrencyUnavailable(agent)
|
|
2714
|
+
return unansweredReceiptOf(recLabel,
|
|
2715
|
+
`⚠ 未能获取用户确认(${reason}):请先把上面的建议转述给用户并等待确认,再执行派发。`)
|
|
2716
|
+
}
|
|
2717
|
+
}
|
|
2718
|
+
|
|
2719
|
+
|
|
2720
|
+
/**
|
|
2721
|
+
* 打断阀门工具(批次 2 / B2-1,V-INT):叫停一个子代理。
|
|
2722
|
+
*
|
|
2723
|
+
* ── 为什么需要这个工具(根因实证 00:31)──────────────────────
|
|
2724
|
+
* send_message 的投递是 inbox → 下一个 step 边界注入:子代理正在长 step 连击
|
|
2725
|
+
* (run_code / 长探针)时,停止消息延迟 1-2 分钟,且**无法中断正在执行的工具调用**
|
|
2726
|
+
* (实测叫停 w-batch-verify 135 秒)。本工具走**双通道**把这个延迟压到秒级:
|
|
2727
|
+
* ① 消息通道:照旧发一条 send_message(子代理的 inbox,等 step 边界才读);
|
|
2728
|
+
* ② 标志通道:写中断标志(gate.mjs 的 interruptFlags)→ 子代理**下一次工具分派**
|
|
2729
|
+
* 的结果上就会带「lead 已请求停止」提示(见 dispatch-gate.mjs 的查点),
|
|
2730
|
+
* 不必等 step 边界。
|
|
2731
|
+
* 两条通道是**双保险**而不是替代关系:消息通道保证「子代理最终一定会被告知」,
|
|
2732
|
+
* 标志通道保证「它不必等到下一次 step 边界才知道」。
|
|
2733
|
+
*
|
|
2734
|
+
* ── 为什么带执行代(epochSeq)──
|
|
2735
|
+
* 参照实现的 InterruptSession 用 expectedExecutionId 做乐观并发控制(防停止意图过期、
|
|
2736
|
+
* 误杀新任务)。本工具写入标志时由 gate.mjs 记下当时的轮次代数;子代理若在此之后
|
|
2737
|
+
* 换轮(新确认 / 新任务),标志自动判陈旧失效——不需要调用方额外传执行代,
|
|
2738
|
+
* 因为「主代理此刻要停的是谁」本身就以写入时刻为准。
|
|
2739
|
+
*
|
|
2740
|
+
* ── 诚实降级 ──
|
|
2741
|
+
* subagents 服务不可用(老宿主 / 子代理运行时不支持 sendMessage)时:标志通道照常生效
|
|
2742
|
+
* (内存层不依赖任何服务),消息通道降级并在回执里如实写明——绝不假装已投递。
|
|
2743
|
+
* 这与 notice.mjs「说不知道远好于说错」是同一条信任链纪律。
|
|
2744
|
+
*
|
|
2745
|
+
* @param {object} ctx cordis context
|
|
2746
|
+
* @returns {object} ToolDefinition
|
|
2747
|
+
*/
|
|
2748
|
+
function makeInterruptTool(ctx) {
|
|
2749
|
+
return {
|
|
2750
|
+
name: 'send_interrupt',
|
|
2751
|
+
description:
|
|
2752
|
+
// 【变更 2026-11-28 send_interrupt 硬停】优先宿主 subagents.interrupt 真硬停;
|
|
2753
|
+
// 无该 API / 解析不出会话 id 时降级纯软通道,回执逐通道如实报告(不谎报)。
|
|
2754
|
+
'叫停一个正在跑的子代理:优先调用宿主 subagents.interrupt 真硬停(终止其当前活动),'
|
|
2755
|
+
+ '同时写中断标志(工具层秒级可见)**并**发一条消息(inbox 通道)。'
|
|
2756
|
+
+ '子代理下一次工具分派的结果上会带「lead 已请求停止:请立即收尾」提示,'
|
|
2757
|
+
+ '要求它把已完成部分写进报告、结掉共享任务,不要再开新工作。'
|
|
2758
|
+
+ '传入 agent(list_agents 返回的名字或会话 id)与可选 reason。'
|
|
2759
|
+
+ '宿主没有 interrupt(或解析不出目标会话 id)时降级为纯软通道:'
|
|
2760
|
+
+ '不掐断正在执行的模型流,只截断「继续开新工作」这条路径;'
|
|
2761
|
+
+ '执行代对账 + 10 分钟 TTL 双保险防陈旧标志误杀新一轮任务。'
|
|
2762
|
+
+ '回执按「硬中断 / 标志 / 消息」三通道如实报告,未调用的通道写明原因,绝不谎报。',
|
|
2763
|
+
parameters: {
|
|
2764
|
+
type: 'object',
|
|
2765
|
+
additionalProperties: false,
|
|
2766
|
+
properties: {
|
|
2767
|
+
agent: {
|
|
2768
|
+
type: 'string',
|
|
2769
|
+
description: '要停止的子代理:list_agents 返回的名字,或它的会话 id。',
|
|
2770
|
+
},
|
|
2771
|
+
reason: {
|
|
2772
|
+
type: 'string',
|
|
2773
|
+
description: '可选。停止理由(会原样转达给子代理,也会写进中断记账)。',
|
|
2774
|
+
},
|
|
2775
|
+
},
|
|
2776
|
+
required: ['agent'],
|
|
2777
|
+
},
|
|
2778
|
+
output: {
|
|
2779
|
+
schema: { type: 'string' },
|
|
2780
|
+
render: (_args, value) => [{ type: 'text', text: String(value) }],
|
|
2781
|
+
},
|
|
2782
|
+
// 只写一张内存标志表 + 一次消息投递,不阻塞、不等用户、不改其它会话状态 →
|
|
2783
|
+
// 同批多个叫停互不干扰,声明可并发。
|
|
2784
|
+
isConcurrencySafe: () => true,
|
|
2785
|
+
async execute(args, exec) {
|
|
2786
|
+
const caller = exec?.agent
|
|
2787
|
+
const target = String(args?.agent ?? '').trim()
|
|
2788
|
+
if (target === '') throw new Error('send_interrupt 需要非空的 agent(子代理名或会话 id)')
|
|
2789
|
+
// 理由截断(schema 拦不住绕过 schema 的直调,与 suggest_concurrency 的入口截断同款纪律):
|
|
2790
|
+
// 超长理由会让记账行与提示文本无界增长,截断而非抛错——按前 MAX_INTERRUPT_REASON_CHARS
|
|
2791
|
+
// 个码点转达,并如实标注已截断。
|
|
2792
|
+
const rawReason = String(args?.reason ?? '').trim()
|
|
2793
|
+
const truncated = Array.from(rawReason).length > MAX_INTERRUPT_REASON_CHARS
|
|
2794
|
+
const reason = truncated
|
|
2795
|
+
? Array.from(rawReason).slice(0, MAX_INTERRUPT_REASON_CHARS).join('')
|
|
2796
|
+
: rawReason
|
|
2797
|
+
// ── 0. 解析目标会话(名字 → 子代理会话 id);服务不可用时按名字兜底 ──
|
|
2798
|
+
// 名字兜底也成立:gate.mjs 的反查表由 subagent/start 建立,读取侧按名匹配。
|
|
2799
|
+
let targetSessionId
|
|
2800
|
+
let subagents
|
|
2801
|
+
try {
|
|
2802
|
+
subagents = ctx?.get?.('subagents')
|
|
2803
|
+
} catch { subagents = undefined }
|
|
2804
|
+
if (subagents !== undefined && subagents !== null && typeof subagents.resolveName === 'function') {
|
|
2805
|
+
try {
|
|
2806
|
+
const resolved = await subagents.resolveName(caller, target, exec?.signal)
|
|
2807
|
+
if (typeof resolved === 'string' && resolved !== '') targetSessionId = resolved
|
|
2808
|
+
} catch { /* 解析失败 → 按名字兜底(标志键退化为 n:<name>) */ }
|
|
2809
|
+
}
|
|
2810
|
+
// ── 0b. 【变更 2026-11-28 send_interrupt 硬停】硬中断通道(先硬停再消息)──
|
|
2811
|
+
//
|
|
2812
|
+
// 宿主 subagents.interrupt(targetSessionId, { kind:'user', parentSessionId }) 是真正的
|
|
2813
|
+
// 硬停止原语(宿主会终止该子代理当前活动),interrupt 同步 void、不返回结果。
|
|
2814
|
+
// 时序:先硬停再发消息——消息可能失败,硬停已生效;反序则「消息说停了、实际还在跑」。
|
|
2815
|
+
// 纪律:调用整体 try/catch 静默(含同步抛错与返回 Promise 的 reject),错误如实写入
|
|
2816
|
+
// hardStopNote;无 interrupt API / 无 targetSessionId 时降级(软通道照旧),绝不谎报。
|
|
2817
|
+
let hardStopOk = false
|
|
2818
|
+
let hardStopNote = ''
|
|
2819
|
+
// callerSession 先取:硬停参数 parentSessionId 与标志记账共用(声明必须先于硬停调用)。
|
|
2820
|
+
const callerSession = sessionKeyOf(caller)
|
|
2821
|
+
if (subagents !== undefined && subagents !== null && typeof subagents.interrupt === 'function') {
|
|
2822
|
+
if (targetSessionId !== undefined && targetSessionId !== null && targetSessionId !== '') {
|
|
2823
|
+
try {
|
|
2824
|
+
const returned = subagents.interrupt(targetSessionId, {
|
|
2825
|
+
kind: 'user',
|
|
2826
|
+
parentSessionId: callerSession,
|
|
2827
|
+
})
|
|
2828
|
+
// 宿主契约是同步 void,不 await(回执同步构建,异步结果本就进不了本次回执);
|
|
2829
|
+
// 若环境返回 Promise(mock / 未来版本),挂一个静默 .catch:
|
|
2830
|
+
// 只为不产生 unhandledRejection 崩进程,不改变本次回执语义。
|
|
2831
|
+
if (returned !== undefined && returned !== null && typeof returned.catch === 'function') {
|
|
2832
|
+
returned.catch((error) => {
|
|
2833
|
+
hardStopNote = '调用失败(' + String(error?.message ?? error) + ')'
|
|
2834
|
+
})
|
|
2835
|
+
}
|
|
2836
|
+
hardStopOk = true
|
|
2837
|
+
} catch (error) {
|
|
2838
|
+
hardStopNote = '调用失败(' + String(error?.message ?? error) + ')'
|
|
2839
|
+
}
|
|
2840
|
+
} else {
|
|
2841
|
+
hardStopNote = '解析不出目标会话 id'
|
|
2842
|
+
}
|
|
2843
|
+
} else {
|
|
2844
|
+
hardStopNote = '宿主没有 subagents.interrupt'
|
|
2845
|
+
}
|
|
2846
|
+
// ── 1. 标志通道(工具层秒级):写中断标志 + 落中断记账 ──
|
|
2847
|
+
const flagged = setInterruptFlag(target, {
|
|
2848
|
+
reason: reason === '' ? undefined : reason,
|
|
2849
|
+
targetSessionId,
|
|
2850
|
+
sessionKey: callerSession,
|
|
2851
|
+
})
|
|
2852
|
+
// ── 2. 消息通道(inbox,照旧):投递失败绝不冒充成功 ──
|
|
2853
|
+
let delivered = false
|
|
2854
|
+
let deliveryNote = ''
|
|
2855
|
+
const messageText = '[team-preflight] lead 已请求停止:请立即收尾。'
|
|
2856
|
+
+ (reason === '' ? '' : '理由:' + reason)
|
|
2857
|
+
+ '\n把已完成部分写进报告、结掉共享任务,不要再开新工作。'
|
|
2858
|
+
if (subagents === undefined || subagents === null) {
|
|
2859
|
+
deliveryNote = '本环境没有 subagents 服务:消息通道未投递(标志通道已生效)'
|
|
2860
|
+
} else if (typeof subagents.sendMessage !== 'function') {
|
|
2861
|
+
deliveryNote = '本环境的 subagents 服务没有 sendMessage:消息通道未投递(标志通道已生效)'
|
|
2862
|
+
} else {
|
|
2863
|
+
try {
|
|
2864
|
+
await subagents.sendMessage(caller, targetSessionId ?? target, [{ type: 'text', text: messageText }], { signal: exec?.signal })
|
|
2865
|
+
delivered = true
|
|
2866
|
+
} catch (error) {
|
|
2867
|
+
deliveryNote = '消息通道投递失败(' + String(error?.message ?? error) + '),标志通道已生效'
|
|
2868
|
+
}
|
|
2869
|
+
}
|
|
2870
|
+
// ── 3. 回执:只陈述可验证的事实,不夸大 ──
|
|
2871
|
+
const lines = []
|
|
2872
|
+
lines.push(flagged
|
|
2873
|
+
? '已向子代理「' + target + '」发出停止请求。'
|
|
2874
|
+
: '未能写入中断标志(agent 参数非法)。')
|
|
2875
|
+
if (flagged) {
|
|
2876
|
+
lines.push('· 硬中断:' + (hardStopOk
|
|
2877
|
+
? '已调用 subagents.interrupt(宿主会终止该子代理当前活动)'
|
|
2878
|
+
: '未调用(' + hardStopNote + ')'));
|
|
2879
|
+
lines.push('· 标志通道:已写入(子代理下一次工具分派的结果上会带「立即收尾」提示,秒级可见)');
|
|
2880
|
+
lines.push('· 消息通道:' + (delivered ? '已投递(等子代理下一次 step 边界才会读到)' : deliveryNote));
|
|
2881
|
+
lines.push('· 执行代对账:标志在子代理换轮(新确认 / 新任务)后自动失效;TTL 10 分钟;子代理收尾即自动清。');
|
|
2882
|
+
lines.push('· 记账:中断事件已写入本会话台账 ledger.jsonl(kind: interrupt),可审计、可续跑。');
|
|
2883
|
+
if (truncated) lines.push('· 注意:reason 超长已截断至 ' + MAX_INTERRUPT_REASON_CHARS + ' 码点。');
|
|
2884
|
+
lines.push('本工具优先宿主硬停(subagents.interrupt,已终止当前活动则无需等待);'
|
|
2885
|
+
+ '硬停未生效时若子代理正在跑一个很长的工具调用,');
|
|
2886
|
+
lines.push('它会在该调用返回后的下一次分派才看到提示——请据此判断是否需要人工介入。');
|
|
2887
|
+
}
|
|
2888
|
+
return lines.join('\n')
|
|
2889
|
+
},
|
|
2890
|
+
}
|
|
2891
|
+
}
|
|
2892
|
+
|
|
2893
|
+
/**
|
|
2894
|
+
* 注册全部工具(【档位路由显式化 2026-11-27】新增第二参 config,向后兼容)。
|
|
2895
|
+
*
|
|
2896
|
+
* 第二参 config 是 apply(ctx, config) 的插件配置透传(可选;缺省 {} 行为不变):
|
|
2897
|
+
* · tierPolicy —— 档位策略(形状同 tier-policy 的 policy 表)。
|
|
2898
|
+
* 传给确认卡的模型档解析链:显式 provider+model 命中白名单才用,
|
|
2899
|
+
* 否则回退白名单关键词启发式(defaultTierPolicy() = 仅启发式)。
|
|
2900
|
+
* 其余工具目前不读配置;后续接线不需要再改本签名。
|
|
2901
|
+
*
|
|
2902
|
+
* @param {object} ctx cordis context
|
|
2903
|
+
* @param {object} [config] 插件配置(apply 第二参;可选)
|
|
2904
|
+
*/
|
|
2905
|
+
export function registerTools(ctx, config = {}) {
|
|
2906
|
+
ctx.tools.register(makeCheckTool(ctx))
|
|
2907
|
+
ctx.tools.register(makeConcurrencyTool(ctx, config?.tierPolicy))
|
|
2908
|
+
// V-INT(批次 2 / B2-1):打断阀门。与上面两个同款注册形态(手写 ToolDefinition、
|
|
2909
|
+
// 零依赖、ctx.tools.register),排在最后——既有两工具的顺序与索引不变,
|
|
2910
|
+
// 依赖 registeredTools[0] / [1] 的既有断言语义不受影响。
|
|
2911
|
+
ctx.tools.register(makeInterruptTool(ctx))
|
|
2912
|
+
}
|