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
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* plan-first 并发闸(【变更 2026-11-27 plan-first】;【变更 2026-11-28 NEW-1/W2-F-01】)
|
|
3
|
+
*
|
|
4
|
+
* 目的:并发派发必须先经 DSH 官方 plan mode —— 模型先 /plan 进入计划模式,
|
|
5
|
+
* 与用户确定计划后用 exit_plan_mode 提交,官方 Plan review 卡批准之后,
|
|
6
|
+
* suggest_concurrency 才按批准的计划路数放行(并顺带解锁既有并发闸)。
|
|
7
|
+
*
|
|
8
|
+
* 三个角色:
|
|
9
|
+
* · notePlanIntent / readPlanIntent —— suggest_concurrency 首次进入 plan 分支时
|
|
10
|
+
* 记录「本会话有计划意图(含推荐路数)」,TTL 内有效。**推荐路数只用于提示**:
|
|
11
|
+
* 绝不进入批准记录、绝不用于授权(NEW-1 硬纪律)。
|
|
12
|
+
* · installPlanGate —— 监听 tools/execute 上的 exit_plan_mode:宿主 Plan review
|
|
13
|
+
* 卡返回且 result.approved === true 且无错误时,从**计划正文**解析显式并发路数
|
|
14
|
+
* (解析不到 → 保守 1),写 planApprovals;计划正文显式 ≥ 2 路时同步
|
|
15
|
+
* markConcurrencyConfirmed(官方批准后按该上限放行)。
|
|
16
|
+
* · readPlanApproval / consumePlanApproval —— suggest_concurrency 下次调用读取批准
|
|
17
|
+
* 记录:有 → 按批准路数放行(读后即焚);无且仍在计划模式 → 拦下并引导先提交计划。
|
|
18
|
+
*
|
|
19
|
+
* 批准语义(【变更 2026-11-28 NEW-1 / W2-F-01】):
|
|
20
|
+
* · 只有宿主 exit_plan_mode 结果 **显式 approved === true**(且无 isError)才写批准;
|
|
21
|
+
* 用户取消(approved:false)/ Keep planning / 提交报错一律不写批准、不解锁并发;
|
|
22
|
+
* · 批准路数**只来自计划正文的显式路数**;解析不到 → 保守 1;
|
|
23
|
+
* 推荐路数(planIntents)只用于提示文案,**绝不参与授权**;
|
|
24
|
+
* · 计划显式串行/单路语义(「改为串行执行」「按单路执行」「只派 1 个」)→ lanes 1,
|
|
25
|
+
* 且不 markConcurrencyConfirmed;
|
|
26
|
+
* · 计划显式 ≥ 2 路 → 按批准路数 markConcurrencyConfirmed(与是否有 intent 无关)。
|
|
27
|
+
*
|
|
28
|
+
* 纪律:
|
|
29
|
+
* · 模块零 I/O(不读盘、不读 ctx、不碰网络);状态全在内存 Map;
|
|
30
|
+
* · 全部入口 try/catch 静默降级(可选服务一律 ctx.get?.() 同款纪律);
|
|
31
|
+
* · 会话键与 services.mjs 的 sessionKeyOf 同款归一化(agent.session 取 id / sessionId);
|
|
32
|
+
* · Map 容量上限(FIFO 淘汰),防长会话跨会话无限增长(与确认纪元 512 上限同族)。
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import { markConcurrencyConfirmed, downscaleConcurrencyConfirmation } from './gate.mjs'
|
|
36
|
+
|
|
37
|
+
/** 计划意图 / 计划批准的通用 TTL:30 分钟(会话内足够长,跨天必过期)。 */
|
|
38
|
+
const PLAN_GATE_TTL_MS = 30 * 60 * 1000
|
|
39
|
+
|
|
40
|
+
/** 两个 Map 的容量上限;超出按插入序淘汰最旧(FIFO,与确认纪元 512 同族)。 */
|
|
41
|
+
const PLAN_GATE_CAP = 256
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 会话键 → 计划意图 { lanes, at }。
|
|
45
|
+
* lanes:suggest_concurrency 当时推荐的并发路数。**只用于提示**(NEW-1:绝不作授权回退)。
|
|
46
|
+
* @type {Map<string, {lanes: number, at: number}>}
|
|
47
|
+
*/
|
|
48
|
+
const planIntents = new Map()
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 会话键 → 计划批准 { lanes, at, planText, lanesSource }。
|
|
52
|
+
* planText:exit_plan_mode 提交的计划原文(readPlanApproval 原样带回,不解析第二份)。
|
|
53
|
+
* lanesSource:路数来源('explicit' | 'serial' | 'unparsed'),供工具层批准文案披露
|
|
54
|
+
* 「计划正文未解析到路数 / 显式串行」用;**不含推荐路数本身**(NEW-1)。
|
|
55
|
+
* @type {Map<string, {lanes: number, at: number, planText: string, lanesSource: string}>}
|
|
56
|
+
*/
|
|
57
|
+
const planApprovals = new Map()
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 会话键归一化(与 services.mjs 的 sessionKeyOf 同款:agent.session 取 id / sessionId;
|
|
61
|
+
* 也直接接受字符串键,方便测试与 installPlanGate 的 exec.agent 直调)。
|
|
62
|
+
* @param {object|string} agentOrKey
|
|
63
|
+
* @returns {string|undefined}
|
|
64
|
+
*/
|
|
65
|
+
function sessionKeyOf(agentOrKey) {
|
|
66
|
+
if (typeof agentOrKey === 'string') return agentOrKey === '' ? undefined : agentOrKey
|
|
67
|
+
try {
|
|
68
|
+
const session = agentOrKey?.session
|
|
69
|
+
const id = session?.id ?? session?.sessionId
|
|
70
|
+
return typeof id === 'string' && id !== '' ? id : undefined
|
|
71
|
+
} catch {
|
|
72
|
+
return undefined
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 带容量上限的写入:超出上限时按插入序淘汰最旧(Map 迭代序即插入序)。 */
|
|
77
|
+
function rememberBounded(map, key, value) {
|
|
78
|
+
if (map.size >= PLAN_GATE_CAP && !map.has(key)) {
|
|
79
|
+
const oldest = map.keys().next()
|
|
80
|
+
if (!oldest.done) map.delete(oldest.value)
|
|
81
|
+
}
|
|
82
|
+
map.set(key, value)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 读取并顺手回收过期条目(TTL 过期 = 没写过)。 */
|
|
86
|
+
function readLive(map, key, now) {
|
|
87
|
+
const entry = map.get(key)
|
|
88
|
+
if (entry === undefined) return undefined
|
|
89
|
+
if (now - entry.at > PLAN_GATE_TTL_MS) {
|
|
90
|
+
map.delete(key)
|
|
91
|
+
return undefined
|
|
92
|
+
}
|
|
93
|
+
return entry
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 记录「本会话有并发计划意图」。suggest_concurrency 进入 plan 分支时调用;
|
|
98
|
+
* 失败静默(agent 无会话键 / 非法 lanes 照记为 0,读取侧只作提示)。
|
|
99
|
+
* **注意(NEW-1)**:本表的 lanes 是评分器推荐值,只用于 /plan 种子文案与提示,
|
|
100
|
+
* 绝不作为 planApprovals 的路数来源,也绝不参与 markConcurrencyConfirmed。
|
|
101
|
+
* @param {object|string} agent
|
|
102
|
+
* @param {number} [lanes] 建议并发路数(非有限数值回退 0)
|
|
103
|
+
* @param {number} [now] 时间戳(测试可注入固定时钟)
|
|
104
|
+
*/
|
|
105
|
+
export function notePlanIntent(agent, lanes, now = Date.now()) {
|
|
106
|
+
try {
|
|
107
|
+
const key = sessionKeyOf(agent)
|
|
108
|
+
if (key === undefined) return
|
|
109
|
+
rememberBounded(planIntents, key, { lanes: Number.isFinite(lanes) && lanes > 0 ? Math.floor(lanes) : 0, at: now })
|
|
110
|
+
} catch { /* 静默降级:意图记录失败绝不影响建议文本 */ }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 读取本会话的计划意图(未记录 / 已过期 / 无会话键 → undefined)。
|
|
115
|
+
* **@internal 测试/诊断专用**(生产只写不读;推荐值不参与授权)。
|
|
116
|
+
* @param {object|string} agent
|
|
117
|
+
* @param {number} [now] 时间戳(测试可注入固定时钟)
|
|
118
|
+
* @returns {{lanes: number, at: number}|undefined}
|
|
119
|
+
*/
|
|
120
|
+
export function readPlanIntent(agent, now = Date.now()) {
|
|
121
|
+
try {
|
|
122
|
+
const key = sessionKeyOf(agent)
|
|
123
|
+
if (key === undefined) return undefined
|
|
124
|
+
const entry = readLive(planIntents, key, now)
|
|
125
|
+
return entry === undefined ? undefined : { lanes: entry.lanes, at: entry.at }
|
|
126
|
+
} catch {
|
|
127
|
+
return undefined
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 读取本会话的计划批准(未批准 / 已过期 / 无会话键 → undefined)。
|
|
133
|
+
* **不消费**:消费走 consumePlanApproval(读后即焚,一次批准只放行一次并发)。
|
|
134
|
+
* @param {object|string} agent
|
|
135
|
+
* @param {number} [now] 时间戳(测试可注入固定时钟)
|
|
136
|
+
* @returns {{lanes: number, at: number, planText: string, lanesSource: string}|undefined}
|
|
137
|
+
*/
|
|
138
|
+
export function readPlanApproval(agent, now = Date.now()) {
|
|
139
|
+
try {
|
|
140
|
+
const key = sessionKeyOf(agent)
|
|
141
|
+
if (key === undefined) return undefined
|
|
142
|
+
const entry = readLive(planApprovals, key, now)
|
|
143
|
+
return entry === undefined ? undefined : {
|
|
144
|
+
lanes: entry.lanes,
|
|
145
|
+
at: entry.at,
|
|
146
|
+
planText: entry.planText,
|
|
147
|
+
lanesSource: entry.lanesSource,
|
|
148
|
+
}
|
|
149
|
+
} catch {
|
|
150
|
+
return undefined
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 消费本会话的计划批准(读后即焚):返回批准并删除记录,下次调用回 undefined。
|
|
156
|
+
* 一次计划批准只放行一轮并发派发;要再并发,重新 /plan 提交。
|
|
157
|
+
* @param {object|string} agent
|
|
158
|
+
* @param {number} [now] 时间戳(测试可注入固定时钟)
|
|
159
|
+
* @returns {{lanes: number, at: number, planText: string, lanesSource: string}|undefined}
|
|
160
|
+
*/
|
|
161
|
+
export function consumePlanApproval(agent, now = Date.now()) {
|
|
162
|
+
try {
|
|
163
|
+
const key = sessionKeyOf(agent)
|
|
164
|
+
if (key === undefined) return undefined
|
|
165
|
+
const entry = readLive(planApprovals, key, now)
|
|
166
|
+
if (entry === undefined) return undefined
|
|
167
|
+
planApprovals.delete(key)
|
|
168
|
+
return {
|
|
169
|
+
lanes: entry.lanes,
|
|
170
|
+
at: entry.at,
|
|
171
|
+
planText: entry.planText,
|
|
172
|
+
lanesSource: entry.lanesSource,
|
|
173
|
+
}
|
|
174
|
+
} catch {
|
|
175
|
+
return undefined
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/* ── 计划正文路数解析(【变更 2026-11-28 NEW-2】)──────────────────────────
|
|
180
|
+
*
|
|
181
|
+
* 旧实现只认「并发 N 路」「N 路并发」两种写法;实测「第一波 7 个并发」
|
|
182
|
+
* 「第一层 7 个工位并发」全部漏判 → 被降为 1 路(NEW-2)。
|
|
183
|
+
* 现按「数字紧邻并发/并行/路/波次」的宽松句式识别,覆盖:
|
|
184
|
+
* · 并发 N 路 / N 路并发 / 并发 N 个(工位)/ 并行 N 路
|
|
185
|
+
* · N 个(工位/子代理/任务)并发 | 并行
|
|
186
|
+
* · N 路(任意上文) / 第一波|批|层|轮 N 个
|
|
187
|
+
* · N 个工位同时|一起
|
|
188
|
+
* 并识别显式串行/单路语义(串行 / 单路 / 单线程 / 不并发 / 只派 1 个)→ 1 路。
|
|
189
|
+
* 语义:显式路数与串行语义同时出现时,以**后出现**者为准(「改为串行」是修正);
|
|
190
|
+
* 两处显式路数取第一处(既有 §2.3 契约)。纯数字相邻的「N 个文件」不算路数
|
|
191
|
+
* (文件数 ≠ 并发路数;宁可保守取 1,不可放大授权)。
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
/** 显式并发路数句式;每个正则的第 1 个捕获组是路数。 */
|
|
195
|
+
const EXPLICIT_LANE_PATTERNS = [
|
|
196
|
+
/并发\s*(\d+)\s*路/g,
|
|
197
|
+
/(\d+)\s*路并发/g,
|
|
198
|
+
/(?:并发|并行)\s*(\d+)\s*(?:个|名|位|条|支|路|线程|工位)?/g,
|
|
199
|
+
/(\d+)\s*(?:个|名|位|条|支|路)?\s*(?:工位|子代理|任务|线程)?\s*(?:并发|并行)/g,
|
|
200
|
+
/(\d+)\s*路/g,
|
|
201
|
+
/第[一二三四五六七八九十百\d]+(?:波|批|层|轮|梯队)\s*(\d+)\s*(?:个|名|位|条|支|路|工位)?/g,
|
|
202
|
+
/(\d+)\s*(?:个|名|位)\s*(?:工位|子代理|任务|agent)?\s*(?:同时|一起)/g,
|
|
203
|
+
]
|
|
204
|
+
|
|
205
|
+
/** 显式串行/单路语义句式(命中即「用户要串行」,路数只能 1)。 */
|
|
206
|
+
const SERIAL_PATTERNS = [
|
|
207
|
+
/串行/g,
|
|
208
|
+
/单路/g,
|
|
209
|
+
/单线程/g,
|
|
210
|
+
/(?:不要|不再|不|勿|无需|不必)\s*并发/g,
|
|
211
|
+
/只(?:派|开|起|用|跑|发)?\s*(?:1|1|一)\s*(?:个|路|位|名|条)/g,
|
|
212
|
+
]
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 找计划文本里第一处**有效**(N > 0)的显式路数。
|
|
216
|
+
* @param {string} planText
|
|
217
|
+
* @returns {{index: number, lanes: number}|undefined}
|
|
218
|
+
*/
|
|
219
|
+
function firstExplicitLane(planText) {
|
|
220
|
+
let best
|
|
221
|
+
for (const pattern of EXPLICIT_LANE_PATTERNS) {
|
|
222
|
+
for (const match of planText.matchAll(pattern)) {
|
|
223
|
+
const lanes = Number.parseInt(match[1], 10)
|
|
224
|
+
if (!Number.isFinite(lanes) || lanes <= 0) continue
|
|
225
|
+
if (best === undefined || match.index < best.index) best = { index: match.index, lanes }
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return best
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 找计划文本里第一处显式串行/单路语义的下标。
|
|
233
|
+
* @param {string} planText
|
|
234
|
+
* @returns {number|undefined}
|
|
235
|
+
*/
|
|
236
|
+
function firstSerialIndex(planText) {
|
|
237
|
+
let best
|
|
238
|
+
for (const pattern of SERIAL_PATTERNS) {
|
|
239
|
+
for (const match of planText.matchAll(pattern)) {
|
|
240
|
+
if (best === undefined || match.index < best) best = match.index
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return best
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 解析计划正文的路数意图(内部):返回 { lanes, source }。
|
|
248
|
+
* · source='explicit':显式 N 路(N > 0);
|
|
249
|
+
* · source='serial':显式串行/单路 → lanes 1;
|
|
250
|
+
* · source='unparsed':解析不到 → lanes undefined(调用方保守取 1)。
|
|
251
|
+
* @param {string} planText
|
|
252
|
+
* @returns {{lanes: number|undefined, source: 'explicit'|'serial'|'unparsed'}}
|
|
253
|
+
*/
|
|
254
|
+
function parsePlanLaneIntent(planText) {
|
|
255
|
+
try {
|
|
256
|
+
if (typeof planText !== 'string' || planText === '') return { lanes: undefined, source: 'unparsed' }
|
|
257
|
+
const explicit = firstExplicitLane(planText)
|
|
258
|
+
const serialAt = firstSerialIndex(planText)
|
|
259
|
+
// 显式路数出现在串行语义之后(如「先串行做 A,再并发 3 路做 B」)→ 以显式路数为准;
|
|
260
|
+
// 串行语义出现在显式路数之后(如「先并发 8 路,后来改为串行」)→ 以串行为准(1 路)。
|
|
261
|
+
if (explicit !== undefined && (serialAt === undefined || explicit.index > serialAt)) {
|
|
262
|
+
return { lanes: explicit.lanes, source: 'explicit' }
|
|
263
|
+
}
|
|
264
|
+
if (serialAt !== undefined) return { lanes: 1, source: 'serial' }
|
|
265
|
+
return { lanes: undefined, source: 'unparsed' }
|
|
266
|
+
} catch {
|
|
267
|
+
return { lanes: undefined, source: 'unparsed' }
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 从计划文本解析显式并发路数(NEW-2 覆盖表见文件头)。
|
|
273
|
+
* 找不到 / 非字符串 / 显式「0 路」→ undefined(调用方保守取 1,绝不回退推荐值);
|
|
274
|
+
* 显式串行/单路语义 → 1;多处出现取第一处。
|
|
275
|
+
* @param {string} planText
|
|
276
|
+
* @returns {number|undefined}
|
|
277
|
+
*/
|
|
278
|
+
export function inferLanesFromPlan(planText) {
|
|
279
|
+
return parsePlanLaneIntent(planText).lanes
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* 判定宿主 exit_plan_mode 的返回是否代表**用户已批准**(W2-F-01)。
|
|
284
|
+
*
|
|
285
|
+
* 宿主 dsh-plan-mode/lib/index.js:用户点 Approve 后 execute 返回 {approved:true};
|
|
286
|
+
* 用户取消(ASK_CANCELLED)返回 {approved:false};Keep planning 直接抛错。
|
|
287
|
+
* 本函数只认显式 approved === true 且无 isError;兼容结果包在 {value:{approved}} 的形态。
|
|
288
|
+
* 其余(undefined / 非对象 / approved:false / isError:true / 未知形状)一律 false——
|
|
289
|
+
* 批准语义宁可少写(fail-closed),绝不把用户取消当批准。
|
|
290
|
+
* @param {unknown} result
|
|
291
|
+
* @returns {boolean}
|
|
292
|
+
*/
|
|
293
|
+
function isApprovedPlanResult(result) {
|
|
294
|
+
try {
|
|
295
|
+
if (result === null || typeof result !== 'object') return false
|
|
296
|
+
if (result.isError === true) return false
|
|
297
|
+
const value = result.value
|
|
298
|
+
if (value !== null && typeof value === 'object' && value.isError === true) return false
|
|
299
|
+
const approved = result.approved !== undefined ? result.approved : value?.approved
|
|
300
|
+
return approved === true
|
|
301
|
+
} catch {
|
|
302
|
+
return false
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* 安装 plan-first 闸:在 tools/execute 瀑布上监听 exit_plan_mode 的提交结果。
|
|
308
|
+
*
|
|
309
|
+
* 时序(W2-F-04 修正):官方 /plan 进入计划模式 → 模型写计划 → exit_plan_mode 提交 →
|
|
310
|
+
* 宿主在 execute 内 await interaction.ask(...),**用户点 Approve 后才返回**
|
|
311
|
+
* {approved:true}(取消返回 {approved:false};Keep planning 抛错)。因此本监听器
|
|
312
|
+
* 在 next() 返回后读到的 result 已包含用户决定;tools.mjs 的批准分支也不再查 planMode
|
|
313
|
+
* 服务(PTC profile 下 planMode 可为 undefined),不存在「planMode 状态双闸」。
|
|
314
|
+
*
|
|
315
|
+
* 行为表(exec.name === 'exit_plan_mode' 且 next() 未抛错时):
|
|
316
|
+
* · result 非显式 approved === true(含 approved:false / isError)→ 不写批准、
|
|
317
|
+
* 不解锁并发、结果原样返回(W2-F-01);
|
|
318
|
+
* · lanes = 计划正文显式路数(NEW-2);解析不到 → 保守 1;
|
|
319
|
+
* **绝不回退 intent 的推荐路数**(NEW-1);显式串行/单路 → 1;
|
|
320
|
+
* · 写 planApprovals(带计划原文 + lanesSource 提示标记);
|
|
321
|
+
* · 计划正文显式 ≥ 2 → markConcurrencyConfirmed(agent, lanes)(W2-F-03:与 intent 无关);
|
|
322
|
+
* · 清 planIntents(意图已兑现);
|
|
323
|
+
* · 非 exit_plan_mode / 无会话键 / 任何异常 → 静默放行 result,绝不影响原结果。
|
|
324
|
+
*
|
|
325
|
+
* 返回卸载函数(本模块自身无监听器状态,卸载即由调用方摘除 ctx.on)。
|
|
326
|
+
* @param {object} ctx Cordis context
|
|
327
|
+
* @param {{now?: () => number}} [options] now:可注入时钟(W2-F-27,测试用)
|
|
328
|
+
* @returns {() => void} dispose
|
|
329
|
+
*/
|
|
330
|
+
export function installPlanGate(ctx, options = {}) {
|
|
331
|
+
const now = typeof options?.now === 'function' ? options.now : () => Date.now()
|
|
332
|
+
let off
|
|
333
|
+
try {
|
|
334
|
+
off = ctx.on?.('tools/execute', async (exec, next) => {
|
|
335
|
+
if (exec?.name !== 'exit_plan_mode') return next()
|
|
336
|
+
const result = await next()
|
|
337
|
+
try {
|
|
338
|
+
const key = sessionKeyOf(exec.agent)
|
|
339
|
+
if (!key) return result
|
|
340
|
+
if (!isApprovedPlanResult(result)) {
|
|
341
|
+
// 用户取消/提交失败:不写批准。若该会话还留着更早的未消费批准,一并作废
|
|
342
|
+
// (fail-closed:旧批准不能跨过一次被取消的新计划继续放行)。
|
|
343
|
+
try { planApprovals.delete(key) } catch { /* 静默 */ }
|
|
344
|
+
return result
|
|
345
|
+
}
|
|
346
|
+
const planText = String(exec.arguments?.plan ?? '')
|
|
347
|
+
// W2-F-02:计划意图的 TTL 在此刻真正生效(旧实现是裸 planIntents.get(key),过期条目
|
|
348
|
+
// 永不失效)。这里只做「过期即回收」,返回值**不参与任何授权判断**(NEW-1)。
|
|
349
|
+
readLive(planIntents, key, now())
|
|
350
|
+
const laneIntent = parsePlanLaneIntent(planText)
|
|
351
|
+
const lanes = laneIntent.lanes === undefined ? 1 : laneIntent.lanes
|
|
352
|
+
rememberBounded(planApprovals, key, {
|
|
353
|
+
lanes,
|
|
354
|
+
at: now(),
|
|
355
|
+
planText,
|
|
356
|
+
lanesSource: laneIntent.source,
|
|
357
|
+
})
|
|
358
|
+
// NEW-1:授权路数只来自计划正文;推荐值(intent.lanes)绝不参与授权。
|
|
359
|
+
// W2-F-03:计划正文显式 ≥2 路即按批准路数解锁(不再要求 intent 存在)。
|
|
360
|
+
if (laneIntent.source === 'explicit' && lanes >= 2) {
|
|
361
|
+
markConcurrencyConfirmed(exec.agent, lanes)
|
|
362
|
+
}
|
|
363
|
+
// NEW-1 残留(L3 闭环;cross-lane 改动由 lead 明确授权):串行/单路计划必须
|
|
364
|
+
// fail-closed 下调该会话既有并发确认 —— 先确认 8 路、之后计划「改为串行执行」
|
|
365
|
+
// → 本轮实际授权收紧到 1 路(只降不升,见 gate.downscaleConcurrencyConfirmation)。
|
|
366
|
+
if (laneIntent.source === 'serial') {
|
|
367
|
+
downscaleConcurrencyConfirmation(exec.agent, 1)
|
|
368
|
+
}
|
|
369
|
+
planIntents.delete(key)
|
|
370
|
+
} catch { /* 批准记录失败不影响提交结果原样返回 */ }
|
|
371
|
+
return result
|
|
372
|
+
})
|
|
373
|
+
} catch { /* ctx.on 缺失 / 抛错:plan-first 降级为不可用,插件加载不受影响 */ }
|
|
374
|
+
return () => {
|
|
375
|
+
try { if (typeof off === 'function') off() } catch { /* 已卸载的 off 抛错吞掉 */ }
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** 清空全部 plan-gate 内存状态。**@internal 仅供测试**。 */
|
|
380
|
+
export function __resetPlanGate() {
|
|
381
|
+
try {
|
|
382
|
+
planIntents.clear()
|
|
383
|
+
planApprovals.clear()
|
|
384
|
+
} catch { /* 静默 */ }
|
|
385
|
+
}
|