@suifei/dsh-ai-team 0.3.81 → 0.3.83
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 +14 -0
- package/client.js +4401 -4428
- package/index.js +3 -39
- package/lib/host/service.js +1334 -1360
- package/lib/ledger/fold.js +1089 -1101
- package/lib/runtime/member-runtime.js +1 -10
- package/package.json +1 -1
- package/lib/host/chat-gate-sync.js +0 -38
package/lib/host/service.js
CHANGED
|
@@ -1,1360 +1,1334 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
|
2
|
-
import pathDefault from 'node:path';
|
|
3
|
-
import { ERRORS, fail } from '../domain/errors.js';
|
|
4
|
-
import { findReportPath, parseDecisionQuestions } from './decision-report.js';
|
|
5
|
-
import { ids } from '../domain/ids.js';
|
|
6
|
-
import { ackInboxItem, createInboxItem, isInformationalThreadMention } from '../domain/inbox.js';
|
|
7
|
-
import { chooseDecisionAnswers } from './pmo.js';
|
|
8
|
-
import {
|
|
9
|
-
acceptTask,
|
|
10
|
-
assignTask,
|
|
11
|
-
cancelTask,
|
|
12
|
-
claimTask,
|
|
13
|
-
createTask,
|
|
14
|
-
deleteTask,
|
|
15
|
-
reassignTask,
|
|
16
|
-
releaseTask,
|
|
17
|
-
reopenTask,
|
|
18
|
-
requestChangesTask,
|
|
19
|
-
submitTask,
|
|
20
|
-
} from '../domain/task.js';
|
|
21
|
-
import { appendReply, createThread } from '../domain/thread.js';
|
|
22
|
-
import { boardView, quadrantsView, taskCard } from './views.js';
|
|
23
|
-
import { applyMemberConfigPatch, normalizeMemberConfig } from '../domain/member-config.js';
|
|
24
|
-
import { isQaMember, unfinishedTasksFor } from '../domain/member-profile.js';
|
|
25
|
-
|
|
26
|
-
function mentionsHuman(body) {
|
|
27
|
-
return typeof body === 'string'
|
|
28
|
-
&& /(^|[^A-Za-z])(human|@你|待你|需要你|请你|等你|你拍板)([^A-Za-z]|$)/i.test(body);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Team domain service on top of a Project ledger.
|
|
33
|
-
*
|
|
34
|
-
* The ledger is the single writer and owns `state`; this service only:
|
|
35
|
-
* 1. reads the current view from `ledger.state`,
|
|
36
|
-
* 2. validates the command with the pure domain rules,
|
|
37
|
-
* 3. appends one event and returns the committed result.
|
|
38
|
-
*
|
|
39
|
-
* `ledger.append` must update `ledger.state` before resolving (or provide a
|
|
40
|
-
* synchronous state read after an awaited append).
|
|
41
|
-
*/
|
|
42
|
-
/**
|
|
43
|
-
* P6 交接文档:按时序拼接 Human 指令与全部任务关键信息,写入项目工作
|
|
44
|
-
* 目录(交付给别的工具继续开发);账本留痕记录路径。
|
|
45
|
-
*/
|
|
46
|
-
// P6 交接文档生成:Human 指令时序 + 任务全量关键信息,Markdown 落盘。
|
|
47
|
-
function handoffMarkdown(state, project, nowMs) {
|
|
48
|
-
const line = (text) => String(text ?? '').replace(/\r?\n/g, '\n ');
|
|
49
|
-
const out = [];
|
|
50
|
-
out.push(`# 项目交接文档 — ${project.name}`);
|
|
51
|
-
out.push('');
|
|
52
|
-
out.push(`- 生成时间:${new Date(nowMs).toISOString()}`);
|
|
53
|
-
out.push(`- 项目 id:${project.id}`);
|
|
54
|
-
out.push(`- 目标:${project.goal ?? '(未填写)'}`);
|
|
55
|
-
out.push(`- 工作目录:${project.workspacePath}`);
|
|
56
|
-
out.push(`- 运行态:${project.runState === 'stopped' ? '已停止(交接前)' : '运行中'}`);
|
|
57
|
-
out.push('');
|
|
58
|
-
out.push('## 一、Human 指令时序(按账本顺序)');
|
|
59
|
-
out.push('');
|
|
60
|
-
const humanMessages = (state.stream ?? []).filter((entry) => entry.kind === 'human_message');
|
|
61
|
-
if (humanMessages.length === 0) out.push('(无 Human 指令记录)');
|
|
62
|
-
for (const entry of humanMessages) {
|
|
63
|
-
out.push(`- ${new Date(entry.at ?? 0).toISOString()} — ${line(entry.body)}`);
|
|
64
|
-
}
|
|
65
|
-
out.push('');
|
|
66
|
-
out.push('## 二、任务清单(含状态与验收要点)');
|
|
67
|
-
out.push('');
|
|
68
|
-
const tasks = Object.values(state.tasks ?? {}).filter((task) => task.tombstone !== true)
|
|
69
|
-
.sort((left, right) => (left.createdAt ?? 0) - (right.createdAt ?? 0));
|
|
70
|
-
if (tasks.length === 0) out.push('(无任务)');
|
|
71
|
-
for (const task of tasks) {
|
|
72
|
-
out.push(`### #${task.number ?? task.id} ${task.title} — ${task.status}`);
|
|
73
|
-
out.push(`- 结果定义:${line(task.resultDefinition)}`);
|
|
74
|
-
if (task.summary) out.push(`- 提交摘要:${line(task.summary)}`);
|
|
75
|
-
for (const ev of task.evidence ?? []) out.push(`- 证据(${ev.kind}):${line(ev.value)}`);
|
|
76
|
-
if (task.qaChecks?.length) out.push(`- QA 检查:${task.qaChecks.map((c) => `${c.passed ? '✔' : '✘'} ${line(c.text ?? '')}`).join(';')}`);
|
|
77
|
-
if (task.reason) out.push(`- 最近理由:${line(task.reason)}`);
|
|
78
|
-
out.push(`- 责任位:owner=${task.ownerId ?? '—'};assignee=${task.assigneeId ?? '—'};更新于 ${new Date(task.updatedAt ?? 0).toISOString()}`);
|
|
79
|
-
out.push('');
|
|
80
|
-
}
|
|
81
|
-
out.push('## 三、Thread 关键讨论(按任务)');
|
|
82
|
-
out.push('');
|
|
83
|
-
for (const [taskId, thread] of Object.entries(state.threads ?? {})) {
|
|
84
|
-
const entries = thread.entries ?? [];
|
|
85
|
-
if (entries.length === 0) continue;
|
|
86
|
-
out.push(`### Task ${taskId}`);
|
|
87
|
-
for (const entry of entries.slice(-20)) {
|
|
88
|
-
out.push(`- ${new Date(entry.at ?? 0).toISOString()} ${entry.author ?? entry.authorId ?? '—'}:${line(entry.body)}`);
|
|
89
|
-
}
|
|
90
|
-
out.push('');
|
|
91
|
-
}
|
|
92
|
-
out.push('## 四、接手提示');
|
|
93
|
-
out.push('');
|
|
94
|
-
out.push('- 以上内容来自插件账本投影(append-only,时序可信);接手工具可直接在工作目录继续开发。');
|
|
95
|
-
out.push('- 成员的 DSH 会话仍在,如需复用团队请保持项目 runState;如改用其他工具,可停止本项目。');
|
|
96
|
-
return out.join('\n') + '\n';
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export class TeamService {
|
|
100
|
-
/**
|
|
101
|
-
* @param {{ ledger: { state: any, append: (input: any) => Promise<any> }, now?: () => number, diag?: { append: (row: any) => any } }} options
|
|
102
|
-
*/
|
|
103
|
-
constructor(options) {
|
|
104
|
-
this.ledger = options.ledger;
|
|
105
|
-
this.now = options.now ?? (() => Date.now());
|
|
106
|
-
this.memberConfigPolicy = options.memberConfigPolicy ?? {};
|
|
107
|
-
/** Diagnostic sink ({ append(row) }); optional so tests run without it. */
|
|
108
|
-
this.diag = options.diag ?? null;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
get state() {
|
|
112
|
-
return this.ledger.state;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** The Human member id, derived once per call from the folded state. */
|
|
116
|
-
humanId() {
|
|
117
|
-
return this.state.humanMemberId
|
|
118
|
-
?? Object.values(this.state.members ?? {}).find((member) => member.kind === 'human')?.id
|
|
119
|
-
?? null;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** Four-column board derived from the same ledger state. */
|
|
123
|
-
board(projectId, options = {}) {
|
|
124
|
-
if (projectId !== undefined && this.state.projects?.[projectId] === undefined) {
|
|
125
|
-
fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
126
|
-
}
|
|
127
|
-
return boardView(this.state, options);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** Four-quadrant situation view; Human is the only attention target. */
|
|
131
|
-
quadrants(projectId, options = {}) {
|
|
132
|
-
if (projectId !== undefined && this.state.projects?.[projectId] === undefined) {
|
|
133
|
-
fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
134
|
-
}
|
|
135
|
-
const human = options.humanMemberId ?? this.humanId() ?? 'mem_human';
|
|
136
|
-
return quadrantsView(this.state, { ...options, humanMemberId: human });
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/** One page of workbench state; the UI reads this instead of the ledger. */
|
|
140
|
-
bootstrap(projectId, threadTaskId) {
|
|
141
|
-
const state = this.state;
|
|
142
|
-
const projects = Object.values(state.projects ?? {})
|
|
143
|
-
.filter((p) => p.status !== 'archived')
|
|
144
|
-
.map((p) => ({
|
|
145
|
-
id: p.id, name: p.name, goal: p.goal ?? '', owner: p.ownerName ?? p.ownerMemberId ?? p.ownerId, workspacePath: p.workspacePath ?? '', pmoAuto: p.pmoAuto !== false, active: p.status !== 'archived',
|
|
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
|
-
|
|
179
|
-
//
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
(item
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
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
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
item.
|
|
254
|
-
&& item.ref?.
|
|
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
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
//
|
|
296
|
-
//
|
|
297
|
-
|
|
298
|
-
const
|
|
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
|
-
case 'project.
|
|
336
|
-
case 'project.
|
|
337
|
-
case 'project.
|
|
338
|
-
case 'project.
|
|
339
|
-
case 'project.
|
|
340
|
-
case 'project.
|
|
341
|
-
case '
|
|
342
|
-
case '
|
|
343
|
-
case '
|
|
344
|
-
case 'member.
|
|
345
|
-
case 'member.
|
|
346
|
-
case 'member.
|
|
347
|
-
case 'member.
|
|
348
|
-
case 'member.
|
|
349
|
-
case 'member.
|
|
350
|
-
case 'member.
|
|
351
|
-
case 'member.
|
|
352
|
-
case 'member.
|
|
353
|
-
case 'member.
|
|
354
|
-
case 'member.
|
|
355
|
-
case 'member.
|
|
356
|
-
case 'member.
|
|
357
|
-
case '
|
|
358
|
-
case '
|
|
359
|
-
case 'task.
|
|
360
|
-
case 'task.
|
|
361
|
-
case 'task.
|
|
362
|
-
case 'task.
|
|
363
|
-
case 'task.
|
|
364
|
-
case 'task.
|
|
365
|
-
case 'task.
|
|
366
|
-
case 'task.
|
|
367
|
-
case 'task.
|
|
368
|
-
case 'task.
|
|
369
|
-
case 'task.
|
|
370
|
-
case '
|
|
371
|
-
case '
|
|
372
|
-
case '
|
|
373
|
-
case '
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
const
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
//
|
|
463
|
-
// '
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
const
|
|
469
|
-
if (
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
return {
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
const
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
const
|
|
502
|
-
|
|
503
|
-
if (project ===
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
const
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
if (
|
|
523
|
-
const
|
|
524
|
-
|
|
525
|
-
if (
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
if (
|
|
537
|
-
const
|
|
538
|
-
if (
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
if (
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
};
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
const
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
};
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
});
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
const
|
|
645
|
-
if (
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
if (
|
|
662
|
-
const
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
if (personality === '') fail(ERRORS.INVALID_ARGUMENT, 'personality is required');
|
|
691
|
-
await this.append(command, 'member/distilled', {
|
|
692
|
-
memberId: member.id,
|
|
693
|
-
personality,
|
|
694
|
-
updatedBy:
|
|
695
|
-
reason: payload.reason ?? '
|
|
696
|
-
});
|
|
697
|
-
return { member: this.state.members[member.id] };
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
:
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
if (
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
if (command.actor.kind
|
|
763
|
-
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
const
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
const
|
|
799
|
-
const
|
|
800
|
-
if (
|
|
801
|
-
if (
|
|
802
|
-
fail(ERRORS.
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
const
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
const
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
if (
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
};
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
const
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
case '
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
(
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
const
|
|
1146
|
-
const
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
:
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
})
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
}
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
if (
|
|
1256
|
-
const item =
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
if (item === undefined) fail(ERRORS.NOT_FOUND, `inbox item ${String(payload.itemId)} not found`);
|
|
1336
|
-
const next = ackInboxItem(item, { disposition: payload.disposition, decision: payload.decision }, { at: this.now() });
|
|
1337
|
-
await this.ledger.append({
|
|
1338
|
-
type: 'inbox/acked',
|
|
1339
|
-
payload: { itemId: next.id, memberId: next.memberId, state: next.state, disposition: next.disposition, at: next.ackedAt },
|
|
1340
|
-
actor: command.actor,
|
|
1341
|
-
correlationId: command.correlationId,
|
|
1342
|
-
projectId: command.projectId,
|
|
1343
|
-
at: this.now(),
|
|
1344
|
-
});
|
|
1345
|
-
return { item: next };
|
|
1346
|
-
}
|
|
1347
|
-
|
|
1348
|
-
/** Create an inbox item and append it; used by the Waker/observer. */
|
|
1349
|
-
async notify(input, command = { actor: { kind: 'system' } }) {
|
|
1350
|
-
const item = createInboxItem(input, { at: this.now() });
|
|
1351
|
-
await this.ledger.append({
|
|
1352
|
-
type: 'inbox/item-created',
|
|
1353
|
-
payload: { itemId: item.id, ...item },
|
|
1354
|
-
actor: command.actor ?? { kind: 'system' },
|
|
1355
|
-
projectId: command.projectId,
|
|
1356
|
-
at: this.now(),
|
|
1357
|
-
});
|
|
1358
|
-
return item;
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import pathDefault from 'node:path';
|
|
3
|
+
import { ERRORS, fail } from '../domain/errors.js';
|
|
4
|
+
import { findReportPath, parseDecisionQuestions } from './decision-report.js';
|
|
5
|
+
import { ids } from '../domain/ids.js';
|
|
6
|
+
import { ackInboxItem, createInboxItem, isInformationalThreadMention } from '../domain/inbox.js';
|
|
7
|
+
import { chooseDecisionAnswers } from './pmo.js';
|
|
8
|
+
import {
|
|
9
|
+
acceptTask,
|
|
10
|
+
assignTask,
|
|
11
|
+
cancelTask,
|
|
12
|
+
claimTask,
|
|
13
|
+
createTask,
|
|
14
|
+
deleteTask,
|
|
15
|
+
reassignTask,
|
|
16
|
+
releaseTask,
|
|
17
|
+
reopenTask,
|
|
18
|
+
requestChangesTask,
|
|
19
|
+
submitTask,
|
|
20
|
+
} from '../domain/task.js';
|
|
21
|
+
import { appendReply, createThread } from '../domain/thread.js';
|
|
22
|
+
import { boardView, quadrantsView, taskCard } from './views.js';
|
|
23
|
+
import { applyMemberConfigPatch, normalizeMemberConfig } from '../domain/member-config.js';
|
|
24
|
+
import { isQaMember, unfinishedTasksFor } from '../domain/member-profile.js';
|
|
25
|
+
|
|
26
|
+
function mentionsHuman(body) {
|
|
27
|
+
return typeof body === 'string'
|
|
28
|
+
&& /(^|[^A-Za-z])(human|@你|待你|需要你|请你|等你|你拍板)([^A-Za-z]|$)/i.test(body);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Team domain service on top of a Project ledger.
|
|
33
|
+
*
|
|
34
|
+
* The ledger is the single writer and owns `state`; this service only:
|
|
35
|
+
* 1. reads the current view from `ledger.state`,
|
|
36
|
+
* 2. validates the command with the pure domain rules,
|
|
37
|
+
* 3. appends one event and returns the committed result.
|
|
38
|
+
*
|
|
39
|
+
* `ledger.append` must update `ledger.state` before resolving (or provide a
|
|
40
|
+
* synchronous state read after an awaited append).
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* P6 交接文档:按时序拼接 Human 指令与全部任务关键信息,写入项目工作
|
|
44
|
+
* 目录(交付给别的工具继续开发);账本留痕记录路径。
|
|
45
|
+
*/
|
|
46
|
+
// P6 交接文档生成:Human 指令时序 + 任务全量关键信息,Markdown 落盘。
|
|
47
|
+
function handoffMarkdown(state, project, nowMs) {
|
|
48
|
+
const line = (text) => String(text ?? '').replace(/\r?\n/g, '\n ');
|
|
49
|
+
const out = [];
|
|
50
|
+
out.push(`# 项目交接文档 — ${project.name}`);
|
|
51
|
+
out.push('');
|
|
52
|
+
out.push(`- 生成时间:${new Date(nowMs).toISOString()}`);
|
|
53
|
+
out.push(`- 项目 id:${project.id}`);
|
|
54
|
+
out.push(`- 目标:${project.goal ?? '(未填写)'}`);
|
|
55
|
+
out.push(`- 工作目录:${project.workspacePath}`);
|
|
56
|
+
out.push(`- 运行态:${project.runState === 'stopped' ? '已停止(交接前)' : '运行中'}`);
|
|
57
|
+
out.push('');
|
|
58
|
+
out.push('## 一、Human 指令时序(按账本顺序)');
|
|
59
|
+
out.push('');
|
|
60
|
+
const humanMessages = (state.stream ?? []).filter((entry) => entry.kind === 'human_message');
|
|
61
|
+
if (humanMessages.length === 0) out.push('(无 Human 指令记录)');
|
|
62
|
+
for (const entry of humanMessages) {
|
|
63
|
+
out.push(`- ${new Date(entry.at ?? 0).toISOString()} — ${line(entry.body)}`);
|
|
64
|
+
}
|
|
65
|
+
out.push('');
|
|
66
|
+
out.push('## 二、任务清单(含状态与验收要点)');
|
|
67
|
+
out.push('');
|
|
68
|
+
const tasks = Object.values(state.tasks ?? {}).filter((task) => task.tombstone !== true)
|
|
69
|
+
.sort((left, right) => (left.createdAt ?? 0) - (right.createdAt ?? 0));
|
|
70
|
+
if (tasks.length === 0) out.push('(无任务)');
|
|
71
|
+
for (const task of tasks) {
|
|
72
|
+
out.push(`### #${task.number ?? task.id} ${task.title} — ${task.status}`);
|
|
73
|
+
out.push(`- 结果定义:${line(task.resultDefinition)}`);
|
|
74
|
+
if (task.summary) out.push(`- 提交摘要:${line(task.summary)}`);
|
|
75
|
+
for (const ev of task.evidence ?? []) out.push(`- 证据(${ev.kind}):${line(ev.value)}`);
|
|
76
|
+
if (task.qaChecks?.length) out.push(`- QA 检查:${task.qaChecks.map((c) => `${c.passed ? '✔' : '✘'} ${line(c.text ?? '')}`).join(';')}`);
|
|
77
|
+
if (task.reason) out.push(`- 最近理由:${line(task.reason)}`);
|
|
78
|
+
out.push(`- 责任位:owner=${task.ownerId ?? '—'};assignee=${task.assigneeId ?? '—'};更新于 ${new Date(task.updatedAt ?? 0).toISOString()}`);
|
|
79
|
+
out.push('');
|
|
80
|
+
}
|
|
81
|
+
out.push('## 三、Thread 关键讨论(按任务)');
|
|
82
|
+
out.push('');
|
|
83
|
+
for (const [taskId, thread] of Object.entries(state.threads ?? {})) {
|
|
84
|
+
const entries = thread.entries ?? [];
|
|
85
|
+
if (entries.length === 0) continue;
|
|
86
|
+
out.push(`### Task ${taskId}`);
|
|
87
|
+
for (const entry of entries.slice(-20)) {
|
|
88
|
+
out.push(`- ${new Date(entry.at ?? 0).toISOString()} ${entry.author ?? entry.authorId ?? '—'}:${line(entry.body)}`);
|
|
89
|
+
}
|
|
90
|
+
out.push('');
|
|
91
|
+
}
|
|
92
|
+
out.push('## 四、接手提示');
|
|
93
|
+
out.push('');
|
|
94
|
+
out.push('- 以上内容来自插件账本投影(append-only,时序可信);接手工具可直接在工作目录继续开发。');
|
|
95
|
+
out.push('- 成员的 DSH 会话仍在,如需复用团队请保持项目 runState;如改用其他工具,可停止本项目。');
|
|
96
|
+
return out.join('\n') + '\n';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export class TeamService {
|
|
100
|
+
/**
|
|
101
|
+
* @param {{ ledger: { state: any, append: (input: any) => Promise<any> }, now?: () => number, diag?: { append: (row: any) => any } }} options
|
|
102
|
+
*/
|
|
103
|
+
constructor(options) {
|
|
104
|
+
this.ledger = options.ledger;
|
|
105
|
+
this.now = options.now ?? (() => Date.now());
|
|
106
|
+
this.memberConfigPolicy = options.memberConfigPolicy ?? {};
|
|
107
|
+
/** Diagnostic sink ({ append(row) }); optional so tests run without it. */
|
|
108
|
+
this.diag = options.diag ?? null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
get state() {
|
|
112
|
+
return this.ledger.state;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The Human member id, derived once per call from the folded state. */
|
|
116
|
+
humanId() {
|
|
117
|
+
return this.state.humanMemberId
|
|
118
|
+
?? Object.values(this.state.members ?? {}).find((member) => member.kind === 'human')?.id
|
|
119
|
+
?? null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Four-column board derived from the same ledger state. */
|
|
123
|
+
board(projectId, options = {}) {
|
|
124
|
+
if (projectId !== undefined && this.state.projects?.[projectId] === undefined) {
|
|
125
|
+
fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
126
|
+
}
|
|
127
|
+
return boardView(this.state, options);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Four-quadrant situation view; Human is the only attention target. */
|
|
131
|
+
quadrants(projectId, options = {}) {
|
|
132
|
+
if (projectId !== undefined && this.state.projects?.[projectId] === undefined) {
|
|
133
|
+
fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
134
|
+
}
|
|
135
|
+
const human = options.humanMemberId ?? this.humanId() ?? 'mem_human';
|
|
136
|
+
return quadrantsView(this.state, { ...options, humanMemberId: human });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** One page of workbench state; the UI reads this instead of the ledger. */
|
|
140
|
+
bootstrap(projectId, threadTaskId) {
|
|
141
|
+
const state = this.state;
|
|
142
|
+
const projects = Object.values(state.projects ?? {})
|
|
143
|
+
.filter((p) => p.status !== 'archived')
|
|
144
|
+
.map((p) => ({
|
|
145
|
+
id: p.id, name: p.name, goal: p.goal ?? '', owner: p.ownerName ?? p.ownerMemberId ?? p.ownerId, workspacePath: p.workspacePath ?? '', pmoAuto: p.pmoAuto !== false, active: p.status !== 'archived',
|
|
146
|
+
runState: p.runState === 'stopped' ? 'stopped' : 'running',
|
|
147
|
+
}));
|
|
148
|
+
const members = Object.values(state.members ?? {}).map((m) => ({
|
|
149
|
+
id: m.id,
|
|
150
|
+
name: m.name,
|
|
151
|
+
role: m.role ?? '',
|
|
152
|
+
status: m.status ?? 'idle',
|
|
153
|
+
kind: m.kind ?? 'agent',
|
|
154
|
+
sessionId: m.sessionId ?? null,
|
|
155
|
+
preset: m.config?.preset ?? m.preset ?? 'standard',
|
|
156
|
+
personality: m.personality ?? '',
|
|
157
|
+
notes: m.notes ?? [],
|
|
158
|
+
skills: m.skills ?? [],
|
|
159
|
+
profileVersion: m.profileVersion ?? 1,
|
|
160
|
+
distillVersion: m.distillVersion ?? 0,
|
|
161
|
+
distilledAt: m.distilledAt ?? null,
|
|
162
|
+
talentPool: m.talentPool === true,
|
|
163
|
+
leftTeamAt: m.leftTeamAt ?? null,
|
|
164
|
+
qa: isQaMember(m),
|
|
165
|
+
config: m.config ?? null,
|
|
166
|
+
configVersion: m.configVersion ?? 0,
|
|
167
|
+
lastFailure: m.lastFailure ?? null,
|
|
168
|
+
attempts: m.attempts ?? 0,
|
|
169
|
+
lastRecoveredAt: m.lastRecoveredAt ?? null,
|
|
170
|
+
}));
|
|
171
|
+
const agents = members.filter((m) => m.kind !== 'human');
|
|
172
|
+
// One Task card shape for board, quadrants and bootstrap; bootstrap only
|
|
173
|
+
// adds the submission bookkeeping the panel needs.
|
|
174
|
+
const tasks = Object.values(state.tasks ?? {})
|
|
175
|
+
.filter((task) => task.tombstone !== true && task.status !== 'deleted')
|
|
176
|
+
.map((task) => ({ ...taskCard(state, task), qaChecks: task.qaChecks ?? [] }));
|
|
177
|
+
const project = projectId === undefined ? projects[0] : projects.find((p) => p.id === projectId);
|
|
178
|
+
// 大规模/长迭代裁剪:面板只需要最近的窗口,全史在账本里按需取。
|
|
179
|
+
// stream 投影本身已封顶(≤500),bootstrap 再按窗口切尾。
|
|
180
|
+
const stream = project === undefined
|
|
181
|
+
? []
|
|
182
|
+
: (state.stream ?? []).filter((entry) => entry.projectId === project.id).slice(-200);
|
|
183
|
+
const selected = threadTaskId ?? tasks.find((task) => task.status === 'in_progress')?.id ?? tasks[0]?.id;
|
|
184
|
+
const threadOf = (taskId) => {
|
|
185
|
+
const full = taskId === undefined || taskId === null ? undefined : state.threads?.[taskId];
|
|
186
|
+
if (full === undefined) return { taskId, headSeq: 0, entries: [], entryTotal: 0 };
|
|
187
|
+
const entries = Array.isArray(full.entries) ? full.entries : [];
|
|
188
|
+
return { ...full, entries: entries.slice(-50), entryTotal: full.headSeq ?? entries.length };
|
|
189
|
+
};
|
|
190
|
+
const thread = selected === undefined ? null : threadOf(selected);
|
|
191
|
+
const threads = Object.fromEntries(tasks.map((task) => [task.id, threadOf(task.id)]));
|
|
192
|
+
const inbox = Object.values(state.inbox ?? {}).filter((item) =>
|
|
193
|
+
(item.memberId === this.humanId())
|
|
194
|
+
&& !isInformationalThreadMention(item));
|
|
195
|
+
return {
|
|
196
|
+
project: project ?? null,
|
|
197
|
+
projects,
|
|
198
|
+
agents,
|
|
199
|
+
members,
|
|
200
|
+
tasks,
|
|
201
|
+
stream,
|
|
202
|
+
thread,
|
|
203
|
+
threads,
|
|
204
|
+
inbox,
|
|
205
|
+
board: project === undefined ? null : this.board(project.id),
|
|
206
|
+
quadrants: project === undefined ? null : this.quadrants(project.id),
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Create missing Human decision items for existing Agent Thread messages
|
|
212
|
+
* that explicitly asked Human to decide. Runs on Project open once; the
|
|
213
|
+
* per-entry dedupe makes it idempotent.
|
|
214
|
+
*/
|
|
215
|
+
async backfillHumanMentions(workspacePath = '') {
|
|
216
|
+
const humanId = this.humanId();
|
|
217
|
+
if (humanId === null) return 0;
|
|
218
|
+
const projectId = Object.keys(this.state.projects ?? {})[0];
|
|
219
|
+
let reportPath = null;
|
|
220
|
+
for (const task of Object.values(this.state.tasks ?? {})) {
|
|
221
|
+
for (const entry of this.state.threads?.[task.id]?.entries ?? []) {
|
|
222
|
+
const found = findReportPath(entry.body);
|
|
223
|
+
if (found !== null && existsSync(found)) { reportPath = found; break; }
|
|
224
|
+
}
|
|
225
|
+
if (reportPath !== null) break;
|
|
226
|
+
}
|
|
227
|
+
let reportQuestions = [];
|
|
228
|
+
if (reportPath !== null) {
|
|
229
|
+
try {
|
|
230
|
+
// An agent-authored path drives a server-side read here; cap the
|
|
231
|
+
// read so a giant or adversarial report cannot flood memory.
|
|
232
|
+
if (statSync(reportPath).size > 256 * 1024) reportPath = null;
|
|
233
|
+
} catch { reportPath = null; }
|
|
234
|
+
}
|
|
235
|
+
if (reportPath !== null) {
|
|
236
|
+
try {
|
|
237
|
+
reportQuestions = parseDecisionQuestions(readFileSync(reportPath, 'utf8'));
|
|
238
|
+
} catch { /* report may be unavailable; keep the Thread fallback */ }
|
|
239
|
+
}
|
|
240
|
+
if (reportQuestions.length === 0) return 0;
|
|
241
|
+
const candidates = [];
|
|
242
|
+
for (const task of Object.values(this.state.tasks ?? {})) {
|
|
243
|
+
if (task.tombstone === true) continue;
|
|
244
|
+
for (const entry of this.state.threads?.[task.id]?.entries ?? []) {
|
|
245
|
+
const author = this.state.members?.[entry.authorId];
|
|
246
|
+
if (author?.kind !== 'agent' || !mentionsHuman(entry.body)) continue;
|
|
247
|
+
candidates.push({ task, entry, key: `${task.id}:${entry.seq}` });
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (candidates.length > 0) {
|
|
251
|
+
const alreadyStructured = Object.values(this.state.inbox ?? {}).some((item) =>
|
|
252
|
+
item.memberId === humanId
|
|
253
|
+
&& item.ref?.source === reportPath
|
|
254
|
+
&& Array.isArray(item.ref?.questions)
|
|
255
|
+
&& item.ref.questions.length > 0);
|
|
256
|
+
if (alreadyStructured) return 0;
|
|
257
|
+
const chosen = candidates.find((candidate) => /§6|六问/.test(candidate.entry.body))
|
|
258
|
+
?? candidates.sort((left, right) => (right.entry.seq ?? 0) - (left.entry.seq ?? 0))[0];
|
|
259
|
+
const item = createInboxItem({
|
|
260
|
+
memberId: humanId,
|
|
261
|
+
type: 'decision_required',
|
|
262
|
+
ref: {
|
|
263
|
+
taskId: chosen.task.id,
|
|
264
|
+
entrySeq: chosen.entry.seq,
|
|
265
|
+
memberId: chosen.entry.authorId,
|
|
266
|
+
memberName: this.state.members?.[chosen.entry.authorId]?.name ?? chosen.entry.authorId,
|
|
267
|
+
category: 'thread_mention',
|
|
268
|
+
title: `需 Human 决策 · ${reportQuestions.length} 问`,
|
|
269
|
+
questions: reportQuestions,
|
|
270
|
+
source: reportPath,
|
|
271
|
+
},
|
|
272
|
+
requiresDecision: true,
|
|
273
|
+
}, { at: this.now() });
|
|
274
|
+
await this.ledger.append({
|
|
275
|
+
type: 'inbox/item-created',
|
|
276
|
+
payload: { itemId: item.id, ...item },
|
|
277
|
+
actor: { kind: 'system' },
|
|
278
|
+
projectId,
|
|
279
|
+
at: this.now(),
|
|
280
|
+
});
|
|
281
|
+
return 1;
|
|
282
|
+
}
|
|
283
|
+
return 0;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Dispatch one command. The caller is an exact Team member (or Human) and
|
|
288
|
+
* the ledger serializes commands per Project.
|
|
289
|
+
* @param {{ type: string, payload?: any, actor: { kind: string, memberId?: string }, correlationId?: string, projectId?: string }} command
|
|
290
|
+
*/
|
|
291
|
+
async dispatch(command) {
|
|
292
|
+
const payload = command.payload ?? {};
|
|
293
|
+
const actor = command.actor;
|
|
294
|
+
// Diagnostic trail: one entry row per command, one failure row on throw,
|
|
295
|
+
// one done row with the duration in every case. 前置校验失败也必须留痕
|
|
296
|
+
// (errors 档只留 .fail 行,校验抛出必须发生在 try 内才能落行)。
|
|
297
|
+
const startedAt = Date.now();
|
|
298
|
+
const failRow = (code, message) => {
|
|
299
|
+
this.diag?.append({
|
|
300
|
+
ev: 'dispatch.fail',
|
|
301
|
+
type: command.type,
|
|
302
|
+
code,
|
|
303
|
+
projectId: command.projectId ?? null,
|
|
304
|
+
message,
|
|
305
|
+
elapsedMs: Date.now() - startedAt,
|
|
306
|
+
});
|
|
307
|
+
};
|
|
308
|
+
this.diag?.append({
|
|
309
|
+
ev: 'dispatch.start',
|
|
310
|
+
type: command.type,
|
|
311
|
+
actor: actor?.kind ?? null,
|
|
312
|
+
memberId: actor?.memberId ?? null,
|
|
313
|
+
projectId: command.projectId ?? null,
|
|
314
|
+
});
|
|
315
|
+
try {
|
|
316
|
+
if (actor === undefined || (actor.kind !== 'human' && actor.kind !== 'agent')) {
|
|
317
|
+
failRow(ERRORS.UNAUTHORIZED, 'an exact Team member is required');
|
|
318
|
+
fail(ERRORS.UNAUTHORIZED, 'an exact Team member is required');
|
|
319
|
+
}
|
|
320
|
+
// An agent actor must resolve to a live, non-retired member: otherwise a
|
|
321
|
+
// forged or stale memberId would bypass owner/QA checks downstream.
|
|
322
|
+
if (actor.kind === 'agent') {
|
|
323
|
+
const actingMember = this.state.members?.[actor.memberId];
|
|
324
|
+
if (actingMember === undefined || actingMember.kind !== 'agent') {
|
|
325
|
+
failRow(ERRORS.UNAUTHORIZED, `agent actor ${String(actor.memberId ?? '?')} is not a Team member`);
|
|
326
|
+
fail(ERRORS.UNAUTHORIZED, `agent actor ${String(actor.memberId ?? '?')} is not a Team member`);
|
|
327
|
+
}
|
|
328
|
+
if (actingMember.status === 'retired') {
|
|
329
|
+
failRow(ERRORS.INVALID_STATE, `agent ${actingMember.name ?? actingMember.id} is retired`);
|
|
330
|
+
fail(ERRORS.INVALID_STATE, `agent ${actingMember.name ?? actingMember.id} is retired`);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
switch (command.type) {
|
|
334
|
+
case 'project.create': return await this.createProject(command, payload);
|
|
335
|
+
case 'project.owner.update': return await this.changeProjectOwner(command, payload);
|
|
336
|
+
case 'project.workspace.update': return await this.changeProjectWorkspace(command, payload);
|
|
337
|
+
case 'project.pmo.update': return await this.setProjectPmo(command, payload);
|
|
338
|
+
case 'project.archive': return await this.setProjectArchive(command, payload);
|
|
339
|
+
case 'project.runState': return await this.setProjectRunState(command, payload);
|
|
340
|
+
case 'project.handoff': return await this.handoffProject(command, payload);
|
|
341
|
+
case 'stream.post': return await this.postStream(command, payload);
|
|
342
|
+
case 'member.add': return await this.addMember(command, payload);
|
|
343
|
+
case 'member.bind': return await this.bindMemberSession(command, payload);
|
|
344
|
+
case 'member.fail': return await this.failMember(command, payload);
|
|
345
|
+
case 'member.retire': return await this.retireMember(command, payload);
|
|
346
|
+
case 'member.profile.update': return await this.updateMemberProfile(command, payload);
|
|
347
|
+
case 'member.evolve': return await this.evolveMember(command, payload);
|
|
348
|
+
case 'member.distill': return await this.distillMember(command, payload);
|
|
349
|
+
case 'member.leave': return await this.leaveMember(command, payload);
|
|
350
|
+
case 'member.rejoin': return await this.rejoinMember(command, payload);
|
|
351
|
+
case 'member.takeover': return await this.takeoverMember(command, payload);
|
|
352
|
+
case 'member.message': return await this.messageMember(command, payload);
|
|
353
|
+
case 'member.note': return await this.addMemberNote(command, payload);
|
|
354
|
+
case 'member.skill': return await this.addMemberSkill(command, payload);
|
|
355
|
+
case 'member.config.update': return await this.updateMemberConfig(command, payload);
|
|
356
|
+
case 'member.retry': return await this.retryMember(command, payload);
|
|
357
|
+
case 'task.create': return await this.createTask(command, payload);
|
|
358
|
+
case 'task.claim': return await this.taskCommand(command, 'claim');
|
|
359
|
+
case 'task.release': return await this.taskCommand(command, 'release');
|
|
360
|
+
case 'task.assign': return await this.taskCommand(command, 'assign');
|
|
361
|
+
case 'task.submit': return await this.taskCommand(command, 'submit');
|
|
362
|
+
case 'task.accept': return await this.taskCommand(command, 'accept');
|
|
363
|
+
case 'task.request_changes': return await this.taskCommand(command, 'request_changes');
|
|
364
|
+
case 'task.cancel': return await this.taskCommand(command, 'cancel');
|
|
365
|
+
case 'task.reopen': return await this.taskCommand(command, 'reopen');
|
|
366
|
+
case 'task.reassign': return await this.taskCommand(command, 'reassign');
|
|
367
|
+
case 'task.delete': return await this.taskCommand(command, 'delete');
|
|
368
|
+
case 'task.convert': return await this.convert(command, payload);
|
|
369
|
+
case 'task.follow': return await this.follow(command, payload);
|
|
370
|
+
case 'thread.post': return await this.postThread(command, payload);
|
|
371
|
+
case 'inbox.ack': return await this.ack(command, payload);
|
|
372
|
+
case 'decision.request': return await this.requestDecision(command, payload);
|
|
373
|
+
case 'decision.answer': return await this.answerDecision(command, payload);
|
|
374
|
+
default: fail(ERRORS.INVALID_ARGUMENT, `unknown command: ${String(command.type)}`);
|
|
375
|
+
}
|
|
376
|
+
} catch (error) {
|
|
377
|
+
this.diag?.append({
|
|
378
|
+
ev: 'dispatch.fail',
|
|
379
|
+
type: command.type,
|
|
380
|
+
code: error?.code ?? 'INTERNAL',
|
|
381
|
+
projectId: command.projectId ?? null,
|
|
382
|
+
message: error?.message ?? String(error),
|
|
383
|
+
elapsedMs: Date.now() - startedAt,
|
|
384
|
+
});
|
|
385
|
+
throw error;
|
|
386
|
+
} finally {
|
|
387
|
+
this.diag?.append({
|
|
388
|
+
ev: 'dispatch.done',
|
|
389
|
+
type: command.type,
|
|
390
|
+
projectId: command.projectId ?? null,
|
|
391
|
+
elapsedMs: Date.now() - startedAt,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
async append(command, type, payload) {
|
|
397
|
+
const event = await this.ledger.append({
|
|
398
|
+
type,
|
|
399
|
+
payload,
|
|
400
|
+
actor: command.actor,
|
|
401
|
+
correlationId: command.correlationId,
|
|
402
|
+
at: this.now(),
|
|
403
|
+
});
|
|
404
|
+
return { event, result: payload };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
async createProject(command, payload) {
|
|
408
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may create a Project');
|
|
409
|
+
if (typeof payload.name !== 'string' || payload.name.trim() === '') fail(ERRORS.INVALID_ARGUMENT, 'project name is required');
|
|
410
|
+
const id = this.ledger.projectId ?? payload.id ?? ids.project();
|
|
411
|
+
const ownerMemberId = payload.ownerId ?? command.actor.memberId ?? this.humanId();
|
|
412
|
+
const project = {
|
|
413
|
+
id,
|
|
414
|
+
name: payload.name.trim(),
|
|
415
|
+
goal: payload.goal ?? '',
|
|
416
|
+
ownerMemberId,
|
|
417
|
+
workspacePath: payload.workspacePath ?? '',
|
|
418
|
+
status: 'active',
|
|
419
|
+
createdAt: this.now(),
|
|
420
|
+
};
|
|
421
|
+
await this.append({ ...command, projectId: id }, 'project/created', {
|
|
422
|
+
name: project.name,
|
|
423
|
+
goal: project.goal,
|
|
424
|
+
ownerMemberId,
|
|
425
|
+
workspacePath: project.workspacePath,
|
|
426
|
+
pmoAuto: payload.pmoAuto !== false,
|
|
427
|
+
status: project.status,
|
|
428
|
+
});
|
|
429
|
+
return { project };
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
async changeProjectWorkspace(command, payload) {
|
|
433
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may change the Project workspace');
|
|
434
|
+
const projectId = command.projectId;
|
|
435
|
+
const project = this.state.projects?.[projectId];
|
|
436
|
+
if (project === undefined) fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
437
|
+
if (typeof payload.workspacePath !== 'string' || payload.workspacePath.trim() === '') {
|
|
438
|
+
fail(ERRORS.INVALID_ARGUMENT, 'workspacePath is required');
|
|
439
|
+
}
|
|
440
|
+
const workspacePath = payload.workspacePath.trim();
|
|
441
|
+
// P5b:目录必须真实存在且是目录——配置不存在的目录会让成员会话 cwd
|
|
442
|
+
// 无效、回合直接失败(用户实证:working-directory: directory does not exist)。
|
|
443
|
+
let stat = null;
|
|
444
|
+
try {
|
|
445
|
+
stat = statSync(workspacePath);
|
|
446
|
+
} catch {
|
|
447
|
+
fail(ERRORS.INVALID_ARGUMENT, `工作目录不存在:${workspacePath};请先创建该目录,或选择已存在的目录`);
|
|
448
|
+
}
|
|
449
|
+
if (stat !== null && typeof stat.isDirectory === 'function' && stat.isDirectory() !== true) {
|
|
450
|
+
fail(ERRORS.INVALID_ARGUMENT, `工作目录不是目录:${workspacePath}`);
|
|
451
|
+
}
|
|
452
|
+
await this.append(command, 'project/workspace-changed', {
|
|
453
|
+
projectId,
|
|
454
|
+
workspacePath,
|
|
455
|
+
reason: payload.reason ?? '',
|
|
456
|
+
});
|
|
457
|
+
return { project: { ...project, workspacePath } };
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// P4:项目归档(软删)——账本 append-only 纪律下不可物理删除;archived
|
|
461
|
+
// 项目从工作台消失(bootstrap 过滤),成员的 DSH 会话不受影响。
|
|
462
|
+
// P6:项目运行态(启动/停止/交接)—— Human 的全局掌控面。
|
|
463
|
+
// 'stopped':PMO tick 停止派发(补派/心跳/升级/continue 全停),正在执行
|
|
464
|
+
// 的回合自然完成;'running'(默认):正常驱动。start 时 explicitStart=true
|
|
465
|
+
// 解除空项目的 P1a 等待门(Human 显式启动即指令)。
|
|
466
|
+
async setProjectRunState(command, payload) {
|
|
467
|
+
const projectId = command.projectId;
|
|
468
|
+
const project = this.state.projects?.[projectId];
|
|
469
|
+
if (project === undefined) fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
470
|
+
const runState = payload.runState;
|
|
471
|
+
if (runState !== 'running' && runState !== 'stopped') fail(ERRORS.INVALID_ARGUMENT, 'runState must be running or stopped');
|
|
472
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may change the project run state');
|
|
473
|
+
await this.append(command, 'project/run-state', { projectId, runState, reason: payload.reason ?? '' });
|
|
474
|
+
return { project: { ...project, runState, explicitStart: runState === 'running' ? true : project.explicitStart === true } };
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
async handoffProject(command, payload) {
|
|
479
|
+
const projectId = command.projectId;
|
|
480
|
+
const project = this.state.projects?.[projectId];
|
|
481
|
+
if (project === undefined) fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
482
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may request a handoff');
|
|
483
|
+
if (typeof project.workspacePath !== 'string' || project.workspacePath.trim() === '') {
|
|
484
|
+
fail(ERRORS.INVALID_STATE, '项目未配置工作目录,无法落盘交接文档');
|
|
485
|
+
}
|
|
486
|
+
const nowMs = this.now();
|
|
487
|
+
const markdown = handoffMarkdown(this.state, project, nowMs);
|
|
488
|
+
const docsDir = pathDefault.join(project.workspacePath, 'docs');
|
|
489
|
+
mkdirSync(docsDir, { recursive: true });
|
|
490
|
+
const stamp = new Date(nowMs).toISOString().replace(/[:T]/g, '-').slice(0, 19);
|
|
491
|
+
const filePath = pathDefault.join(docsDir, `HANDOFF-${stamp}.md`);
|
|
492
|
+
writeFileSync(filePath, markdown, 'utf8');
|
|
493
|
+
await this.append(command, 'project/handoff', { projectId, filePath, reason: payload.reason ?? '' });
|
|
494
|
+
return { project, filePath };
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
async setProjectArchive(command, payload) {
|
|
498
|
+
const projectId = command.projectId;
|
|
499
|
+
const project = this.state.projects?.[projectId];
|
|
500
|
+
if (project === undefined) fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
501
|
+
const archived = payload.archived !== false;
|
|
502
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may archive a project');
|
|
503
|
+
if (archived === false && project.status === 'archived') fail(ERRORS.INVALID_STATE, 'archived projects cannot be un-archived in this version');
|
|
504
|
+
// 归档许可由 Host 层判定(registry 是全局项目清单;本 service 只持有
|
|
505
|
+
// 自己项目的账本,无法看到其它项目——旧实现的「最后 active 项目」检查
|
|
506
|
+
// 在本账本里恒为 1,导致所有归档都被误拒 409)。
|
|
507
|
+
await this.append(command, 'project/archived', { projectId, archived, reason: payload.reason ?? '' });
|
|
508
|
+
return { project: { ...project, status: archived ? 'archived' : 'active' } };
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
async setProjectPmo(command, payload) { const projectId = command.projectId;
|
|
512
|
+
const project = this.state.projects?.[projectId];
|
|
513
|
+
if (project === undefined) fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
514
|
+
if (typeof payload.pmoAuto !== 'boolean') fail(ERRORS.INVALID_ARGUMENT, 'pmoAuto must be boolean');
|
|
515
|
+
const isOwner = command.actor.kind === 'agent' && command.actor.memberId === project.ownerMemberId;
|
|
516
|
+
if (command.actor.kind !== 'human' && !isOwner) fail(ERRORS.UNAUTHORIZED, 'only Human or the Project owner may change PMO mode');
|
|
517
|
+
await this.append(command, 'project/pmo-updated', { projectId, pmoAuto: payload.pmoAuto, reason: payload.reason ?? '' });
|
|
518
|
+
return { project: { ...project, pmoAuto: payload.pmoAuto } };
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
async changeProjectOwner(command, payload) {
|
|
522
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may change the Project owner');
|
|
523
|
+
const projectId = command.projectId;
|
|
524
|
+
const project = this.state.projects?.[projectId];
|
|
525
|
+
if (project === undefined) fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
526
|
+
const ownerMemberId = payload.ownerMemberId;
|
|
527
|
+
if (typeof ownerMemberId !== 'string' || this.state.members?.[ownerMemberId] === undefined) {
|
|
528
|
+
fail(ERRORS.INVALID_ARGUMENT, 'ownerMemberId must name an existing member');
|
|
529
|
+
}
|
|
530
|
+
await this.append(command, 'project/owner-changed', { projectId, ownerMemberId, reason: payload.reason ?? '' });
|
|
531
|
+
return { project: { ...project, ownerMemberId } };
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
async postStream(command, payload) {
|
|
535
|
+
const body = typeof payload.body === 'string' ? payload.body.trim() : '';
|
|
536
|
+
if (body === '') fail(ERRORS.INVALID_ARGUMENT, 'stream message body is required');
|
|
537
|
+
const author = this.state.members?.[command.actor.memberId];
|
|
538
|
+
if (author === undefined) fail(ERRORS.UNAUTHORIZED, 'actor must be a Team member');
|
|
539
|
+
// seq 由账本事件定(fold 用 event.seq;payload 里的幻影计数会被忽略,
|
|
540
|
+
// 并发下还会重复——曾造成 API 返回值与投影不一致)。
|
|
541
|
+
const entry = {
|
|
542
|
+
kind: 'human_message',
|
|
543
|
+
authorId: author.id,
|
|
544
|
+
body,
|
|
545
|
+
at: this.now(),
|
|
546
|
+
};
|
|
547
|
+
const event = await this.ledger.append({
|
|
548
|
+
type: 'stream/posted',
|
|
549
|
+
payload: entry,
|
|
550
|
+
actor: command.actor,
|
|
551
|
+
projectId: command.projectId,
|
|
552
|
+
at: this.now(),
|
|
553
|
+
});
|
|
554
|
+
return { entry: { ...entry, seq: event.seq }, event };
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
async addMember(command, payload) {
|
|
558
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may create a member');
|
|
559
|
+
if (typeof payload.name !== 'string' || payload.name.trim() === '') fail(ERRORS.INVALID_ARGUMENT, 'member name is required');
|
|
560
|
+
const member = {
|
|
561
|
+
id: payload.id ?? ids.member(),
|
|
562
|
+
name: payload.name.trim(),
|
|
563
|
+
kind: payload.kind === 'human' ? 'human' : 'agent',
|
|
564
|
+
role: payload.role ?? '',
|
|
565
|
+
personality: typeof payload.personality === 'string' ? payload.personality : '',
|
|
566
|
+
status: typeof payload.status === 'string' ? payload.status : 'idle',
|
|
567
|
+
createdAt: this.now(),
|
|
568
|
+
};
|
|
569
|
+
await this.append(command, 'project/member-added', {
|
|
570
|
+
memberId: member.id,
|
|
571
|
+
name: member.name,
|
|
572
|
+
kind: member.kind,
|
|
573
|
+
role: member.role,
|
|
574
|
+
personality: member.personality,
|
|
575
|
+
status: member.status,
|
|
576
|
+
projectId: payload.projectId,
|
|
577
|
+
});
|
|
578
|
+
return { member };
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
async recordWake(memberId, info, projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
582
|
+
const event = {
|
|
583
|
+
type: info.ok === true ? 'wake/delivered' : 'wake/failed',
|
|
584
|
+
payload: { memberId, taskId: info.taskId ?? null, reason: info.reason ?? null, messageId: info.messageId ?? null, error: info.error ?? null },
|
|
585
|
+
actor: { kind: 'system' },
|
|
586
|
+
projectId,
|
|
587
|
+
at: this.now(),
|
|
588
|
+
};
|
|
589
|
+
await this.ledger.append(event);
|
|
590
|
+
return event;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
async bindHostSession(sessionId, projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
594
|
+
if (typeof sessionId !== 'string' || sessionId === '') fail(ERRORS.INVALID_ARGUMENT, 'host sessionId is required');
|
|
595
|
+
await this.ledger.append({
|
|
596
|
+
type: 'team/host-bound',
|
|
597
|
+
payload: { sessionId },
|
|
598
|
+
actor: { kind: 'system' },
|
|
599
|
+
projectId,
|
|
600
|
+
at: this.now(),
|
|
601
|
+
});
|
|
602
|
+
return { sessionId };
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
async bindMemberSession(command, payload) {
|
|
606
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may bind a member session');
|
|
607
|
+
const member = this.state.members?.[payload.memberId];
|
|
608
|
+
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(payload.memberId)} not found`);
|
|
609
|
+
// 退役成员不得经 bind 复活;重新入队必须走 member.rejoin(清 talentPool)。
|
|
610
|
+
if (member.status === 'retired') fail(ERRORS.INVALID_STATE, `member ${member.name ?? member.id} is retired`);
|
|
611
|
+
if (typeof payload.sessionId !== 'string' || payload.sessionId === '') fail(ERRORS.INVALID_ARGUMENT, 'sessionId is required');
|
|
612
|
+
await this.append(command, 'member/session-bound', {
|
|
613
|
+
memberId: member.id,
|
|
614
|
+
sessionId: payload.sessionId,
|
|
615
|
+
status: 'running',
|
|
616
|
+
});
|
|
617
|
+
return { member: { ...member, sessionId: payload.sessionId, status: 'running' } };
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
async failMember(command, payload) {
|
|
621
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may mark a member failed');
|
|
622
|
+
const member = this.state.members?.[payload.memberId];
|
|
623
|
+
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(payload.memberId)} not found`);
|
|
624
|
+
await this.append(command, 'member/failed', { memberId: member.id, reason: payload.reason ?? '' });
|
|
625
|
+
return { member: { ...member, status: 'failed', failure: payload.reason ?? '' } };
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
async retireMember(command, payload) {
|
|
629
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may retire a member');
|
|
630
|
+
const member = this.state.members?.[payload.memberId];
|
|
631
|
+
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(payload.memberId)} not found`);
|
|
632
|
+
await this.append(command, 'project/member-retired', { memberId: member.id });
|
|
633
|
+
return { member: { ...member, status: 'retired' } };
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
async updateMemberProfile(command, payload) {
|
|
637
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may change name, role or personality');
|
|
638
|
+
const member = this.state.members?.[payload.memberId];
|
|
639
|
+
if (member === undefined || member.kind !== 'agent') fail(ERRORS.NOT_FOUND, `agent ${String(payload.memberId)} not found`);
|
|
640
|
+
const text = (value) => (typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined);
|
|
641
|
+
const name = text(payload.name);
|
|
642
|
+
const role = text(payload.role);
|
|
643
|
+
// An empty personality means "leave it unchanged", not "clear it".
|
|
644
|
+
const personality = text(payload.personality);
|
|
645
|
+
if (name === undefined && role === undefined && personality === undefined) {
|
|
646
|
+
fail(ERRORS.INVALID_ARGUMENT, 'name, role or personality is required');
|
|
647
|
+
}
|
|
648
|
+
await this.append(command, 'member/profile-updated', {
|
|
649
|
+
memberId: member.id,
|
|
650
|
+
...(name === undefined ? {} : { name }),
|
|
651
|
+
...(role === undefined ? {} : { role }),
|
|
652
|
+
...(personality === undefined ? {} : { personality }),
|
|
653
|
+
updatedBy: command.actor.memberId,
|
|
654
|
+
reason: payload.reason ?? '',
|
|
655
|
+
});
|
|
656
|
+
return { member: this.state.members[member.id] };
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/** An Agent may evolve its own working personality, never its name/role. */
|
|
660
|
+
async evolveMember(command, payload) {
|
|
661
|
+
if (command.actor.kind !== 'agent') fail(ERRORS.UNAUTHORIZED, 'only an Agent may evolve its personality');
|
|
662
|
+
const member = this.state.members?.[command.actor.memberId];
|
|
663
|
+
if (member === undefined) fail(ERRORS.NOT_FOUND, 'actor member not found');
|
|
664
|
+
if (payload.memberId !== undefined && payload.memberId !== member.id) {
|
|
665
|
+
fail(ERRORS.UNAUTHORIZED, 'an Agent may only evolve itself');
|
|
666
|
+
}
|
|
667
|
+
const personality = typeof payload.personality === 'string' ? payload.personality.trim() : '';
|
|
668
|
+
if (personality === '') fail(ERRORS.INVALID_ARGUMENT, 'personality is required');
|
|
669
|
+
await this.append(command, 'member/distilled', {
|
|
670
|
+
memberId: member.id,
|
|
671
|
+
personality,
|
|
672
|
+
updatedBy: member.id,
|
|
673
|
+
reason: payload.reason ?? 'self-evolution',
|
|
674
|
+
});
|
|
675
|
+
return { member: this.state.members[member.id] };
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/** Human/Lead-triggered distillation snapshot; model quality is a later step. */
|
|
679
|
+
async distillMember(command, payload) {
|
|
680
|
+
if (command.actor.kind !== 'human' && command.actor.kind !== 'agent') fail(ERRORS.UNAUTHORIZED, 'a member is required');
|
|
681
|
+
const member = this.state.members?.[payload.memberId];
|
|
682
|
+
if (member === undefined || member.kind !== 'agent') fail(ERRORS.NOT_FOUND, `agent ${String(payload.memberId)} not found`);
|
|
683
|
+
const lead = this.state.projects?.[command.projectId]?.ownerMemberId === command.actor.memberId;
|
|
684
|
+
if (command.actor.kind !== 'human' && !lead && command.actor.memberId !== member.id) {
|
|
685
|
+
fail(ERRORS.UNAUTHORIZED, 'only Human, the Project owner or the member may distill');
|
|
686
|
+
}
|
|
687
|
+
const personality = typeof payload.personality === 'string' && payload.personality.trim() !== ''
|
|
688
|
+
? payload.personality.trim()
|
|
689
|
+
: member.personality ?? '';
|
|
690
|
+
if (personality === '') fail(ERRORS.INVALID_ARGUMENT, 'personality is required for distillation');
|
|
691
|
+
await this.append(command, 'member/distilled', {
|
|
692
|
+
memberId: member.id,
|
|
693
|
+
personality,
|
|
694
|
+
updatedBy: command.actor.memberId,
|
|
695
|
+
reason: payload.reason ?? 're-distill',
|
|
696
|
+
});
|
|
697
|
+
return { member: this.state.members[member.id] };
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
async messageMember(command, payload) {
|
|
701
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may message a member');
|
|
702
|
+
const member = this.state.members?.[payload.memberId];
|
|
703
|
+
if (member === undefined || member.kind !== 'agent') fail(ERRORS.NOT_FOUND, `agent ${String(payload.memberId)} not found`);
|
|
704
|
+
const body = typeof payload.body === 'string' ? payload.body.trim() : '';
|
|
705
|
+
if (body === '') fail(ERRORS.INVALID_ARGUMENT, 'message body is required');
|
|
706
|
+
const event = await this.ledger.append({
|
|
707
|
+
type: 'member/message',
|
|
708
|
+
payload: { memberId: member.id, authorId: command.actor.memberId, body, at: this.now() },
|
|
709
|
+
actor: command.actor,
|
|
710
|
+
correlationId: command.correlationId,
|
|
711
|
+
projectId: command.projectId,
|
|
712
|
+
at: this.now(),
|
|
713
|
+
});
|
|
714
|
+
return { event, memberId: member.id };
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
async addMemberNote(command, payload) {
|
|
718
|
+
const member = this.state.members?.[payload.memberId ?? command.actor.memberId];
|
|
719
|
+
if (member === undefined || member.kind !== 'agent') fail(ERRORS.NOT_FOUND, `agent ${String(payload.memberId ?? command.actor.memberId)} not found`);
|
|
720
|
+
if (command.actor.kind === 'agent' && command.actor.memberId !== member.id) {
|
|
721
|
+
fail(ERRORS.UNAUTHORIZED, 'an Agent may only note itself');
|
|
722
|
+
}
|
|
723
|
+
const text = typeof payload.text === 'string' ? payload.text.trim() : '';
|
|
724
|
+
if (text === '') fail(ERRORS.INVALID_ARGUMENT, 'note text is required');
|
|
725
|
+
await this.append(command, 'member/note-added', {
|
|
726
|
+
memberId: member.id,
|
|
727
|
+
text,
|
|
728
|
+
addedBy: command.actor.memberId,
|
|
729
|
+
});
|
|
730
|
+
return { member: this.state.members[member.id] };
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
async addMemberSkill(command, payload) {
|
|
734
|
+
const member = this.state.members?.[payload.memberId ?? command.actor.memberId];
|
|
735
|
+
if (member === undefined || member.kind !== 'agent') fail(ERRORS.NOT_FOUND, `agent ${String(payload.memberId ?? command.actor.memberId)} not found`);
|
|
736
|
+
if (command.actor.kind === 'agent' && command.actor.memberId !== member.id) {
|
|
737
|
+
fail(ERRORS.UNAUTHORIZED, 'an Agent may only add its own skill');
|
|
738
|
+
}
|
|
739
|
+
const name = typeof payload.name === 'string' ? payload.name.trim() : '';
|
|
740
|
+
if (name === '') fail(ERRORS.INVALID_ARGUMENT, 'skill name is required');
|
|
741
|
+
await this.append(command, 'member/skill-added', {
|
|
742
|
+
memberId: member.id,
|
|
743
|
+
name,
|
|
744
|
+
addedBy: command.actor.memberId,
|
|
745
|
+
});
|
|
746
|
+
return { member: this.state.members[member.id] };
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
async leaveMember(command, payload) {
|
|
750
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may move a member to the talent pool');
|
|
751
|
+
const member = this.state.members?.[payload.memberId];
|
|
752
|
+
if (member === undefined || member.kind !== 'agent') fail(ERRORS.NOT_FOUND, `agent ${String(payload.memberId)} not found`);
|
|
753
|
+
const unfinished = unfinishedTasksFor(this.state, member.id);
|
|
754
|
+
await this.append(command, 'member/left-team', { memberId: member.id, reason: payload.reason ?? '' });
|
|
755
|
+
return {
|
|
756
|
+
member: this.state.members[member.id],
|
|
757
|
+
unfinishedTasks: unfinished.map((task) => ({ id: task.id, number: task.number, title: task.title, status: task.status })),
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
async rejoinMember(command, payload) {
|
|
762
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may recruit from the talent pool');
|
|
763
|
+
const member = this.state.members?.[payload.memberId];
|
|
764
|
+
if (member === undefined || member.kind !== 'agent') fail(ERRORS.NOT_FOUND, `agent ${String(payload.memberId)} not found`);
|
|
765
|
+
if (member.status !== 'retired') fail(ERRORS.INVALID_STATE, 'member is already in the team');
|
|
766
|
+
await this.append(command, 'member/rejoined', { memberId: member.id, reason: payload.reason ?? '' });
|
|
767
|
+
return { member: this.state.members[member.id] };
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
async takeoverMember(command, payload) {
|
|
771
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may assign a successor');
|
|
772
|
+
const from = this.state.members?.[payload.fromMemberId];
|
|
773
|
+
const to = this.state.members?.[payload.toMemberId];
|
|
774
|
+
if (from === undefined) fail(ERRORS.NOT_FOUND, `member ${String(payload.fromMemberId)} not found`);
|
|
775
|
+
if (to === undefined || to.kind !== 'agent' || to.status === 'retired') {
|
|
776
|
+
fail(ERRORS.INVALID_ARGUMENT, 'successor must be an active Agent');
|
|
777
|
+
}
|
|
778
|
+
const unfinished = unfinishedTasksFor(this.state, from.id);
|
|
779
|
+
const taken = [];
|
|
780
|
+
for (const task of unfinished) {
|
|
781
|
+
await this.append(command, 'task/taken-over', {
|
|
782
|
+
taskId: task.id,
|
|
783
|
+
revision: task.revision + 1,
|
|
784
|
+
fromMemberId: from.id,
|
|
785
|
+
toMemberId: to.id,
|
|
786
|
+
reason: payload.reason ?? `takeover from ${from.name}`,
|
|
787
|
+
});
|
|
788
|
+
taken.push(this.state.tasks[task.id]);
|
|
789
|
+
}
|
|
790
|
+
return { from: from.id, to: to.id, tasks: taken };
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
/** Lead (Project owner) or Human may update another member's config. */
|
|
794
|
+
async updateMemberConfig(command, payload) {
|
|
795
|
+
const member = this.state.members?.[payload.memberId];
|
|
796
|
+
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(payload.memberId)} not found`);
|
|
797
|
+
const project = this.state.projects?.[command.projectId];
|
|
798
|
+
const isHuman = command.actor.kind === 'human';
|
|
799
|
+
const isLead = project?.ownerMemberId === command.actor.memberId || project?.ownerId === command.actor.memberId;
|
|
800
|
+
if (!isHuman && !isLead) fail(ERRORS.UNAUTHORIZED, 'only the Project owner or Human may update member config');
|
|
801
|
+
if (command.actor.memberId === member.id) {
|
|
802
|
+
fail(ERRORS.UNAUTHORIZED, 'a member cannot update its own config; Lead self-config is Human-only');
|
|
803
|
+
}
|
|
804
|
+
if (Number.isSafeInteger(payload.expectedConfigVersion) && payload.expectedConfigVersion !== (member.configVersion ?? 0)) {
|
|
805
|
+
fail(ERRORS.REVISION, 'member config changed since you read it', {
|
|
806
|
+
details: { expected: payload.expectedConfigVersion, current: member.configVersion ?? 0 },
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
const current = normalizeMemberConfig(member.config ?? {});
|
|
810
|
+
const result = applyMemberConfigPatch(current, payload.patch ?? {}, {
|
|
811
|
+
policy: { ...this.memberConfigPolicy, ...(payload.policy ?? {}) },
|
|
812
|
+
configVersion: member.configVersion ?? 0,
|
|
813
|
+
});
|
|
814
|
+
if (!result.changed) return { member, config: current, changed: false };
|
|
815
|
+
const nextConfig = {
|
|
816
|
+
...result.config,
|
|
817
|
+
updatedBy: command.actor.memberId ?? command.actor.kind,
|
|
818
|
+
updatedAt: this.now(),
|
|
819
|
+
reason: typeof payload.reason === 'string' ? payload.reason : '',
|
|
820
|
+
};
|
|
821
|
+
await this.append(command, 'member/config-updated', {
|
|
822
|
+
memberId: member.id,
|
|
823
|
+
config: nextConfig,
|
|
824
|
+
configVersion: nextConfig.configVersion,
|
|
825
|
+
before: result.before,
|
|
826
|
+
reason: nextConfig.reason,
|
|
827
|
+
});
|
|
828
|
+
return { member: { ...member, config: nextConfig, configVersion: nextConfig.configVersion }, config: nextConfig, changed: true };
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
async retryMember(command, payload) {
|
|
832
|
+
const member = this.state.members?.[payload.memberId];
|
|
833
|
+
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(payload.memberId)} not found`);
|
|
834
|
+
// 退役成员的重启必须走 member.rejoin(清 talentPool/leftTeamAt)。
|
|
835
|
+
if (member.status === 'retired') fail(ERRORS.INVALID_STATE, `member ${member.name ?? member.id} is retired; use member.rejoin`);
|
|
836
|
+
const project = this.state.projects?.[command.projectId];
|
|
837
|
+
const isHuman = command.actor.kind === 'human';
|
|
838
|
+
const isLead = project?.ownerMemberId === command.actor.memberId || project?.ownerId === command.actor.memberId;
|
|
839
|
+
if (!isHuman && !isLead) fail(ERRORS.UNAUTHORIZED, 'only the Project owner or Human may retry a member');
|
|
840
|
+
await this.append(command, 'member/retry-requested', {
|
|
841
|
+
memberId: member.id,
|
|
842
|
+
reason: typeof payload.reason === 'string' ? payload.reason : '',
|
|
843
|
+
});
|
|
844
|
+
return { member: { ...member, status: 'provisioning' } };
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
/** Host/Waker calls these; actor is system, never a Human decision. */
|
|
848
|
+
async recordMemberFailure(memberId, failure, projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
849
|
+
const member = this.state.members?.[memberId];
|
|
850
|
+
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(memberId)} not found`);
|
|
851
|
+
const attempt = Number.isSafeInteger(failure.attempt) ? failure.attempt : (member.lastFailure?.attempt ?? 0) + 1;
|
|
852
|
+
const fatal = failure.category === 'auth' || failure.category === 'billing' || failure.category === 'policy' || failure.category === 'unknown';
|
|
853
|
+
// Tool-argument and provider dumps can run to kilobytes; the ledger and
|
|
854
|
+
// the risk list only ever need a human-readable first act of the error.
|
|
855
|
+
const message = typeof failure.message === 'string' && failure.message.length > 400
|
|
856
|
+
? failure.message.slice(0, 400) + '…'
|
|
857
|
+
: failure.message;
|
|
858
|
+
const recorded = { ...failure, message, attempt };
|
|
859
|
+
await this.ledger.append({
|
|
860
|
+
type: 'member/llm-failed',
|
|
861
|
+
payload: { ...recorded, memberId, fatal },
|
|
862
|
+
actor: { kind: 'system' },
|
|
863
|
+
projectId,
|
|
864
|
+
at: this.now(),
|
|
865
|
+
});
|
|
866
|
+
this.diag?.append({
|
|
867
|
+
ev: fatal ? 'member.fail' : 'member.llm-failed',
|
|
868
|
+
memberId,
|
|
869
|
+
category: recorded.category,
|
|
870
|
+
attempt,
|
|
871
|
+
provider: recorded.provider ?? null,
|
|
872
|
+
model: recorded.model ?? null,
|
|
873
|
+
projectId,
|
|
874
|
+
});
|
|
875
|
+
let inboxItem = null;
|
|
876
|
+
if (fatal) {
|
|
877
|
+
const humanId = this.humanId();
|
|
878
|
+
const duplicate = humanId === null ? undefined : Object.values(this.state.inbox ?? {}).find((item) =>
|
|
879
|
+
item.memberId === humanId
|
|
880
|
+
&& item.requiresDecision === true
|
|
881
|
+
&& item.ref?.memberId === memberId
|
|
882
|
+
&& !['resolved', 'dismissed', 'superseded'].includes(item.state));
|
|
883
|
+
if (humanId !== null && duplicate === undefined) {
|
|
884
|
+
const activeTask = Object.values(this.state.tasks ?? {}).find((task) =>
|
|
885
|
+
task.tombstone !== true
|
|
886
|
+
&& ['pending', 'in_progress', 'pending_acceptance'].includes(task.status)
|
|
887
|
+
&& (task.ownerId === memberId || task.assigneeId === memberId));
|
|
888
|
+
const taskId = recorded.taskId ?? activeTask?.id ?? null;
|
|
889
|
+
inboxItem = createInboxItem({
|
|
890
|
+
memberId: humanId,
|
|
891
|
+
type: 'decision_required',
|
|
892
|
+
ref: { memberId, category: recorded.category, taskId },
|
|
893
|
+
requiresDecision: true,
|
|
894
|
+
}, { at: this.now() });
|
|
895
|
+
await this.ledger.append({
|
|
896
|
+
type: 'inbox/item-created',
|
|
897
|
+
payload: { itemId: inboxItem.id, ...inboxItem },
|
|
898
|
+
actor: { kind: 'system' },
|
|
899
|
+
projectId,
|
|
900
|
+
at: this.now(),
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
return {
|
|
905
|
+
member: { ...member, lastFailure: recorded, attempts: attempt, status: fatal ? 'failed' : member.status },
|
|
906
|
+
inboxItem,
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/** Fallback changes are system-authored config events, rollback-able by Human. */
|
|
911
|
+
async applySystemFallback(memberId, patch, reason = 'llm-fallback', projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
912
|
+
const member = this.state.members?.[memberId];
|
|
913
|
+
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(memberId)} not found`);
|
|
914
|
+
const current = normalizeMemberConfig(member.config ?? {});
|
|
915
|
+
const result = applyMemberConfigPatch(current, patch, {
|
|
916
|
+
policy: this.memberConfigPolicy,
|
|
917
|
+
configVersion: member.configVersion ?? 0,
|
|
918
|
+
});
|
|
919
|
+
if (!result.changed) return { member, config: current, changed: false };
|
|
920
|
+
this.diag?.append({
|
|
921
|
+
ev: 'member.fallback',
|
|
922
|
+
memberId,
|
|
923
|
+
reason,
|
|
924
|
+
patch,
|
|
925
|
+
projectId,
|
|
926
|
+
});
|
|
927
|
+
const nextConfig = {
|
|
928
|
+
...result.config,
|
|
929
|
+
updatedBy: 'system',
|
|
930
|
+
updatedAt: this.now(),
|
|
931
|
+
reason,
|
|
932
|
+
};
|
|
933
|
+
await this.ledger.append({
|
|
934
|
+
type: 'member/config-updated',
|
|
935
|
+
payload: {
|
|
936
|
+
memberId,
|
|
937
|
+
config: nextConfig,
|
|
938
|
+
configVersion: nextConfig.configVersion,
|
|
939
|
+
before: result.before,
|
|
940
|
+
reason,
|
|
941
|
+
},
|
|
942
|
+
actor: { kind: 'system' },
|
|
943
|
+
projectId,
|
|
944
|
+
at: this.now(),
|
|
945
|
+
});
|
|
946
|
+
return { member: { ...member, config: nextConfig, configVersion: nextConfig.configVersion }, config: nextConfig, changed: true };
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
async recordMemberRecovery(memberId, attempt, effectiveConfig, projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
950
|
+
const member = this.state.members?.[memberId];
|
|
951
|
+
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(memberId)} not found`);
|
|
952
|
+
await this.ledger.append({
|
|
953
|
+
type: 'member/recovered',
|
|
954
|
+
payload: { memberId, attempt, effectiveConfig: effectiveConfig ?? member.config ?? null },
|
|
955
|
+
actor: { kind: 'system' },
|
|
956
|
+
projectId,
|
|
957
|
+
at: this.now(),
|
|
958
|
+
});
|
|
959
|
+
this.diag?.append({ ev: 'member.recovered', memberId, attempt, projectId });
|
|
960
|
+
return { member: { ...member, status: 'running', lastRecoveredAt: this.now() } };
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
async createTask(command, payload) {
|
|
964
|
+
const state = this.state;
|
|
965
|
+
const projectId = command.projectId;
|
|
966
|
+
const project = state.projects?.[projectId];
|
|
967
|
+
if (project === undefined) fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
968
|
+
const number = Math.max(0, ...Object.values(state.tasks ?? {}).map((task) => task.number ?? 0)) + 1;
|
|
969
|
+
const ownerId = payload.ownerId ?? project.ownerMemberId ?? project.ownerId;
|
|
970
|
+
const qaRequired = payload.qaRequired === true;
|
|
971
|
+
const qaChecklist = Array.isArray(payload.qaChecklist) ? payload.qaChecklist.filter((item) => typeof item === 'string' && item.trim() !== '') : [];
|
|
972
|
+
if (qaRequired && qaChecklist.length === 0) {
|
|
973
|
+
fail(ERRORS.INVALID_ARGUMENT, 'qaRequired needs at least one qaChecklist item');
|
|
974
|
+
}
|
|
975
|
+
const title = typeof payload.title === 'string' ? payload.title.trim() : '';
|
|
976
|
+
const resultDefinition = typeof payload.resultDefinition === 'string' ? payload.resultDefinition.trim() : '';
|
|
977
|
+
const duplicate = Object.values(state.tasks ?? {}).find((candidate) =>
|
|
978
|
+
candidate.tombstone !== true
|
|
979
|
+
&& ['pending', 'in_progress', 'pending_acceptance'].includes(candidate.status)
|
|
980
|
+
&& candidate.ownerId === ownerId
|
|
981
|
+
&& candidate.title === title
|
|
982
|
+
&& candidate.resultDefinition === resultDefinition);
|
|
983
|
+
if (duplicate !== undefined && payload.allowDuplicate !== true) {
|
|
984
|
+
fail(ERRORS.CONFLICT, 'an active Task with the same title, result definition and owner already exists', {
|
|
985
|
+
details: { taskId: duplicate.id, number: duplicate.number, title: duplicate.title },
|
|
986
|
+
});
|
|
987
|
+
}
|
|
988
|
+
const { task, event } = createTask({
|
|
989
|
+
number,
|
|
990
|
+
title,
|
|
991
|
+
resultDefinition,
|
|
992
|
+
ownerId,
|
|
993
|
+
assigneeId: payload.assigneeId,
|
|
994
|
+
blockedBy: payload.blockedBy,
|
|
995
|
+
writeScopes: payload.writeScopes,
|
|
996
|
+
irreversible: payload.irreversible,
|
|
997
|
+
qaRequired,
|
|
998
|
+
qaChecklist,
|
|
999
|
+
originRefs: payload.originRefs,
|
|
1000
|
+
at: this.now(),
|
|
1001
|
+
});
|
|
1002
|
+
await this.ledger.append({ ...event, actor: command.actor, correlationId: command.correlationId, at: this.now(), projectId });
|
|
1003
|
+
return { task };
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
async taskCommand(command, action) {
|
|
1007
|
+
const state = this.state;
|
|
1008
|
+
const taskId = command.payload?.taskId;
|
|
1009
|
+
const task = state.tasks?.[taskId];
|
|
1010
|
+
if (task === undefined) fail(ERRORS.NOT_FOUND, `task ${String(taskId)} not found`);
|
|
1011
|
+
const expectedRevision = command.payload?.expectedRevision;
|
|
1012
|
+
let outcome;
|
|
1013
|
+
switch (action) {
|
|
1014
|
+
case 'claim': outcome = claimTask(task, command.actor, state.tasks ?? {}, { expectedRevision, at: this.now() }); break;
|
|
1015
|
+
case 'release': outcome = releaseTask(task, command.actor, { expectedRevision, reason: command.payload?.reason, at: this.now() }); break;
|
|
1016
|
+
case 'assign': outcome = assignTask(task, command.actor, command.payload?.assigneeId, { expectedRevision, at: this.now() }); break;
|
|
1017
|
+
case 'submit': outcome = submitTask(task, command.actor, { summary: command.payload?.summary, evidence: command.payload?.evidence }, { expectedRevision, at: this.now() }); break;
|
|
1018
|
+
case 'accept': {
|
|
1019
|
+
// Agent actors are pre-resolved by dispatch; resolve defensively here
|
|
1020
|
+
// so an unknown memberId fails closed instead of skipping the QA gate.
|
|
1021
|
+
const reviewer = command.actor.kind === 'agent'
|
|
1022
|
+
? this.state.members?.[command.actor.memberId] ?? undefined
|
|
1023
|
+
: null;
|
|
1024
|
+
if (command.actor.kind === 'agent' && reviewer === undefined) {
|
|
1025
|
+
fail(ERRORS.UNAUTHORIZED, `agent actor ${String(command.actor.memberId ?? '?')} is not a Team member`);
|
|
1026
|
+
}
|
|
1027
|
+
if (task.qaRequired === true && reviewer !== null && !isQaMember(reviewer)) {
|
|
1028
|
+
fail(ERRORS.INVALID_STATE, 'this task requires a QA reviewer');
|
|
1029
|
+
}
|
|
1030
|
+
const checklist = Array.isArray(task.qaChecklist) ? task.qaChecklist : [];
|
|
1031
|
+
const checks = Array.isArray(command.payload?.checks) ? command.payload.checks : [];
|
|
1032
|
+
if (reviewer !== null && checklist.length > 0) {
|
|
1033
|
+
if (checks.length !== checklist.length || checks.some((check) => check?.passed !== true)) {
|
|
1034
|
+
fail(ERRORS.INVALID_STATE, 'every QA checklist item must pass before acceptance');
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
outcome = acceptTask(task, command.actor, {
|
|
1038
|
+
note: command.payload?.note,
|
|
1039
|
+
confirmed: command.payload?.confirmed,
|
|
1040
|
+
qaChecks: checks,
|
|
1041
|
+
}, { expectedRevision, at: this.now() });
|
|
1042
|
+
break;
|
|
1043
|
+
}
|
|
1044
|
+
case 'request_changes': outcome = requestChangesTask(task, command.actor, { note: command.payload?.note }, { expectedRevision, at: this.now() }); break;
|
|
1045
|
+
case 'cancel': outcome = cancelTask(task, command.actor, { reason: command.payload?.reason, expectedRevision, at: this.now() }); break;
|
|
1046
|
+
case 'reopen': outcome = reopenTask(task, command.actor, { reason: command.payload?.reason, expectedRevision, members: state.members, at: this.now() }); break;
|
|
1047
|
+
case 'reassign': outcome = reassignTask(task, command.actor, command.payload?.ownerId, { expectedRevision, reason: command.payload?.reason, at: this.now() }); break;
|
|
1048
|
+
case 'delete': outcome = deleteTask(task, command.actor, { expectedRevision, at: this.now() }); break;
|
|
1049
|
+
default: fail(ERRORS.INVALID_ARGUMENT, `unknown task action: ${action}`);
|
|
1050
|
+
}
|
|
1051
|
+
await this.ledger.append({ ...outcome.event, actor: command.actor, correlationId: command.correlationId, projectId: command.projectId, at: this.now() });
|
|
1052
|
+
if (action === 'accept') await this.notifyTaskFollowers(outcome.task, command);
|
|
1053
|
+
return { task: outcome.task, event: outcome.event };
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
async convert(command, payload) {
|
|
1057
|
+
const sourceEventIds = payload.sourceEventIds;
|
|
1058
|
+
if (!Array.isArray(sourceEventIds) || sourceEventIds.length === 0) fail(ERRORS.INVALID_ARGUMENT, 'sourceEventIds is required');
|
|
1059
|
+
const state = this.state;
|
|
1060
|
+
const existing = Object.values(state.tasks ?? {}).find((task) =>
|
|
1061
|
+
(task.originRefs ?? []).some((ref) => sourceEventIds.includes(ref)));
|
|
1062
|
+
if (existing !== undefined) {
|
|
1063
|
+
fail(ERRORS.CONFLICT, `already converted to ${existing.id}`, { details: { taskId: existing.id } });
|
|
1064
|
+
}
|
|
1065
|
+
const result = await this.createTask(command, {
|
|
1066
|
+
title: payload.title,
|
|
1067
|
+
resultDefinition: payload.doneCriteria ?? payload.resultDefinition,
|
|
1068
|
+
ownerId: payload.ownerId,
|
|
1069
|
+
assigneeId: payload.assigneeId,
|
|
1070
|
+
writeScopes: payload.writeScopes,
|
|
1071
|
+
originRefs: sourceEventIds,
|
|
1072
|
+
});
|
|
1073
|
+
return result;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
/** Followers get one report when a Task is accepted; unfollow removes future reports. */
|
|
1077
|
+
async notifyTaskFollowers(task, command) {
|
|
1078
|
+
const followers = Array.isArray(this.state.followers?.[task.id]) ? this.state.followers[task.id] : [];
|
|
1079
|
+
for (const followerId of followers) {
|
|
1080
|
+
if (followerId === command.actor.memberId) continue;
|
|
1081
|
+
const duplicate = Object.values(this.state.inbox ?? {}).find((item) =>
|
|
1082
|
+
item.memberId === followerId
|
|
1083
|
+
&& item.type === 'followed_task_update'
|
|
1084
|
+
&& item.ref?.taskId === task.id
|
|
1085
|
+
&& item.ref?.kind === 'completed'
|
|
1086
|
+
&& item.state === 'unread');
|
|
1087
|
+
if (duplicate !== undefined) continue;
|
|
1088
|
+
const item = createInboxItem({
|
|
1089
|
+
memberId: followerId,
|
|
1090
|
+
type: 'followed_task_update',
|
|
1091
|
+
ref: { taskId: task.id, title: task.title, kind: 'completed' },
|
|
1092
|
+
requiresDecision: false,
|
|
1093
|
+
}, { at: this.now() });
|
|
1094
|
+
await this.ledger.append({
|
|
1095
|
+
type: 'inbox/item-created',
|
|
1096
|
+
payload: { itemId: item.id, ...item },
|
|
1097
|
+
actor: { kind: 'system' },
|
|
1098
|
+
projectId: command.projectId,
|
|
1099
|
+
at: this.now(),
|
|
1100
|
+
});
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
async follow(command, payload) {
|
|
1105
|
+
if (typeof payload.taskId !== 'string') fail(ERRORS.INVALID_ARGUMENT, 'taskId is required');
|
|
1106
|
+
const following = payload.following === true;
|
|
1107
|
+
await this.ledger.append({
|
|
1108
|
+
type: following ? 'task/followed' : 'task/unfollowed',
|
|
1109
|
+
payload: { taskId: payload.taskId, memberId: command.actor.memberId ?? this.humanId() },
|
|
1110
|
+
actor: command.actor,
|
|
1111
|
+
correlationId: command.correlationId,
|
|
1112
|
+
projectId: command.projectId,
|
|
1113
|
+
at: this.now(),
|
|
1114
|
+
});
|
|
1115
|
+
return { following };
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
async postThread(command, payload) {
|
|
1119
|
+
const state = this.state;
|
|
1120
|
+
const taskId = payload.taskId;
|
|
1121
|
+
if (state.tasks?.[taskId] === undefined) fail(ERRORS.NOT_FOUND, `task ${String(taskId)} not found`);
|
|
1122
|
+
const thread = state.threads?.[taskId] ?? createThread(taskId);
|
|
1123
|
+
const actorKind = command.actor.kind;
|
|
1124
|
+
const authorId = command.actor.memberId ?? this.humanId();
|
|
1125
|
+
const outcome = appendReply(thread, { authorId, actorKind, baseSeq: payload.baseSeq, body: payload.body, refs: payload.refs }, { at: this.now() });
|
|
1126
|
+
await this.ledger.append({
|
|
1127
|
+
type: 'thread/posted',
|
|
1128
|
+
payload: { taskId, ...outcome.entry },
|
|
1129
|
+
actor: command.actor,
|
|
1130
|
+
correlationId: command.correlationId,
|
|
1131
|
+
projectId: command.projectId,
|
|
1132
|
+
at: this.now(),
|
|
1133
|
+
});
|
|
1134
|
+
return { thread: outcome.thread, entry: outcome.entry };
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
/** PMO: answer all pending structured questions with the max-value policy. */
|
|
1138
|
+
async autoAnswerDecisions(projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
1139
|
+
const humanId = this.humanId();
|
|
1140
|
+
if (humanId === null) return 0;
|
|
1141
|
+
const items = Object.values(this.state.inbox ?? {}).filter((item) =>
|
|
1142
|
+
item.memberId === humanId
|
|
1143
|
+
&& item.type === 'decision_required'
|
|
1144
|
+
&& item.state === 'unread');
|
|
1145
|
+
const results = [];
|
|
1146
|
+
for (const item of items) {
|
|
1147
|
+
const questions = Array.isArray(item.ref?.questions) ? item.ref.questions : [];
|
|
1148
|
+
const answers = chooseDecisionAnswers(questions);
|
|
1149
|
+
results.push(await this.autoAnswerDecision(item, answers, projectId));
|
|
1150
|
+
}
|
|
1151
|
+
return results;
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
async autoAnswerDecision(item, answers, projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
1155
|
+
const taskId = typeof item.ref?.taskId === 'string' ? item.ref.taskId : null;
|
|
1156
|
+
const task = taskId === null ? undefined : this.state.tasks?.[taskId];
|
|
1157
|
+
const questionById = new Map((item.ref?.questions ?? []).map((question) => [question.id, question.text]));
|
|
1158
|
+
const lines = answers.length > 0
|
|
1159
|
+
? answers.map((answer) => `- ${questionById.get(answer.id) ?? answer.id}: ${answer.value}`)
|
|
1160
|
+
: ['- 无结构化问题:按当前 Task/Thread 的最高价值路径推进,不等待 Human。'];
|
|
1161
|
+
const body = `【PMO 自动决策】按最高价值建议执行,不需要 Human 再确认:\n${lines.join('\n')}`;
|
|
1162
|
+
let entry = null;
|
|
1163
|
+
if (task !== undefined) {
|
|
1164
|
+
const thread = this.state.threads?.[task.id] ?? createThread(task.id);
|
|
1165
|
+
const outcome = appendReply(thread, {
|
|
1166
|
+
authorId: 'mem_pmo',
|
|
1167
|
+
actorKind: 'system',
|
|
1168
|
+
baseSeq: thread.headSeq,
|
|
1169
|
+
body,
|
|
1170
|
+
}, { at: this.now() });
|
|
1171
|
+
await this.ledger.append({
|
|
1172
|
+
type: 'thread/posted',
|
|
1173
|
+
payload: { taskId: task.id, ...outcome.entry },
|
|
1174
|
+
actor: { kind: 'system', memberId: 'mem_pmo' },
|
|
1175
|
+
projectId,
|
|
1176
|
+
at: this.now(),
|
|
1177
|
+
});
|
|
1178
|
+
entry = outcome.entry;
|
|
1179
|
+
}
|
|
1180
|
+
const next = ackInboxItem(item, {
|
|
1181
|
+
disposition: 'done',
|
|
1182
|
+
decision: { answers, auto: true, by: 'mem_pmo', body },
|
|
1183
|
+
}, { at: this.now() });
|
|
1184
|
+
this.diag?.append({
|
|
1185
|
+
ev: 'pmo.auto-answer',
|
|
1186
|
+
itemId: item.id,
|
|
1187
|
+
taskId,
|
|
1188
|
+
answers: answers.length,
|
|
1189
|
+
projectId,
|
|
1190
|
+
});
|
|
1191
|
+
await this.ledger.append({
|
|
1192
|
+
type: 'inbox/acked',
|
|
1193
|
+
payload: { itemId: next.id, memberId: next.memberId, state: next.state, disposition: next.disposition, at: next.ackedAt },
|
|
1194
|
+
actor: { kind: 'system', memberId: 'mem_pmo' },
|
|
1195
|
+
projectId,
|
|
1196
|
+
at: this.now(),
|
|
1197
|
+
});
|
|
1198
|
+
return { item: next, entry };
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
/** Surface an explicit Human decision request with all questions inline. */
|
|
1202
|
+
async requestDecision(command, payload) {
|
|
1203
|
+
const humanId = this.humanId();
|
|
1204
|
+
if (humanId === null) fail(ERRORS.NOT_FOUND, 'Human member not found');
|
|
1205
|
+
const raw = Array.isArray(payload.questions) ? payload.questions : [];
|
|
1206
|
+
if (raw.length === 0 || raw.length > 20) fail(ERRORS.INVALID_ARGUMENT, 'questions must contain 1..20 items');
|
|
1207
|
+
const questions = raw.map((question, index) => {
|
|
1208
|
+
if (typeof question === 'string') return { id: `q${index + 1}`, text: question.trim() };
|
|
1209
|
+
if (question === null || typeof question !== 'object') fail(ERRORS.INVALID_ARGUMENT, `questions[${index}] must be a string or object`);
|
|
1210
|
+
const text = typeof question.text === 'string' ? question.text.trim() : '';
|
|
1211
|
+
if (text === '') fail(ERRORS.INVALID_ARGUMENT, `questions[${index}].text is required`);
|
|
1212
|
+
const options = Array.isArray(question.options) ? question.options.filter((option) => typeof option === 'string' && option.trim() !== '').slice(0, 12) : [];
|
|
1213
|
+
return {
|
|
1214
|
+
id: typeof question.id === 'string' && question.id !== '' ? question.id : `q${index + 1}`,
|
|
1215
|
+
text,
|
|
1216
|
+
...(options.length ? { options } : {}),
|
|
1217
|
+
...(typeof question.recommendation === 'string' && question.recommendation !== '' ? { recommendation: question.recommendation } : {}),
|
|
1218
|
+
...(typeof question.blocking === 'string' && question.blocking !== '' ? { blocking: question.blocking } : {}),
|
|
1219
|
+
};
|
|
1220
|
+
});
|
|
1221
|
+
const title = typeof payload.title === 'string' && payload.title.trim() !== '' ? payload.title.trim() : '需要你决策';
|
|
1222
|
+
const taskId = typeof payload.taskId === 'string' ? payload.taskId : null;
|
|
1223
|
+
const duplicate = Object.values(this.state.inbox ?? {}).find((item) =>
|
|
1224
|
+
item.memberId === humanId
|
|
1225
|
+
&& item.type === 'decision_required'
|
|
1226
|
+
&& item.ref?.title === title
|
|
1227
|
+
&& item.ref?.taskId === taskId
|
|
1228
|
+
&& !['resolved', 'dismissed', 'superseded'].includes(item.state));
|
|
1229
|
+
if (duplicate !== undefined) return { item: duplicate, duplicate: true };
|
|
1230
|
+
const item = createInboxItem({
|
|
1231
|
+
memberId: humanId,
|
|
1232
|
+
type: 'decision_required',
|
|
1233
|
+
ref: {
|
|
1234
|
+
taskId,
|
|
1235
|
+
title,
|
|
1236
|
+
memberId: command.actor.memberId ?? null,
|
|
1237
|
+
memberName: this.state.members?.[command.actor.memberId]?.name ?? command.actor.memberId ?? null,
|
|
1238
|
+
questions,
|
|
1239
|
+
...(typeof payload.source === 'string' && payload.source !== '' ? { source: payload.source } : {}),
|
|
1240
|
+
},
|
|
1241
|
+
requiresDecision: true,
|
|
1242
|
+
}, { at: this.now() });
|
|
1243
|
+
await this.ledger.append({
|
|
1244
|
+
type: 'inbox/item-created',
|
|
1245
|
+
payload: { itemId: item.id, ...item },
|
|
1246
|
+
actor: command.actor,
|
|
1247
|
+
correlationId: command.correlationId,
|
|
1248
|
+
projectId: command.projectId,
|
|
1249
|
+
at: this.now(),
|
|
1250
|
+
});
|
|
1251
|
+
return { item, duplicate: false };
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
async answerDecision(command, payload) {
|
|
1255
|
+
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may answer a decision');
|
|
1256
|
+
const item = this.state.inbox?.[payload.itemId];
|
|
1257
|
+
if (item === undefined) fail(ERRORS.NOT_FOUND, `inbox item ${String(payload.itemId)} not found`);
|
|
1258
|
+
if (item.type !== 'decision_required' || item.requiresDecision !== true) {
|
|
1259
|
+
fail(ERRORS.INVALID_STATE, 'this inbox item is not a Human decision');
|
|
1260
|
+
}
|
|
1261
|
+
const humanId = command.actor.memberId;
|
|
1262
|
+
if (item.memberId !== humanId) fail(ERRORS.UNAUTHORIZED, 'decision belongs to another member');
|
|
1263
|
+
const answers = (Array.isArray(payload.answers) ? payload.answers : [])
|
|
1264
|
+
.map((answer, index) => ({
|
|
1265
|
+
id: typeof answer?.id === 'string' && answer.id !== '' ? answer.id : `q${index + 1}`,
|
|
1266
|
+
value: typeof answer?.value === 'string' ? answer.value.trim() : '',
|
|
1267
|
+
}))
|
|
1268
|
+
.filter((answer) => answer.value !== '');
|
|
1269
|
+
if (answers.length === 0) fail(ERRORS.INVALID_ARGUMENT, 'at least one answer is required');
|
|
1270
|
+
const questions = Array.isArray(item.ref?.questions) ? item.ref.questions : [];
|
|
1271
|
+
const lines = answers.map((answer) => {
|
|
1272
|
+
const question = questions.find((candidate) => candidate.id === answer.id);
|
|
1273
|
+
return `- ${question?.text ?? answer.id}: ${answer.value}`;
|
|
1274
|
+
});
|
|
1275
|
+
const body = `Human 决策:\n${lines.join('\n')}${typeof payload.note === 'string' && payload.note.trim() !== '' ? `\n备注:${payload.note.trim()}` : ''}`;
|
|
1276
|
+
let entry = null;
|
|
1277
|
+
if (typeof item.ref?.taskId === 'string' && this.state.tasks?.[item.ref.taskId] !== undefined) {
|
|
1278
|
+
const taskId = item.ref.taskId;
|
|
1279
|
+
const thread = this.state.threads?.[taskId] ?? createThread(taskId);
|
|
1280
|
+
const outcome = appendReply(thread, {
|
|
1281
|
+
authorId: humanId,
|
|
1282
|
+
actorKind: 'human',
|
|
1283
|
+
baseSeq: thread.headSeq,
|
|
1284
|
+
body,
|
|
1285
|
+
}, { at: this.now() });
|
|
1286
|
+
await this.ledger.append({
|
|
1287
|
+
type: 'thread/posted',
|
|
1288
|
+
payload: { taskId, ...outcome.entry },
|
|
1289
|
+
actor: command.actor,
|
|
1290
|
+
projectId: command.projectId,
|
|
1291
|
+
at: this.now(),
|
|
1292
|
+
});
|
|
1293
|
+
entry = outcome.entry;
|
|
1294
|
+
}
|
|
1295
|
+
const next = ackInboxItem(item, { disposition: 'done', decision: { answers, note: payload.note ?? '', body } }, { at: this.now() });
|
|
1296
|
+
await this.ledger.append({
|
|
1297
|
+
type: 'inbox/acked',
|
|
1298
|
+
payload: { itemId: next.id, memberId: next.memberId, state: next.state, disposition: next.disposition, at: next.ackedAt },
|
|
1299
|
+
actor: command.actor,
|
|
1300
|
+
projectId: command.projectId,
|
|
1301
|
+
at: this.now(),
|
|
1302
|
+
});
|
|
1303
|
+
return { item: next, entry };
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
async ack(command, payload) {
|
|
1307
|
+
const state = this.state;
|
|
1308
|
+
const item = state.inbox?.[payload.itemId];
|
|
1309
|
+
if (item === undefined) fail(ERRORS.NOT_FOUND, `inbox item ${String(payload.itemId)} not found`);
|
|
1310
|
+
const next = ackInboxItem(item, { disposition: payload.disposition, decision: payload.decision }, { at: this.now() });
|
|
1311
|
+
await this.ledger.append({
|
|
1312
|
+
type: 'inbox/acked',
|
|
1313
|
+
payload: { itemId: next.id, memberId: next.memberId, state: next.state, disposition: next.disposition, at: next.ackedAt },
|
|
1314
|
+
actor: command.actor,
|
|
1315
|
+
correlationId: command.correlationId,
|
|
1316
|
+
projectId: command.projectId,
|
|
1317
|
+
at: this.now(),
|
|
1318
|
+
});
|
|
1319
|
+
return { item: next };
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
/** Create an inbox item and append it; used by the Waker/observer. */
|
|
1323
|
+
async notify(input, command = { actor: { kind: 'system' } }) {
|
|
1324
|
+
const item = createInboxItem(input, { at: this.now() });
|
|
1325
|
+
await this.ledger.append({
|
|
1326
|
+
type: 'inbox/item-created',
|
|
1327
|
+
payload: { itemId: item.id, ...item },
|
|
1328
|
+
actor: command.actor ?? { kind: 'system' },
|
|
1329
|
+
projectId: command.projectId,
|
|
1330
|
+
at: this.now(),
|
|
1331
|
+
});
|
|
1332
|
+
return item;
|
|
1333
|
+
}
|
|
1334
|
+
}
|