dsh-workplan 2.1.5 → 2.2.7
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/CHANGELOG.md +408 -1
- package/README.en.md +5 -2
- package/README.md +5 -2
- package/client.js +148 -3
- package/docs/dsh-workplan_T4_runtime.patch +508 -0
- package/docs/release-record-2.2.6-20261109-T5.md +64 -0
- package/docs/release-records/release-notes-2.2.6-T3.md +128 -0
- package/docs/release-records/release-steps-20261106.md +1 -1
- package/docs/release-records/version-freeze-20261109-T2.md +37 -0
- package/lib/client.js +148 -3
- package/lib/comm-format.js +17 -0
- package/lib/host.js +95 -17
- package/lib/notice-channel.js +290 -0
- package/lib/plan.js +668 -39
- package/lib/replan.js +35 -4
- package/lib/runtime.js +1918 -116
- package/lib/supersede.js +2 -2
- package/lib/understanding.js +8 -2
- package/lib/watchdog.js +52 -1
- package/package.json +3 -2
- package/scripts/repair-malformed-user-messages.mjs +312 -0
- package/src/client.ts +155 -2
- package/src/comm-format.ts +17 -0
- package/src/host.ts +90 -12
- package/src/notice-channel.ts +328 -0
- package/src/plan.ts +699 -30
- package/src/replan.ts +32 -1
- package/src/runtime.ts +1966 -118
- package/src/supersede.ts +2 -1
- package/src/understanding.ts +10 -2
- package/src/watchdog.ts +80 -1
- package/README.zh.md +0 -239
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [2.2.0][T3] 子→主「开工预告」通道(O-2a):目标解析、可达性观测与失败留痕。
|
|
3
|
+
*
|
|
4
|
+
* 缺陷(2026-11-08 真实环境复现):插件在部门子 Agent 的提示词里要求
|
|
5
|
+
* `send_message(target: "lead")` 报开工预告,但宿主工具契约的字段名是
|
|
6
|
+
* `agent_id`,且 `"lead"` 不是任何会话 id。真实派发中的结果只有一句
|
|
7
|
+
* `subagent "lead" is unavailable`——看起来像「通道坏了」,实际是「目标不存在」。
|
|
8
|
+
* 三段各自的「合理」组合成结构性断链:
|
|
9
|
+
*
|
|
10
|
+
* [0] 插件用 `delivery: 'caller'` 派发(src/runtime.ts executeSubtask)
|
|
11
|
+
* [1] 宿主只在 `spec.delivery !== "caller"` 时把父会话 id 追加进子提示词
|
|
12
|
+
* (host: dsh-subagent/lib/index.js:1466)
|
|
13
|
+
* [2] 插件却要求子 Agent 主动向 "lead" 回报(旧版 src/plan.ts 的 O-2a 段)
|
|
14
|
+
*
|
|
15
|
+
* 于是子 Agent 既不知道父会话 id,又被要求给父发消息,消息在目标解析阶段必然
|
|
16
|
+
* 失败,并且失败原因被误读成「通道不可用」。修法只有一条:**由插件注入真实父会话
|
|
17
|
+
* id**(`agent.id` / `session.id` / `plan.sessionId`),并让通道变成可观测的——
|
|
18
|
+
* 消息到底有没有在父会话日志里落地,由插件自己去看,而不是信任任何自述。
|
|
19
|
+
*
|
|
20
|
+
* 本模块只做纯函数,不依赖 harness 服务,也不 import 其它插件模块(避免环):
|
|
21
|
+
* - `resolveLeadAgentId`:解析要注入的父会话 id 及其来源(来源要能写进证据);
|
|
22
|
+
* - `observeRelayedNotice`:在父会话事件日志里找「该子会话转达过来的消息」;
|
|
23
|
+
* - `createNoticeChannel` / `finaliseNoticeChannel`:状态行上的通道条目;
|
|
24
|
+
* - `validateNoticeChannel`:写入前的形状校验(applyStatus 单点入口用);
|
|
25
|
+
* - `formatNoticeChannelLog`:失败日志(目标 / 调用方 / 时间戳 / 原因)。
|
|
26
|
+
*
|
|
27
|
+
* 安全边界(T5 会复核):目标 id 由插件注入,子 Agent 无权改写;本模块不提供
|
|
28
|
+
* 任何「按名字查会话」「任意 id 直发」的能力,所有可达性判断仍由宿主的
|
|
29
|
+
* lineage 校验(`authorizeLineage`)决定。
|
|
30
|
+
*/
|
|
31
|
+
/** 日志里「目标缺失」的固定措辞(与提示词里的分支保持一致,便于 grep 复核)。 */
|
|
32
|
+
export const NOTICE_TARGET_MISSING_TEXT = '(未注入)';
|
|
33
|
+
/** 通道条目在状态行上的字段名。 */
|
|
34
|
+
export const NOTICE_CHANNEL_FIELD = 'noticeChannel';
|
|
35
|
+
/** 通道状态:等待观测 → 已观测 / 未观测。 */
|
|
36
|
+
export const NOTICE_CHANNEL_STATES = Object.freeze(['awaiting', 'observed', 'not-observed']);
|
|
37
|
+
/** 未观测的原因码(写日志用,不允许自由文本冒充原因)。 */
|
|
38
|
+
export const NOTICE_CHANNEL_REASONS = Object.freeze([
|
|
39
|
+
/** 父会话日志里已经出现该子会话转达的消息(正面结果)。 */
|
|
40
|
+
'agent-message-relayed',
|
|
41
|
+
/** 扫了父会话日志但没有该子会话的转达消息。 */
|
|
42
|
+
'NOT_OBSERVED_IN_SESSION_LOG',
|
|
43
|
+
/** 拿不到父会话对象(无法扫描),只能如实记录「无法观测」。 */
|
|
44
|
+
'SESSION_UNAVAILABLE',
|
|
45
|
+
/** 目标 id 没能解析出来,提示词走的是「禁止猜测」分支。 */
|
|
46
|
+
'TARGET_NOT_INJECTED',
|
|
47
|
+
]);
|
|
48
|
+
/** 通道字段长度上限:状态行会进入模型可读面与 HTTP 载荷,不能让单条记录无限膨胀。 */
|
|
49
|
+
export const MAX_NOTICE_FIELD_CHARS = 200;
|
|
50
|
+
/** JS Date 可表示的最大毫秒值(8.64e15);超过它 `toISOString()` 会抛 RangeError。 */
|
|
51
|
+
export const MAX_NOTICE_TIMESTAMP = 8_640_000_000_000_000;
|
|
52
|
+
/**
|
|
53
|
+
* 读一个「像会话 id」的字符串。
|
|
54
|
+
*
|
|
55
|
+
* 不做格式白名单(宿主 id 前缀在版本间变过:根会话是 `session-<uuid>`,
|
|
56
|
+
* 子会话是裸 uuid),只拒绝空白与非字符串——编造一个形状对但内容错的 id
|
|
57
|
+
* 比留空更糟,因为子 Agent 会把错误归因到通道而不是目标。
|
|
58
|
+
*/
|
|
59
|
+
export function readLeadAgentId(value) {
|
|
60
|
+
return typeof value === 'string' && value.trim() !== '' ? value.trim() : '';
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 解析要注入子提示词的父会话 id。
|
|
64
|
+
*
|
|
65
|
+
* 优先级(每一级都必须能自证来源,写进 `targetSource`):
|
|
66
|
+
* 1. `agent.id` —— 宿主传入的活 Agent 身份,也是宿主 sendMessage 里
|
|
67
|
+
* `activation.parent.id === targetId` 比较的同一个值,等价性最高;
|
|
68
|
+
* 2. `options.session.id` —— 本轮会话。
|
|
69
|
+
*
|
|
70
|
+
* [2.2.1][T5][F4] 原第 3 级 `record.plan.sessionId` 已删除:它只是「计划创建
|
|
71
|
+
* 时落盘的会话 id」,在 fork / 重启后换会话 / 迁移时可能指向另一个 root 会话,
|
|
72
|
+
* 把它当发送目标等于把子 Agent 的预告投到别人的会话(越权投递面)。计划正文
|
|
73
|
+
* 是不可信输入,不能参与「发给谁」的决策;两级都拿不到时返回空串,调用方必须
|
|
74
|
+
* 走「禁止猜测」分支,而不是拼一个 id。
|
|
75
|
+
*/
|
|
76
|
+
export function resolveLeadAgentId(input = {}) {
|
|
77
|
+
const agentId = readLeadAgentId(input.agent?.id);
|
|
78
|
+
if (agentId)
|
|
79
|
+
return { id: agentId, source: 'agent.id' };
|
|
80
|
+
const sessionId = readLeadAgentId(input.session?.id);
|
|
81
|
+
if (sessionId)
|
|
82
|
+
return { id: sessionId, source: 'session.id' };
|
|
83
|
+
return { id: '', source: 'TARGET_NOT_INJECTED' };
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 从一条会话事件里读出「转达者会话 id」。
|
|
87
|
+
*
|
|
88
|
+
* 真实事件形状(2026-11-08 从父会话日志实采,seq 2006):
|
|
89
|
+
* {"type":"agent/inbox/spliced","data":{"target":"next-step","inserted":[
|
|
90
|
+
* {"content":[...],"source":{"kind":"agent-message","form":"relay",
|
|
91
|
+
* "senderSessionId":"3759600a-…"}}]}}
|
|
92
|
+
* 另外兼容宿主在投递落地后可能产生的 `user/message`(`data.message.source`
|
|
93
|
+
* 或 `data.source` 上带同样的 source)。两处都认,避免把一次版本升级当成
|
|
94
|
+
* 「通道又不通了」。
|
|
95
|
+
*/
|
|
96
|
+
export function relayedSenderId(event) {
|
|
97
|
+
if (!event || typeof event !== 'object')
|
|
98
|
+
return '';
|
|
99
|
+
const data = event.data;
|
|
100
|
+
if (Array.isArray(data?.inserted)) {
|
|
101
|
+
for (const item of data.inserted) {
|
|
102
|
+
const sender = readLeadAgentId(item?.source?.senderSessionId);
|
|
103
|
+
if (sender && item?.source?.kind === 'agent-message')
|
|
104
|
+
return sender;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (data && typeof data === 'object') {
|
|
108
|
+
const message = data.message ?? data;
|
|
109
|
+
const sender = readLeadAgentId(message?.source?.senderSessionId);
|
|
110
|
+
if (sender && message?.source?.kind === 'agent-message')
|
|
111
|
+
return sender;
|
|
112
|
+
}
|
|
113
|
+
return '';
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 在父会话事件日志里找该子会话转达过来的消息(最新一条)。
|
|
117
|
+
*
|
|
118
|
+
* 只读、只回看:从日志尾部向前扫,遇到早于 `sinceMs` 的事件就停(消息不可能
|
|
119
|
+
* 平白出现在派发之前),扫描条数另有上限,杜绝一次异常日志把派发线程拖住。
|
|
120
|
+
*
|
|
121
|
+
* @returns 命中时 `{at, seq, eventType}`,未命中返回 null。
|
|
122
|
+
*/
|
|
123
|
+
export function observeRelayedNotice(session, options = {}) {
|
|
124
|
+
const childId = readLeadAgentId(options.childId);
|
|
125
|
+
if (!childId)
|
|
126
|
+
return null;
|
|
127
|
+
if (!session || typeof session.eventAt !== 'function')
|
|
128
|
+
return null;
|
|
129
|
+
const total = Number(session.seq);
|
|
130
|
+
if (!Number.isFinite(total) || total <= 0)
|
|
131
|
+
return null;
|
|
132
|
+
const sinceMs = Number.isFinite(Number(options.sinceMs)) ? Number(options.sinceMs) : 0;
|
|
133
|
+
const maxScan = Number.isFinite(Number(options.maxScan)) ? Math.max(1, Math.floor(Number(options.maxScan))) : 3000;
|
|
134
|
+
const last = Math.min(Math.floor(total), 1_000_000) - 1;
|
|
135
|
+
let scanned = 0;
|
|
136
|
+
for (let seq = last; seq >= 0 && scanned < maxScan; seq -= 1) {
|
|
137
|
+
const event = session.eventAt(seq);
|
|
138
|
+
scanned += 1;
|
|
139
|
+
if (!event)
|
|
140
|
+
continue;
|
|
141
|
+
const time = Number(event.time);
|
|
142
|
+
if (sinceMs > 0 && Number.isFinite(time) && time > 0 && time < sinceMs)
|
|
143
|
+
break;
|
|
144
|
+
if (relayedSenderId(event) !== childId)
|
|
145
|
+
continue;
|
|
146
|
+
return { at: Number.isFinite(time) && time > 0 ? time : Date.now(), seq, eventType: String(event.type ?? '') };
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
/** 派发时刻写下的初始通道条目(还不知道消息会不会来)。 */
|
|
151
|
+
export function createNoticeChannel(input) {
|
|
152
|
+
const target = readLeadAgentId(input.target);
|
|
153
|
+
const at = Number.isFinite(Number(input.at)) ? Number(input.at) : Date.now();
|
|
154
|
+
return {
|
|
155
|
+
state: 'awaiting',
|
|
156
|
+
target: target || null,
|
|
157
|
+
targetSource: typeof input.targetSource === 'string' && input.targetSource ? input.targetSource : 'none',
|
|
158
|
+
caller: readLeadAgentId(input.caller) || null,
|
|
159
|
+
at,
|
|
160
|
+
observedAt: null,
|
|
161
|
+
reason: target ? null : 'TARGET_NOT_INJECTED',
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* 结算时把通道条目收束成「已观测 / 未观测」。
|
|
166
|
+
*
|
|
167
|
+
* `observed` 只来自父会话日志的实证(`observeRelayedNotice`);拿不到会话对象
|
|
168
|
+
* 时写 `SESSION_UNAVAILABLE`,绝不用「没看到」冒充「没有发」。
|
|
169
|
+
*/
|
|
170
|
+
export function finaliseNoticeChannel(current, outcome = {}) {
|
|
171
|
+
const base = createNoticeChannel({
|
|
172
|
+
target: current?.target,
|
|
173
|
+
targetSource: current?.targetSource,
|
|
174
|
+
caller: current?.caller,
|
|
175
|
+
at: current?.at,
|
|
176
|
+
});
|
|
177
|
+
const now = Number.isFinite(Number(outcome.now)) ? Number(outcome.now) : Date.now();
|
|
178
|
+
if (outcome.sessionAvailable !== true) {
|
|
179
|
+
return { ...base, state: 'not-observed', observedAt: null, reason: 'SESSION_UNAVAILABLE' };
|
|
180
|
+
}
|
|
181
|
+
if (outcome.observed === true) {
|
|
182
|
+
const observedAt = Number.isFinite(Number(outcome.observedAt)) ? Number(outcome.observedAt) : now;
|
|
183
|
+
return { ...base, state: 'observed', observedAt, reason: 'agent-message-relayed' };
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
...base,
|
|
187
|
+
state: 'not-observed',
|
|
188
|
+
observedAt: null,
|
|
189
|
+
reason: base.target ? 'NOT_OBSERVED_IN_SESSION_LOG' : 'TARGET_NOT_INJECTED',
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
/** 写入前的形状校验:未知状态/原因一律拒绝,避免脏值悄悄进状态行。 */
|
|
193
|
+
export function validateNoticeChannel(value) {
|
|
194
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
195
|
+
return { ok: false, error: 'noticeChannel 必须是对象' };
|
|
196
|
+
}
|
|
197
|
+
const source = value;
|
|
198
|
+
if (!NOTICE_CHANNEL_STATES.includes(source.state)) {
|
|
199
|
+
return { ok: false, error: `noticeChannel.state 必须是 ${NOTICE_CHANNEL_STATES.join(' / ')}:${String(source.state)}` };
|
|
200
|
+
}
|
|
201
|
+
const target = source.target === null ? '' : readLeadAgentId(source.target);
|
|
202
|
+
if (source.target !== null && source.target !== undefined && !target) {
|
|
203
|
+
return { ok: false, error: 'noticeChannel.target 必须是非空字符串或 null' };
|
|
204
|
+
}
|
|
205
|
+
if (source.reason !== null && source.reason !== undefined && !NOTICE_CHANNEL_REASONS.includes(source.reason)) {
|
|
206
|
+
return { ok: false, error: `noticeChannel.reason 非法:${String(source.reason)}` };
|
|
207
|
+
}
|
|
208
|
+
const at = Number(source.at);
|
|
209
|
+
if (!Number.isFinite(at) || at <= 0)
|
|
210
|
+
return { ok: false, error: 'noticeChannel.at 必须是正数毫秒时间戳' };
|
|
211
|
+
const observedAt = source.observedAt === null || source.observedAt === undefined ? null : Number(source.observedAt);
|
|
212
|
+
if (observedAt !== null && (!Number.isFinite(observedAt) || observedAt <= 0)) {
|
|
213
|
+
return { ok: false, error: 'noticeChannel.observedAt 必须是正数毫秒时间戳或 null' };
|
|
214
|
+
}
|
|
215
|
+
if (at > MAX_NOTICE_TIMESTAMP) {
|
|
216
|
+
return { ok: false, error: 'noticeChannel.at 超出可表示范围' };
|
|
217
|
+
}
|
|
218
|
+
if (observedAt !== null && observedAt > MAX_NOTICE_TIMESTAMP) {
|
|
219
|
+
return { ok: false, error: 'noticeChannel.observedAt 超出可表示范围' };
|
|
220
|
+
}
|
|
221
|
+
if (target && target.length > MAX_NOTICE_FIELD_CHARS) {
|
|
222
|
+
return { ok: false, error: `noticeChannel.target 超过 ${MAX_NOTICE_FIELD_CHARS} 字符` };
|
|
223
|
+
}
|
|
224
|
+
const reason = source.reason ?? null;
|
|
225
|
+
const state = source.state;
|
|
226
|
+
/*
|
|
227
|
+
* 跨字段一致性(F2):状态、原因码与观测时间必须能互相解释,不允许
|
|
228
|
+
* 「observed 但没有到达时间」「awaiting 却已有 observedAt」这类自相矛盾记录。
|
|
229
|
+
* 这里 fail-closed:拒绝比展示一条自相矛盾的证据更安全。
|
|
230
|
+
*/
|
|
231
|
+
if (state === 'observed') {
|
|
232
|
+
if (reason !== 'agent-message-relayed' || observedAt === null || observedAt <= 0) {
|
|
233
|
+
return { ok: false, error: 'noticeChannel: state=observed 必须 reason=agent-message-relayed 且 observedAt>0' };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
else if (state === 'awaiting') {
|
|
237
|
+
if (observedAt !== null || (reason !== null && reason !== 'TARGET_NOT_INJECTED')) {
|
|
238
|
+
return { ok: false, error: 'noticeChannel: state=awaiting 不得带 observedAt,reason 只能是 null/TARGET_NOT_INJECTED' };
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
else if (state === 'not-observed') {
|
|
242
|
+
if (observedAt !== null) {
|
|
243
|
+
return { ok: false, error: 'noticeChannel: state=not-observed 不得带 observedAt' };
|
|
244
|
+
}
|
|
245
|
+
if (!['NOT_OBSERVED_IN_SESSION_LOG', 'SESSION_UNAVAILABLE', 'TARGET_NOT_INJECTED'].includes(String(reason))) {
|
|
246
|
+
return { ok: false, error: 'noticeChannel: state=not-observed 的 reason 非法' };
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
ok: true,
|
|
251
|
+
value: {
|
|
252
|
+
state,
|
|
253
|
+
target: target || null,
|
|
254
|
+
targetSource: typeof source.targetSource === 'string' ? source.targetSource.slice(0, MAX_NOTICE_FIELD_CHARS) : 'none',
|
|
255
|
+
caller: readLeadAgentId(source.caller) ? readLeadAgentId(source.caller).slice(0, MAX_NOTICE_FIELD_CHARS) : null,
|
|
256
|
+
at,
|
|
257
|
+
observedAt,
|
|
258
|
+
reason,
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* 通道失败日志:目标 / 调用方 / 时间戳 / 原因,四项缺一不可。
|
|
264
|
+
*
|
|
265
|
+
* 这条日志是「不静默丢失」的可复核凭据:主 Agent 读计划记录时能直接看到
|
|
266
|
+
* 该子任务的预告到底有没有到、到不了是哪种原因,而不是只看到一句
|
|
267
|
+
* `subagent "lead" is unavailable` 便无从下手。
|
|
268
|
+
*/
|
|
269
|
+
export function formatNoticeChannelLog(input) {
|
|
270
|
+
const subtaskId = typeof input.subtaskId === 'string' && input.subtaskId ? input.subtaskId : '?';
|
|
271
|
+
const target = readLeadAgentId(input.target) || NOTICE_TARGET_MISSING_TEXT;
|
|
272
|
+
const caller = readLeadAgentId(input.caller) || 'pending-child';
|
|
273
|
+
const rawAt = Number.isFinite(Number(input.at)) && Number(input.at) > 0 ? Number(input.at) : Date.now();
|
|
274
|
+
// 超出 Date 可表示范围的值直接回退当前时间,避免日志函数自己抛 RangeError。
|
|
275
|
+
const at = rawAt <= MAX_NOTICE_TIMESTAMP ? rawAt : Date.now();
|
|
276
|
+
const reason = typeof input.reason === 'string' && input.reason ? input.reason : 'NOT_OBSERVED_IN_SESSION_LOG';
|
|
277
|
+
return `${subtaskId} noticeChannel: target=${target} caller=${caller} at=${new Date(at).toISOString()} reason=${reason}`;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* 提示词里要子 Agent 照抄的调用形式。
|
|
281
|
+
*
|
|
282
|
+
* **唯一来源**:提示词正文、回归测试断言、人工复核都从这里取,避免三处各写
|
|
283
|
+
* 一遍后再次漂移(本次缺陷就是「提示词写的调用形式」与「宿主 schema」漂移)。
|
|
284
|
+
*/
|
|
285
|
+
export function leadNoticeCallForm(leadAgentId) {
|
|
286
|
+
const id = readLeadAgentId(leadAgentId);
|
|
287
|
+
return id
|
|
288
|
+
? `send_message({ agent_id: ${JSON.stringify(id)}, message: "…" })`
|
|
289
|
+
: 'send_message({ agent_id: "<父会话 ID:本提示词未注入>", message: "…" })';
|
|
290
|
+
}
|