@suifei/dsh-ai-team 0.4.89 → 0.4.91
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 +61 -0
- package/index.js +107 -49
- package/lib/diaglog.js +145 -143
- package/lib/host/member-tools.js +317 -307
- package/lib/host/service.js +5 -0
- package/lib/host/waker.js +371 -359
- package/lib/ledger/fold.js +60 -4
- package/lib/runtime/host-agent.js +39 -61
- package/package.json +1 -1
package/lib/host/waker.js
CHANGED
|
@@ -1,359 +1,371 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Event-driven wake planner. No timers: a command produces at most one plan,
|
|
3
|
-
* and the Host delivers it with `subagents.sendMessage`/MemberRuntime.wake.
|
|
4
|
-
*
|
|
5
|
-
* The planner is pure; it returns targets and a short prompt. Delivery,
|
|
6
|
-
* deduplication and permission checks stay in the Host.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { eligibleReviewers, isQaMember } from '../domain/member-profile.js';
|
|
10
|
-
import { isReviewCandidate } from '../domain/policy.js';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
return
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
//
|
|
113
|
-
//
|
|
114
|
-
|
|
115
|
-
targets.
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
//
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
if (
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
);
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
//
|
|
289
|
-
//
|
|
290
|
-
const
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Event-driven wake planner. No timers: a command produces at most one plan,
|
|
3
|
+
* and the Host delivers it with `subagents.sendMessage`/MemberRuntime.wake.
|
|
4
|
+
*
|
|
5
|
+
* The planner is pure; it returns targets and a short prompt. Delivery,
|
|
6
|
+
* deduplication and permission checks stay in the Host.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { eligibleReviewers, isQaMember, unfinishedTasksFor } from '../domain/member-profile.js';
|
|
10
|
+
import { isReviewCandidate } from '../domain/policy.js';
|
|
11
|
+
import { isBoardIdle } from '../ledger/fold.js';
|
|
12
|
+
|
|
13
|
+
function text(value) {
|
|
14
|
+
return typeof value === 'string' ? value : '';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function agentMember(state, memberId) {
|
|
18
|
+
if (typeof memberId !== 'string') return null;
|
|
19
|
+
const member = state.members?.[memberId];
|
|
20
|
+
if (member === undefined || member.kind !== 'agent') return null;
|
|
21
|
+
// failed 成员不自动唤醒(其 LLM 正在失败,自动唤醒只会烧预算):
|
|
22
|
+
// 恢复走 member.retry(Human/Project owner 显式动作)或 Human 在成员
|
|
23
|
+
// 资料里的「重试」;review 路径另经 eligibleReviewers 的同一排除。
|
|
24
|
+
if (member.status === 'retired' || member.status === 'failed') return null;
|
|
25
|
+
return member;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function taskTitle(state, taskId) {
|
|
29
|
+
const task = state.tasks?.[taskId];
|
|
30
|
+
if (task === undefined) return taskId;
|
|
31
|
+
return `#${task.number ?? '?'} ${task.title ?? ''}`.trim();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function promptFor(reason, state, taskId) {
|
|
35
|
+
const task = state.tasks?.[taskId];
|
|
36
|
+
const title = taskTitle(state, taskId);
|
|
37
|
+
if (reason === 'task_created') {
|
|
38
|
+
return `Team event: ${title} 已创建,你是 accountable owner。调用 team 工具查看 tasks,认领/推进它;需要别人做时写清结果定义。完成后提交证据。`;
|
|
39
|
+
}
|
|
40
|
+
if (reason === 'task_assigned') {
|
|
41
|
+
return `Team event: ${title} 已分派给你。调用 team 工具查看 tasks,认领后推进;完成后 submit 证据。`;
|
|
42
|
+
}
|
|
43
|
+
if (reason === 'task_submitted') {
|
|
44
|
+
return `Team event: ${title} 已提交待验收。调用 team 工具查看 thread/tasks;若你不是执行者且从未认领,可在合格时验收;否则把问题写回 Thread。`;
|
|
45
|
+
}
|
|
46
|
+
if (reason === 'review_requested') {
|
|
47
|
+
return `Team event: ${title} 已提交待验收,你是独立 reviewer。调用 team 工具读 thread 与证据;若你从未认领该卡,请验收或写明返工理由。QA 角色优先复跑门禁。`;
|
|
48
|
+
}
|
|
49
|
+
if (reason === 'reflection_requested') {
|
|
50
|
+
return `Team event: ${title} 已被验收完成。做一次简短复盘:哪一步做得好、哪一步下次要改;如果你发现了持久的工作方式改进,可以调用 team 工具 evolve 更新自己的 personality(只能改个性,不改名字/角色)。不要重开已完成的卡。`;
|
|
51
|
+
}
|
|
52
|
+
if (reason === 'member_message') {
|
|
53
|
+
return 'Human 给你发了一条私信。先读消息,再用 team 工具按当前 tasks/thread 行动;不要把它当成新 Task。';
|
|
54
|
+
}
|
|
55
|
+
if (reason === 'startup_resume') {
|
|
56
|
+
return `Team event: DSH 已重启,${title} 仍未完成。调用 team 工具查看 tasks/thread,从当前状态继续;不要重新创建同一张卡。`;
|
|
57
|
+
}
|
|
58
|
+
if (reason === 'thread_reply') {
|
|
59
|
+
return `Team event: ${title} 的 Thread 有新回复。先调用 team 工具读 thread(记录 headSeq),再决定是否回复或推进;不要对已变化的 Thread 直接回复。`;
|
|
60
|
+
}
|
|
61
|
+
if (reason === 'changes_requested') {
|
|
62
|
+
return `Team event: ${title} 被返工。读 thread 的返工理由,修正后重新 submit;不要只回复“收到”。`;
|
|
63
|
+
}
|
|
64
|
+
if (reason === 'task_reopened') {
|
|
65
|
+
return `Team event: ${title} 被重开。调用 team 工具查看当前状态与 thread,按 owner/Human 的理由继续。`;
|
|
66
|
+
}
|
|
67
|
+
if (reason === 'inbox') {
|
|
68
|
+
return 'Team event: 你的收件箱有新条目。调用 team 工具查看 inbox,处理与你有关的事项。';
|
|
69
|
+
}
|
|
70
|
+
if (reason === 'pmo_dispatch') {
|
|
71
|
+
return `PMO 补派:${title} 仍处于待处理且你当前空闲(可能此前的唤醒未送达)。调用 team 工具认领(claim)它并立即推进;若它不再由你负责或无法推进,用 task.reassign 交还并说明原因;不要静默等待。`;
|
|
72
|
+
}
|
|
73
|
+
if (reason === 'pmo_review') {
|
|
74
|
+
return `PMO 验收提醒:${title} 仍处于 pending_acceptance,QA checklist 还没有完成记录。请独立复跑并按 resultDefinition 验收或退回;不要静默等待,也不要把验收推回 Human。`;
|
|
75
|
+
}
|
|
76
|
+
if (reason === 'pmo_escalate') {
|
|
77
|
+
return `PMO 升级:${title} 已超过 60 分钟仍在进行。请 owner 评估是否需要拆分 Task、补充人手、缩小首版范围或重定验收标准;不要只催进度,给出可执行决定。`;
|
|
78
|
+
}
|
|
79
|
+
if (reason === 'pmo_heartbeat') {
|
|
80
|
+
return `PMO 心跳:${title} 已 10 分钟没有 Thread 更新。请在 Thread 发一条简短进度(已完成 / 正在做 / 下一步 / 卡点),不要求现在完成;让 Human 能看到你确实在工作。`;
|
|
81
|
+
}
|
|
82
|
+
if (reason === 'pmo_stall') {
|
|
83
|
+
return `PMO 巡检:${title} 已长时间没有新进展。读 Thread 当前状态,继续推进或明确报告卡点;不要静默等待,也不要重复已完成的证据。`;
|
|
84
|
+
}
|
|
85
|
+
if (reason === 'pmo_meeting') {
|
|
86
|
+
return 'PMO 会议进行中,你是正式参会成员。读取已完成 Task、Thread 与项目目标,从你的专业视角(UX/业务/执行/验收/编程)在 Thread 发表意见并 @owner;指出被忽略的用户体验、运营需求或风险,不重复他人结论,不自行开卡,不等待 Human。';
|
|
87
|
+
}
|
|
88
|
+
if (reason === 'wave_done') {
|
|
89
|
+
return `Team event: 本批任务已全部验收(最后一张:${title}),看板已空。你是 PMO:用 team action=say 在主对话向 Human 汇报结果——完成了什么、产物在哪(路径)、怎么验证(命令与结果),并用 taskIds 附上这批卡。只汇报,不要在本回合续开新任务;是否继续由 PMO 推进节奏与 Human 决定。`;
|
|
90
|
+
}
|
|
91
|
+
if (reason === 'pmo_continue') {
|
|
92
|
+
return 'PMO 自动推进:当前没有 active Task。先对照 Human 最近一次的指令判断目标是否已经达成:已达成且没有明确的后续价值 → 用 team action=say 在主对话简短说明「已完成、暂无后续」,然后结束本回合,不开新卡。确有必要继续时,先开一次复盘会议(读取已完成 Task、Thread 与核心 skill;在 Thread/@ 相关成员形成意见),据此产出验收标准与下一批 Task,并用 action=say 告诉 Human 新一批做什么、为什么;resultDefinition 必须可验证,qaRequired 必须有 qaChecklist。不要重复已完成卡,不要扩大到 Human 没要求的范围。自主续开有批次上限,用尽后 PMO 会暂停等 Human。';
|
|
93
|
+
}
|
|
94
|
+
return `Team event: ${task?.title ?? taskId}。调用 team 工具查看 tasks/thread 后行动。`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @param {any} state ledger projection
|
|
99
|
+
* @param {{ event?: {type:string,payload?:any,seq?:number}, task?: any, thread?: any, item?: any }} result
|
|
100
|
+
* @param {{ agentId?: string }} actor
|
|
101
|
+
* @returns {Array<{memberId:string, taskId:string|null, reason:string, prompt:string, key:string}>}
|
|
102
|
+
*/
|
|
103
|
+
export function planWakes(state, result, actor = {}, options = {}) {
|
|
104
|
+
const event = result?.event ?? null;
|
|
105
|
+
const task = result?.task ?? null;
|
|
106
|
+
const targets = new Map();
|
|
107
|
+
const add = (memberId, taskId, reason, key, promptOverride) => {
|
|
108
|
+
const member = agentMember(state, memberId);
|
|
109
|
+
if (member === null) return;
|
|
110
|
+
// A thread author must not be woken by their own reply, and the reviewer
|
|
111
|
+
// candidate is already filtered by eligibility. Other Agent-authored
|
|
112
|
+
// events (notably a Lead creating a task owned by itself) still need a
|
|
113
|
+
// wake so the same member can claim it.
|
|
114
|
+
if (actor.agentId !== undefined && memberId === actor.agentId && (reason === 'thread_reply' || reason === 'review_requested')) return;
|
|
115
|
+
if (targets.has(memberId + ':' + reason)) return;
|
|
116
|
+
// 去重键必须带上接收者与 Project:`wakeKeys` 是 Host 级全局表,同一 event
|
|
117
|
+
// seq 在多个 Project 里会重复,两个接收者共用键会让第二个计划被静默丢弃。
|
|
118
|
+
const scopedKey = `${key}:${memberId}:${options.projectId ?? ''}`;
|
|
119
|
+
targets.set(memberId + ':' + reason, {
|
|
120
|
+
memberId,
|
|
121
|
+
taskId: taskId ?? null,
|
|
122
|
+
reason,
|
|
123
|
+
prompt: (promptOverride ?? promptFor(reason, state, taskId)) + (taskId ? ` Task id: ${taskId}.` : ''),
|
|
124
|
+
key: scopedKey,
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// 路由变更(成员 provider/model 改过)必须立即唤醒并重建会话:
|
|
129
|
+
// * 已存活会话的 provider 在创建时固定,追加 model/selection 只改模型名,
|
|
130
|
+
// 实机会表现成「模型换成 Qwen、provider 仍是 deepseek-official → 缺 key」;
|
|
131
|
+
// * 失败成员被 agentMember() 排除在自动唤醒之外(避免烧预算),但 Human 改
|
|
132
|
+
// 模型就是明确的修复动作,必须放行,否则改完仍然卡死。
|
|
133
|
+
if (event?.type === 'member/config-updated' && event.payload?.routeChanged === true) {
|
|
134
|
+
const memberId = event.payload.memberId;
|
|
135
|
+
const member = typeof memberId === 'string' ? state.members?.[memberId] : undefined;
|
|
136
|
+
// 只唤醒手上有未完成任务的成员:fold 已把 routeStale 记进账本,空闲成员下次被
|
|
137
|
+
// 真正唤醒时自然在新路由下重建。否则改一次全员模型就空转激活 N 个会话(r08:6 次)。
|
|
138
|
+
if (member !== undefined && member.kind === 'agent' && member.status !== 'retired' && unfinishedTasksFor(state, memberId).length > 0) {
|
|
139
|
+
targets.set(`${memberId}:route_changed`, {
|
|
140
|
+
memberId,
|
|
141
|
+
taskId: null,
|
|
142
|
+
reason: 'route_changed',
|
|
143
|
+
prompt: '模型/提供方已切换。按新配置继续:先读 tasks 与 thread 的当前状态,不要重复已完成的工作。',
|
|
144
|
+
key: `route_changed:${event.seq ?? memberId}:${memberId}:${options.projectId ?? ''}`,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (event?.type === 'team/startup') { const unfinished = Object.values(state.tasks ?? {})
|
|
149
|
+
.filter((entry) => entry.tombstone !== true && ['pending', 'in_progress', 'pending_acceptance'].includes(entry.status))
|
|
150
|
+
.slice(0, 5);
|
|
151
|
+
for (const entry of unfinished) {
|
|
152
|
+
add(entry.ownerId, entry.id, 'startup_resume', `startup:${entry.id}`);
|
|
153
|
+
add(entry.assigneeId, entry.id, 'startup_resume', `startup:${entry.id}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (event?.type === 'member/message') {
|
|
157
|
+
add(
|
|
158
|
+
event.payload?.memberId,
|
|
159
|
+
null,
|
|
160
|
+
'member_message',
|
|
161
|
+
`member_message:${event.seq ?? event.payload?.memberId}`,
|
|
162
|
+
`Human 给你发私信:“${String(event.payload?.body ?? '').slice(0, 1200)}”。先读上下文,再按当前 tasks/thread 行动。`,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
if (event?.type === 'task/created' || (event === null && task !== null && task.status === 'pending')) {
|
|
166
|
+
// 账本事件路径没有嵌套 task:payload 只有 taskId,必须回投影里取。
|
|
167
|
+
const created = task ?? (event?.payload?.taskId === undefined ? null : state.tasks?.[event.payload.taskId]) ?? null;
|
|
168
|
+
if (created !== null && created !== undefined) {
|
|
169
|
+
add(created.ownerId, created.id, 'task_created', `task_created:${created.id}`);
|
|
170
|
+
add(created.assigneeId, created.id, 'task_assigned', `task_assigned:${created.id}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (event?.type === 'task/assigned') {
|
|
174
|
+
add(event.payload?.assigneeId, event.payload?.taskId, 'task_assigned', `task_assigned:${event.seq ?? event.payload?.taskId}`);
|
|
175
|
+
}
|
|
176
|
+
if (event?.type === 'task/submitted') {
|
|
177
|
+
const current = state.tasks?.[event.payload?.taskId];
|
|
178
|
+
const reviewer = eligibleReviewers(state, current)[0];
|
|
179
|
+
if (reviewer !== undefined) {
|
|
180
|
+
add(reviewer.id, event.payload?.taskId, 'review_requested', `review_requested:${event.seq ?? event.payload?.taskId}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (event?.type === 'task/accepted' || event?.type === 'review/accepted') {
|
|
184
|
+
const current = state.tasks?.[event.payload?.taskId];
|
|
185
|
+
if (current !== undefined && current !== null) {
|
|
186
|
+
// One reflection per Task, independent of whether the HTTP result or
|
|
187
|
+
// the ledger event scheduled it (they carry different seq context).
|
|
188
|
+
add(current.assigneeId, current.id, 'reflection_requested', `reflection:${current.id}`);
|
|
189
|
+
// 回收结果(核心模型:PMO 排会、回收结果、派活):这一批全部验收、看板空了,
|
|
190
|
+
// 叫 PMO 在主对话向 Human 汇报。此前没人汇报,Human 只能去看板里翻。
|
|
191
|
+
const ownerId = Object.values(state.projects ?? {})[0]?.ownerMemberId;
|
|
192
|
+
if (ownerId !== undefined && isBoardIdle(state)) {
|
|
193
|
+
add(ownerId, current.id, 'wave_done', `wave_done:${current.id}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (event?.type === 'task/changes-requested') {
|
|
198
|
+
const current = state.tasks?.[event.payload?.taskId];
|
|
199
|
+
add(current?.assigneeId, event.payload?.taskId, 'changes_requested', `changes:${event.seq ?? event.payload?.taskId}`);
|
|
200
|
+
}
|
|
201
|
+
if (event?.type === 'task/reopened') {
|
|
202
|
+
add(event.payload?.taskId === undefined ? null : state.tasks?.[event.payload.taskId]?.ownerId, event.payload?.taskId, 'task_reopened', `reopened:${event.seq ?? event.payload?.taskId}`);
|
|
203
|
+
}
|
|
204
|
+
if (event?.type === 'thread/posted') {
|
|
205
|
+
const taskId = event.payload?.taskId;
|
|
206
|
+
const current = state.tasks?.[taskId];
|
|
207
|
+
const author = event.payload?.entry?.authorId;
|
|
208
|
+
for (const memberId of [current?.ownerId, current?.assigneeId]) {
|
|
209
|
+
if (memberId !== undefined && memberId !== author) {
|
|
210
|
+
add(memberId, taskId, 'thread_reply', `thread:${taskId}:${event.seq ?? event.payload?.entry?.seq}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (event?.type === 'stream/posted' && event.payload?.kind === 'human_message') {
|
|
215
|
+
const project = Object.values(state.projects ?? {})[0];
|
|
216
|
+
const body = typeof event.payload?.body === 'string' ? event.payload.body : '';
|
|
217
|
+
const roster = Object.values(state.members ?? {})
|
|
218
|
+
.filter((member) => member.kind === 'agent' && member.status !== 'retired')
|
|
219
|
+
.map((member) => `${member.name}=${member.id}(${member.role || '成员'})`)
|
|
220
|
+
.join('、');
|
|
221
|
+
add(
|
|
222
|
+
project?.ownerMemberId,
|
|
223
|
+
null,
|
|
224
|
+
'human_message',
|
|
225
|
+
`human_message:${event.seq ?? event.payload?.seq}`,
|
|
226
|
+
`你是本项目的 PMO(Project owner)。Human 在主对话说:“${body.slice(0, 2000)}”。Human 只看主对话,你是唯一在主对话回应 Human 的成员。先判断这句话是什么:\n- 打招呼、提问、确认、闲聊 → 直接用 team action=say 在主对话回答,不建卡。\n- 要团队完成的工作 → 先用 team action=members 看成员,再用 action=create 建卡(ownerId/assigneeId 用成员 id,resultDefinition 写可验证的验收标准,需要独立复跑时 qaRequired=true 并给 qaChecklist),然后用 action=say 在主对话告诉 Human 你的安排并用 taskIds 附上这些卡。需求很大或含糊时先建一张「澄清/拆解」卡,不要直接开始实现。\n只做 Human 要的范围,不要自行扩大。需要 Human 拍板的问题用 action=askHuman。可用成员:${roster || '无'}。`,
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
if (event?.type === 'pmo/review') {
|
|
230
|
+
const task = state.tasks?.[event.payload?.taskId];
|
|
231
|
+
if (task !== undefined) {
|
|
232
|
+
const bucket = event.payload?.bucket ?? Math.floor(Date.now() / 600_000);
|
|
233
|
+
// 只叫 QA 成员,并且用同一套资格判定:之前把**所有** QA 成员都叫来,
|
|
234
|
+
// 包括刚交活的 assignee 和 claim 过这张卡的人,等于让人验收自己的活。
|
|
235
|
+
for (const member of Object.values(state.members ?? {})) {
|
|
236
|
+
if (!isQaMember(member)) continue;
|
|
237
|
+
if (!isReviewCandidate(member, task)) continue;
|
|
238
|
+
add(member.id, task.id, 'pmo_review', `pmo_review:${task.id}:${member.id}:${bucket}`);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (event?.type === 'pmo/escalate') {
|
|
243
|
+
const task = state.tasks?.[event.payload?.taskId];
|
|
244
|
+
if (task !== undefined) {
|
|
245
|
+
const bucket = event.payload?.bucket ?? Math.floor(Date.now() / 3_600_000);
|
|
246
|
+
add(task.ownerId, task.id, 'pmo_escalate', `pmo_escalate:${task.id}:${bucket}`);
|
|
247
|
+
// Handoffs and meeting decisions live in Thread text; the wake
|
|
248
|
+
// matrix cannot see them. Escalation therefore also reaches the
|
|
249
|
+
// Project owner, who may take the card or re-point it.
|
|
250
|
+
const projectOwner = Object.values(state.projects ?? {})[0]?.ownerMemberId;
|
|
251
|
+
if (projectOwner !== undefined && projectOwner !== task.ownerId) {
|
|
252
|
+
add(projectOwner, task.id, 'pmo_escalate', `pmo_escalate_owner:${task.id}:${bucket}`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (event?.type === 'pmo/dispatch') {
|
|
257
|
+
// Pending 快速补派(60s tick 的状态检查兜底):任务仍 pending 且责任位
|
|
258
|
+
// 空闲。key 带 bucket(每分钟最多一次),避免与创建时的 task_created
|
|
259
|
+
// 去重键互相吞掉。
|
|
260
|
+
const task = state.tasks?.[event.payload?.taskId];
|
|
261
|
+
if (task !== undefined && task.status === 'pending') {
|
|
262
|
+
const bucket = event.payload?.bucket ?? Math.floor(Date.now() / 60_000);
|
|
263
|
+
const responsibleId = task.assigneeId ?? task.ownerId;
|
|
264
|
+
add(responsibleId, task.id, 'pmo_dispatch', `pmo_dispatch:${task.id}:${bucket}`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (event?.type === 'pmo/heartbeat') {
|
|
268
|
+
const task = state.tasks?.[event.payload?.taskId];
|
|
269
|
+
if (task !== undefined) {
|
|
270
|
+
const target = event.payload?.memberId ?? task.assigneeId ?? task.ownerId;
|
|
271
|
+
const bucket = event.payload?.bucket ?? Math.floor(Date.now() / 600_000);
|
|
272
|
+
add(target, task.id, 'pmo_heartbeat', `pmo_heartbeat:${task.id}:${bucket}`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (event?.type === 'pmo/stall') {
|
|
276
|
+
const task = state.tasks?.[event.payload?.taskId];
|
|
277
|
+
if (task !== undefined) {
|
|
278
|
+
const bucket = event.payload?.bucket ?? Math.floor(Date.now() / 1_800_000);
|
|
279
|
+
for (const memberId of [task.ownerId, task.assigneeId]) {
|
|
280
|
+
add(memberId, task.id, 'pmo_stall', `pmo_stall:${task.id}:${bucket}`);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (event?.type === 'pmo/continue') {
|
|
285
|
+
const project = Object.values(state.projects ?? {})[0];
|
|
286
|
+
const bucket = event.payload?.bucket ?? Math.floor(Date.now() / 600_000);
|
|
287
|
+
const ownerId = project?.ownerMemberId;
|
|
288
|
+
// PMO meetings include every agent member; the owner leads, everyone
|
|
289
|
+
// else contributes from their own professional seat.
|
|
290
|
+
for (const member of Object.values(state.members ?? {})) {
|
|
291
|
+
if (member.kind !== 'agent' || member.status === 'retired') continue;
|
|
292
|
+
if (member.id === ownerId) {
|
|
293
|
+
add(member.id, null, 'pmo_continue', `pmo:${project?.id ?? 'project'}:${bucket}`);
|
|
294
|
+
} else {
|
|
295
|
+
add(member.id, null, 'pmo_meeting', `pmo_meeting:${project?.id ?? 'project'}:${member.id}:${bucket}`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (event?.type === 'inbox/item-created') {
|
|
300
|
+
// 账本写的是**扁平**载荷({ itemId, memberId, ref, ... }),不是嵌套的
|
|
301
|
+
// payload.item —— 之前按嵌套读,这条唤醒分支永远不触发。
|
|
302
|
+
const item = event.payload ?? {};
|
|
303
|
+
add(item.memberId, item.ref?.taskId ?? null, 'inbox', `inbox:${item.itemId ?? item.id ?? event.seq}`);
|
|
304
|
+
}
|
|
305
|
+
return [...targets.values()];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** Thread 尾巴:只带最近几条,正文按字符截断,避免把提示词撑大。 */
|
|
309
|
+
const CONTEXT_ENTRY_LIMIT = 3;
|
|
310
|
+
const CONTEXT_BODY_LIMIT = 120;
|
|
311
|
+
const CONTEXT_TITLE_LIMIT = 80;
|
|
312
|
+
|
|
313
|
+
const shortText = (value, limit) => {
|
|
314
|
+
const text = typeof value === 'string' ? value.replace(/\s+/g, ' ').trim() : '';
|
|
315
|
+
return text.length > limit ? `${text.slice(0, limit)}…` : text;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* 冷路径(会话不可恢复/重建)的最小可续跑上下文:当前 Task 一句话 + Thread 最近几条。
|
|
320
|
+
*
|
|
321
|
+
* 为什么不另存一份:这些都已在 Project 账本投影里(`state.tasks` / `state.threads` /
|
|
322
|
+
* `state.members`),再建一份存储就是第二个真相源。这里只负责**投喂**——成员重建后
|
|
323
|
+
* 不必从零开始回忆「刚才在做什么、谁说了什么」。
|
|
324
|
+
*
|
|
325
|
+
* @param {object} state 账本投影
|
|
326
|
+
* @param {string} taskId 当前 Task
|
|
327
|
+
* @param {{ includeThread?: boolean, entryLimit?: number, bodyLimit?: number }} options
|
|
328
|
+
* @returns {string} 以换行开头;没有可用内容时返回空串
|
|
329
|
+
*/
|
|
330
|
+
export function describeTaskContext(state, taskId, options = {}) {
|
|
331
|
+
if (typeof taskId !== 'string' || taskId === '') return '';
|
|
332
|
+
const entryLimit = Number.isSafeInteger(options.entryLimit) && options.entryLimit > 0 ? options.entryLimit : CONTEXT_ENTRY_LIMIT;
|
|
333
|
+
const bodyLimit = Number.isSafeInteger(options.bodyLimit) && options.bodyLimit > 0 ? options.bodyLimit : CONTEXT_BODY_LIMIT;
|
|
334
|
+
const lines = [];
|
|
335
|
+
const task = state?.tasks?.[taskId];
|
|
336
|
+
if (task !== undefined && task !== null) {
|
|
337
|
+
const title = shortText(task.title, CONTEXT_TITLE_LIMIT) || '(无标题)';
|
|
338
|
+
lines.push(`【当前 Task】#${task.number ?? '?'} ${title} [${task.status ?? '?'}]`);
|
|
339
|
+
}
|
|
340
|
+
if (options.includeThread === true) {
|
|
341
|
+
const entries = Array.isArray(state?.threads?.[taskId]?.entries)
|
|
342
|
+
? state.threads[taskId].entries.slice(-entryLimit)
|
|
343
|
+
: [];
|
|
344
|
+
const rendered = [];
|
|
345
|
+
for (const entry of entries) {
|
|
346
|
+
const body = shortText(entry?.body, bodyLimit);
|
|
347
|
+
if (body === '') continue;
|
|
348
|
+
const author = state?.members?.[entry?.authorId]?.name ?? entry?.authorId ?? '?';
|
|
349
|
+
rendered.push(`- ${author}:${body}`);
|
|
350
|
+
}
|
|
351
|
+
if (rendered.length > 0) lines.push('【最近对话】', ...rendered);
|
|
352
|
+
}
|
|
353
|
+
return lines.length === 0 ? '' : `\n${lines.join('\n')}`;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Compact Task delta for a live member. Identity, personality and the core
|
|
358
|
+
* skill are activation-only; re-sending them on every wake duplicated the
|
|
359
|
+
* prompt and crowded the persistent session.
|
|
360
|
+
*
|
|
361
|
+
* `options.includeThread` 打开时才附 Thread 尾巴:**只有冷路径**(会话不可恢复、
|
|
362
|
+
* 或重建后)才需要它,否则每次唤醒都白烧 token。
|
|
363
|
+
*/
|
|
364
|
+
export function describeWake(wake, state, options = {}) {
|
|
365
|
+
const member = state.members?.[wake.memberId];
|
|
366
|
+
const boundary = typeof wake.taskId === 'string' && wake.taskId !== ''
|
|
367
|
+
? '【Task 边界】这是当前 Task 的增量指令;先读 Task/Thread 当前状态,不要把上一个 Task 的假设直接带入。\n'
|
|
368
|
+
: '';
|
|
369
|
+
const context = describeTaskContext(state, wake.taskId, options);
|
|
370
|
+
return `${boundary}[${member?.name ?? wake.memberId}] ${wake.prompt}${context}`;
|
|
371
|
+
}
|