@suifei/dsh-ai-team 0.3.53 → 0.3.56
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 +38 -0
- package/client.js +289 -332
- package/index.js +181 -28
- package/lib/diaglog.js +105 -0
- package/lib/domain/member-profile.js +4 -11
- package/lib/domain/policy.js +31 -0
- package/lib/host/default-roster.js +39 -3
- package/lib/host/member-tools.js +1 -1
- package/lib/host/project-registry.js +6 -1
- package/lib/host/project-runtime.js +31 -3
- package/lib/host/service.js +173 -99
- package/lib/host/views.js +6 -16
- package/lib/host/waker.js +14 -1
- package/lib/ledger/fold.js +36 -8
- package/lib/ledger/writer.js +110 -9
- package/lib/runtime/failure-policy.js +19 -7
- package/lib/runtime/member-runtime.js +6 -1
- package/lib/runtime/skill-provider.js +96 -33
- package/lib/vendor/skillopt/src/index.js +51 -3
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +6 -1
|
@@ -32,6 +32,18 @@ export const DEFAULT_MEMBER_ROSTER = Object.freeze([
|
|
|
32
32
|
role: '编程',
|
|
33
33
|
personality: '负责实现与工程化落地;先读核心纪律,拆分可验证小步,实测证据优先;与 PMO、QA 协同,不抢执行者的 claim。',
|
|
34
34
|
},
|
|
35
|
+
{
|
|
36
|
+
id: 'mem_ux',
|
|
37
|
+
name: '知微',
|
|
38
|
+
role: 'UX 设计',
|
|
39
|
+
personality: '以用户体验为第一核心:研究目标产品的 UI/UE,从真实使用路径找摩擦点;每个改进意见都要落到可验证的交互设计与证据,不凭感觉改版。',
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
id: 'mem_biz',
|
|
43
|
+
name: '观澜',
|
|
44
|
+
role: '业务',
|
|
45
|
+
personality: '从运营/用户视角研究目标系统如何使用,洞察真实需求与使用场景;把需求转成可执行的 UX 改进意见,交 PMO 会议评审立项。',
|
|
46
|
+
},
|
|
35
47
|
]);
|
|
36
48
|
|
|
37
49
|
/** Names shipped by 0.3.9 and earlier; only these exact pins are migrated. */
|
|
@@ -67,14 +79,38 @@ async function upgradeLegacyDefaultNames(service, projectId, humanId) {
|
|
|
67
79
|
|
|
68
80
|
/**
|
|
69
81
|
* Add the default roster to an empty Project and make the Lead its owner.
|
|
70
|
-
* On an existing roster,
|
|
71
|
-
*
|
|
82
|
+
* On an existing roster, exact legacy default names are upgraded and any
|
|
83
|
+
* still-missing core-role member (UX/业务) is appended; custom names, custom
|
|
84
|
+
* roles and the user's own roster are never touched or duplicated.
|
|
72
85
|
* @returns {Promise<{seeded: boolean, members: string[], renamed: string[]}>}
|
|
73
86
|
*/
|
|
74
87
|
export async function seedDefaultRoster(service, projectId, { humanId = 'mem_human' } = {}) {
|
|
75
88
|
if (agentMembers(service).length > 0) {
|
|
76
89
|
const renamed = await upgradeLegacyDefaultNames(service, projectId, humanId);
|
|
77
|
-
|
|
90
|
+
const added = [];
|
|
91
|
+
const existingRoles = new Set(agentMembers(service).map((member) => member.role));
|
|
92
|
+
const existingIds = new Set(Object.keys(service.state.members ?? {}));
|
|
93
|
+
for (const member of DEFAULT_MEMBER_ROSTER) {
|
|
94
|
+
if (existingIds.has(member.id)) continue;
|
|
95
|
+
if (existingRoles.has(member.role)) continue;
|
|
96
|
+
await service.dispatch({
|
|
97
|
+
type: 'member.add',
|
|
98
|
+
actor: { kind: 'human', memberId: humanId },
|
|
99
|
+
projectId,
|
|
100
|
+
payload: {
|
|
101
|
+
id: member.id,
|
|
102
|
+
name: member.name,
|
|
103
|
+
role: member.role,
|
|
104
|
+
personality: member.personality,
|
|
105
|
+
kind: 'agent',
|
|
106
|
+
status: 'idle',
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
existingIds.add(member.id);
|
|
110
|
+
existingRoles.add(member.role);
|
|
111
|
+
added.push(member.id);
|
|
112
|
+
}
|
|
113
|
+
return { seeded: false, members: added, renamed };
|
|
78
114
|
}
|
|
79
115
|
const added = [];
|
|
80
116
|
for (const member of DEFAULT_MEMBER_ROSTER) {
|
package/lib/host/member-tools.js
CHANGED
|
@@ -66,7 +66,7 @@ export function createTeamTool(options) {
|
|
|
66
66
|
evidence: { type: 'array', items: { type: 'string' }, description: 'Evidence strings/paths, at least one for submit.' },
|
|
67
67
|
note: { type: 'string', description: 'Acceptance or changes-requested note.' },
|
|
68
68
|
body: { type: 'string', description: 'Thread reply body.' },
|
|
69
|
-
title: { type: 'string', description: 'Task title for create.' },
|
|
69
|
+
title: { type: 'string', description: 'Task title for create; decision title for askHuman.' },
|
|
70
70
|
resultDefinition: { type: 'string', description: 'Acceptance definition for create.' },
|
|
71
71
|
ownerId: { type: 'string', description: 'Accountable owner id from members for create; defaults to you.' },
|
|
72
72
|
assigneeId: { type: 'string', description: 'Optional assignee id from members for create.' },
|
|
@@ -16,7 +16,12 @@ export class ProjectRegistry {
|
|
|
16
16
|
let parsed;
|
|
17
17
|
try {
|
|
18
18
|
parsed = existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : null;
|
|
19
|
-
} catch {
|
|
19
|
+
} catch (error) {
|
|
20
|
+
// A corrupt index must not silently vanish the user's project list:
|
|
21
|
+
// keep the broken file for forensics, then fall back to the default.
|
|
22
|
+
const kept = `${file}.corrupt-${Date.now()}`;
|
|
23
|
+
try { renameSync(file, kept); } catch { /* rename is best-effort */ }
|
|
24
|
+
console.error(`[ai-team] projects.json was unreadable (${error?.message ?? error}); kept as ${kept}`);
|
|
20
25
|
parsed = null;
|
|
21
26
|
}
|
|
22
27
|
const projects = Array.isArray(parsed?.projects) && parsed.projects.length > 0
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
import { mkdirSync } from 'node:fs';
|
|
11
11
|
import { join } from 'node:path';
|
|
12
|
+
import { FOLD_STATE_VERSION } from '../ledger/fold.js';
|
|
12
13
|
import { openLedger } from '../ledger/writer.js';
|
|
13
14
|
import { seedDefaultRoster } from './default-roster.js';
|
|
14
15
|
import { TeamService } from './service.js';
|
|
@@ -25,15 +26,35 @@ export class ProjectRuntime {
|
|
|
25
26
|
this.service = service;
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
static async open({ rootDir, projectId = DEFAULT_PROJECT_ID, now = undefined, seed = true, projectName = '我的项目', projectGoal = '把事交给团队', workspacePath = '' }) {
|
|
29
|
+
static async open({ rootDir, projectId = DEFAULT_PROJECT_ID, now = undefined, seed = true, projectName = '我的项目', projectGoal = '把事交给团队', workspacePath = '', diag = null }) {
|
|
29
30
|
const dir = join(rootDir, 'projects', projectId);
|
|
30
31
|
mkdirSync(dir, { recursive: true });
|
|
31
|
-
const ledger = openLedger({
|
|
32
|
-
|
|
32
|
+
const ledger = openLedger({
|
|
33
|
+
dir,
|
|
34
|
+
projectId,
|
|
35
|
+
fsync: true,
|
|
36
|
+
// 检查点接线:boot 从投影地板续放尾部,跳过全量 fold;每 500 次
|
|
37
|
+
// 追加与 close 前落盘,崩溃窗口有界(账本本体不受影响)。
|
|
38
|
+
checkpoint: { stateVersion: FOLD_STATE_VERSION, saveEvery: 500 },
|
|
39
|
+
});
|
|
40
|
+
const service = new TeamService({ ledger, now, diag });
|
|
33
41
|
const runtime = new ProjectRuntime({ id: projectId, rootDir, dir, ledger, service });
|
|
34
42
|
|
|
43
|
+
const recovery = ledger.recovery;
|
|
44
|
+
if (recovery !== null && recovery !== undefined) {
|
|
45
|
+
diag?.append({
|
|
46
|
+
ev: 'ledger.checkpoint',
|
|
47
|
+
projectId,
|
|
48
|
+
source: recovery.source,
|
|
49
|
+
fromSeq: recovery.fromSeq,
|
|
50
|
+
errors: recovery.errors?.length ?? 0,
|
|
51
|
+
saved: recovery.saved ?? null,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
35
55
|
const repairs = ledger.opened?.repairs ?? (ledger.opened?.repair ? [ledger.opened.repair] : []);
|
|
36
56
|
if (repairs.length > 0) {
|
|
57
|
+
diag?.append({ ev: 'ledger.repair', projectId, repairs });
|
|
37
58
|
await ledger.append({
|
|
38
59
|
type: 'ledger/repaired',
|
|
39
60
|
payload: { repairs },
|
|
@@ -41,6 +62,13 @@ export class ProjectRuntime {
|
|
|
41
62
|
projectId,
|
|
42
63
|
});
|
|
43
64
|
}
|
|
65
|
+
diag?.append({
|
|
66
|
+
ev: 'ledger.open',
|
|
67
|
+
projectId,
|
|
68
|
+
events: ledger.headSeq,
|
|
69
|
+
head: ledger.head.seq,
|
|
70
|
+
segments: ledger.opened?.segments ?? null,
|
|
71
|
+
});
|
|
44
72
|
|
|
45
73
|
if (seed) {
|
|
46
74
|
const state = service.state;
|
package/lib/host/service.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
1
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
2
2
|
import { ERRORS, fail } from '../domain/errors.js';
|
|
3
3
|
import { findReportPath, parseDecisionQuestions } from './decision-report.js';
|
|
4
4
|
import { ids } from '../domain/ids.js';
|
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
submitTask,
|
|
19
19
|
} from '../domain/task.js';
|
|
20
20
|
import { appendReply, createThread } from '../domain/thread.js';
|
|
21
|
-
import { boardView, quadrantsView } from './views.js';
|
|
21
|
+
import { boardView, quadrantsView, taskCard } from './views.js';
|
|
22
22
|
import { applyMemberConfigPatch, normalizeMemberConfig } from '../domain/member-config.js';
|
|
23
23
|
import { isQaMember, unfinishedTasksFor } from '../domain/member-profile.js';
|
|
24
24
|
|
|
@@ -40,18 +40,27 @@ function mentionsHuman(body) {
|
|
|
40
40
|
*/
|
|
41
41
|
export class TeamService {
|
|
42
42
|
/**
|
|
43
|
-
* @param {{ ledger: { state: any, append: (input: any) => Promise<any> }, now?: () => number }} options
|
|
43
|
+
* @param {{ ledger: { state: any, append: (input: any) => Promise<any> }, now?: () => number, diag?: { append: (row: any) => any } }} options
|
|
44
44
|
*/
|
|
45
45
|
constructor(options) {
|
|
46
46
|
this.ledger = options.ledger;
|
|
47
47
|
this.now = options.now ?? (() => Date.now());
|
|
48
48
|
this.memberConfigPolicy = options.memberConfigPolicy ?? {};
|
|
49
|
+
/** Diagnostic sink ({ append(row) }); optional so tests run without it. */
|
|
50
|
+
this.diag = options.diag ?? null;
|
|
49
51
|
}
|
|
50
52
|
|
|
51
53
|
get state() {
|
|
52
54
|
return this.ledger.state;
|
|
53
55
|
}
|
|
54
56
|
|
|
57
|
+
/** The Human member id, derived once per call from the folded state. */
|
|
58
|
+
humanId() {
|
|
59
|
+
return this.state.humanMemberId
|
|
60
|
+
?? Object.values(this.state.members ?? {}).find((member) => member.kind === 'human')?.id
|
|
61
|
+
?? null;
|
|
62
|
+
}
|
|
63
|
+
|
|
55
64
|
/** Four-column board derived from the same ledger state. */
|
|
56
65
|
board(projectId, options = {}) {
|
|
57
66
|
if (projectId !== undefined && this.state.projects?.[projectId] === undefined) {
|
|
@@ -65,9 +74,7 @@ export class TeamService {
|
|
|
65
74
|
if (projectId !== undefined && this.state.projects?.[projectId] === undefined) {
|
|
66
75
|
fail(ERRORS.NOT_FOUND, `project ${String(projectId)} not found`);
|
|
67
76
|
}
|
|
68
|
-
const human = options.humanMemberId ?? this.
|
|
69
|
-
?? Object.values(this.state.members ?? {}).find((member) => member.kind === 'human')?.id
|
|
70
|
-
?? 'mem_human';
|
|
77
|
+
const human = options.humanMemberId ?? this.humanId() ?? 'mem_human';
|
|
71
78
|
return quadrantsView(this.state, { ...options, humanMemberId: human });
|
|
72
79
|
}
|
|
73
80
|
|
|
@@ -101,37 +108,30 @@ export class TeamService {
|
|
|
101
108
|
lastRecoveredAt: m.lastRecoveredAt ?? null,
|
|
102
109
|
}));
|
|
103
110
|
const agents = members.filter((m) => m.kind !== 'human');
|
|
111
|
+
// One Task card shape for board, quadrants and bootstrap; bootstrap only
|
|
112
|
+
// adds the submission bookkeeping the panel needs.
|
|
104
113
|
const tasks = Object.values(state.tasks ?? {})
|
|
105
114
|
.filter((task) => task.tombstone !== true && task.status !== 'deleted')
|
|
106
|
-
.map((task) => ({
|
|
107
|
-
id: task.id,
|
|
108
|
-
number: task.number,
|
|
109
|
-
title: task.title,
|
|
110
|
-
status: task.status,
|
|
111
|
-
owner: state.members?.[task.ownerId]?.name ?? task.ownerId,
|
|
112
|
-
ownerId: task.ownerId,
|
|
113
|
-
assignee: task.assigneeId === null || task.assigneeId === undefined ? null : (state.members?.[task.assigneeId]?.name ?? task.assigneeId),
|
|
114
|
-
assigneeId: task.assigneeId ?? null,
|
|
115
|
-
replies: state.threads?.[task.id]?.headSeq ?? 0,
|
|
116
|
-
following: (state.followers?.[task.id] ?? []).length > 0,
|
|
117
|
-
blockedBy: task.blockedBy ?? [],
|
|
118
|
-
revision: task.revision,
|
|
119
|
-
resultDefinition: task.resultDefinition,
|
|
120
|
-
qaRequired: task.qaRequired === true,
|
|
121
|
-
qaChecklist: task.qaChecklist ?? [],
|
|
122
|
-
qaChecks: task.qaChecks ?? [],
|
|
123
|
-
updatedAt: task.updatedAt ?? task.createdAt ?? null,
|
|
124
|
-
}));
|
|
115
|
+
.map((task) => ({ ...taskCard(state, task), qaChecks: task.qaChecks ?? [] }));
|
|
125
116
|
const project = projectId === undefined ? projects[0] : projects.find((p) => p.id === projectId);
|
|
126
|
-
|
|
117
|
+
// 大规模/长迭代裁剪:面板只需要最近的窗口,全史在账本里按需取。
|
|
118
|
+
// stream 投影本身已封顶(≤500),bootstrap 再按窗口切尾。
|
|
119
|
+
const stream = project === undefined
|
|
120
|
+
? []
|
|
121
|
+
: (state.stream ?? []).filter((entry) => entry.projectId === project.id).slice(-200);
|
|
127
122
|
const selected = threadTaskId ?? tasks.find((task) => task.status === 'in_progress')?.id ?? tasks[0]?.id;
|
|
128
123
|
const thread = selected === undefined ? null : (state.threads?.[selected] ?? { taskId: selected, headSeq: 0, entries: [] });
|
|
129
|
-
const threads = Object.fromEntries(tasks.map((task) =>
|
|
130
|
-
task.id
|
|
131
|
-
|
|
132
|
-
|
|
124
|
+
const threads = Object.fromEntries(tasks.map((task) => {
|
|
125
|
+
const full = state.threads?.[task.id];
|
|
126
|
+
if (full === undefined) return [task.id, { taskId: task.id, headSeq: 0, entries: [], entryTotal: 0 }];
|
|
127
|
+
const entries = Array.isArray(full.entries) ? full.entries : [];
|
|
128
|
+
return [
|
|
129
|
+
task.id,
|
|
130
|
+
{ ...full, entries: entries.slice(-50), entryTotal: full.headSeq ?? entries.length },
|
|
131
|
+
];
|
|
132
|
+
}));
|
|
133
133
|
const inbox = Object.values(state.inbox ?? {}).filter((item) =>
|
|
134
|
-
(item.memberId ===
|
|
134
|
+
(item.memberId === this.humanId())
|
|
135
135
|
&& !isInformationalThreadMention(item));
|
|
136
136
|
return {
|
|
137
137
|
project: project ?? null,
|
|
@@ -154,9 +154,7 @@ export class TeamService {
|
|
|
154
154
|
* per-entry dedupe makes it idempotent.
|
|
155
155
|
*/
|
|
156
156
|
async backfillHumanMentions(workspacePath = '') {
|
|
157
|
-
const humanId = this.
|
|
158
|
-
?? Object.values(this.state.members ?? {}).find((member) => member.kind === 'human')?.id
|
|
159
|
-
?? null;
|
|
157
|
+
const humanId = this.humanId();
|
|
160
158
|
if (humanId === null) return 0;
|
|
161
159
|
const projectId = Object.keys(this.state.projects ?? {})[0];
|
|
162
160
|
let reportPath = null;
|
|
@@ -168,6 +166,13 @@ export class TeamService {
|
|
|
168
166
|
if (reportPath !== null) break;
|
|
169
167
|
}
|
|
170
168
|
let reportQuestions = [];
|
|
169
|
+
if (reportPath !== null) {
|
|
170
|
+
try {
|
|
171
|
+
// An agent-authored path drives a server-side read here; cap the
|
|
172
|
+
// read so a giant or adversarial report cannot flood memory.
|
|
173
|
+
if (statSync(reportPath).size > 256 * 1024) reportPath = null;
|
|
174
|
+
} catch { reportPath = null; }
|
|
175
|
+
}
|
|
171
176
|
if (reportPath !== null) {
|
|
172
177
|
try {
|
|
173
178
|
reportQuestions = parseDecisionQuestions(readFileSync(reportPath, 'utf8'));
|
|
@@ -230,45 +235,85 @@ export class TeamService {
|
|
|
230
235
|
if (actor === undefined || (actor.kind !== 'human' && actor.kind !== 'agent')) {
|
|
231
236
|
fail(ERRORS.UNAUTHORIZED, 'an exact Team member is required');
|
|
232
237
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
238
|
+
// An agent actor must resolve to a live, non-retired member: otherwise a
|
|
239
|
+
// forged or stale memberId would bypass owner/QA checks downstream.
|
|
240
|
+
if (actor.kind === 'agent') {
|
|
241
|
+
const actingMember = this.state.members?.[actor.memberId];
|
|
242
|
+
if (actingMember === undefined || actingMember.kind !== 'agent') {
|
|
243
|
+
fail(ERRORS.UNAUTHORIZED, `agent actor ${String(actor.memberId ?? '?')} is not a Team member`);
|
|
244
|
+
}
|
|
245
|
+
if (actingMember.status === 'retired') {
|
|
246
|
+
fail(ERRORS.INVALID_STATE, `agent ${actingMember.name ?? actingMember.id} is retired`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// Diagnostic trail: one entry row per command, one failure row on throw,
|
|
250
|
+
// one done row with the duration in every case.
|
|
251
|
+
const startedAt = Date.now();
|
|
252
|
+
this.diag?.append({
|
|
253
|
+
ev: 'dispatch.start',
|
|
254
|
+
type: command.type,
|
|
255
|
+
actor: actor.kind,
|
|
256
|
+
memberId: actor.memberId ?? null,
|
|
257
|
+
projectId: command.projectId ?? null,
|
|
258
|
+
});
|
|
259
|
+
try {
|
|
260
|
+
switch (command.type) {
|
|
261
|
+
case 'project.create': return await this.createProject(command, payload);
|
|
262
|
+
case 'project.owner.update': return await this.changeProjectOwner(command, payload);
|
|
263
|
+
case 'project.workspace.update': return await this.changeProjectWorkspace(command, payload);
|
|
264
|
+
case 'project.pmo.update': return await this.setProjectPmo(command, payload);
|
|
265
|
+
case 'stream.post': return await this.postStream(command, payload);
|
|
266
|
+
case 'member.add': return await this.addMember(command, payload);
|
|
267
|
+
case 'member.bind': return await this.bindMemberSession(command, payload);
|
|
268
|
+
case 'member.fail': return await this.failMember(command, payload);
|
|
269
|
+
case 'member.retire': return await this.retireMember(command, payload);
|
|
270
|
+
case 'member.profile.update': return await this.updateMemberProfile(command, payload);
|
|
271
|
+
case 'member.evolve': return await this.evolveMember(command, payload);
|
|
272
|
+
case 'member.distill': return await this.distillMember(command, payload);
|
|
273
|
+
case 'member.leave': return await this.leaveMember(command, payload);
|
|
274
|
+
case 'member.rejoin': return await this.rejoinMember(command, payload);
|
|
275
|
+
case 'member.takeover': return await this.takeoverMember(command, payload);
|
|
276
|
+
case 'member.message': return await this.messageMember(command, payload);
|
|
277
|
+
case 'member.note': return await this.addMemberNote(command, payload);
|
|
278
|
+
case 'member.skill': return await this.addMemberSkill(command, payload);
|
|
279
|
+
case 'member.config.update': return await this.updateMemberConfig(command, payload);
|
|
280
|
+
case 'member.retry': return await this.retryMember(command, payload);
|
|
281
|
+
case 'task.create': return await this.createTask(command, payload);
|
|
282
|
+
case 'task.claim': return await this.taskCommand(command, 'claim');
|
|
283
|
+
case 'task.release': return await this.taskCommand(command, 'release');
|
|
284
|
+
case 'task.assign': return await this.taskCommand(command, 'assign');
|
|
285
|
+
case 'task.submit': return await this.taskCommand(command, 'submit');
|
|
286
|
+
case 'task.accept': return await this.taskCommand(command, 'accept');
|
|
287
|
+
case 'task.request_changes': return await this.taskCommand(command, 'request_changes');
|
|
288
|
+
case 'task.cancel': return await this.taskCommand(command, 'cancel');
|
|
289
|
+
case 'task.reopen': return await this.taskCommand(command, 'reopen');
|
|
290
|
+
case 'task.reassign': return await this.taskCommand(command, 'reassign');
|
|
291
|
+
case 'task.delete': return await this.taskCommand(command, 'delete');
|
|
292
|
+
case 'task.convert': return await this.convert(command, payload);
|
|
293
|
+
case 'task.follow': return await this.follow(command, payload);
|
|
294
|
+
case 'thread.post': return await this.postThread(command, payload);
|
|
295
|
+
case 'inbox.ack': return await this.ack(command, payload);
|
|
296
|
+
case 'decision.request': return await this.requestDecision(command, payload);
|
|
297
|
+
case 'decision.answer': return await this.answerDecision(command, payload);
|
|
298
|
+
default: fail(ERRORS.INVALID_ARGUMENT, `unknown command: ${String(command.type)}`);
|
|
299
|
+
}
|
|
300
|
+
} catch (error) {
|
|
301
|
+
this.diag?.append({
|
|
302
|
+
ev: 'dispatch.fail',
|
|
303
|
+
type: command.type,
|
|
304
|
+
code: error?.code ?? 'INTERNAL',
|
|
305
|
+
projectId: command.projectId ?? null,
|
|
306
|
+
message: error?.message ?? String(error),
|
|
307
|
+
elapsedMs: Date.now() - startedAt,
|
|
308
|
+
});
|
|
309
|
+
throw error;
|
|
310
|
+
} finally {
|
|
311
|
+
this.diag?.append({
|
|
312
|
+
ev: 'dispatch.done',
|
|
313
|
+
type: command.type,
|
|
314
|
+
projectId: command.projectId ?? null,
|
|
315
|
+
elapsedMs: Date.now() - startedAt,
|
|
316
|
+
});
|
|
272
317
|
}
|
|
273
318
|
}
|
|
274
319
|
|
|
@@ -395,25 +440,25 @@ export class TeamService {
|
|
|
395
440
|
return { member };
|
|
396
441
|
}
|
|
397
442
|
|
|
398
|
-
async recordWake(memberId, info) {
|
|
443
|
+
async recordWake(memberId, info, projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
399
444
|
const event = {
|
|
400
445
|
type: info.ok === true ? 'wake/delivered' : 'wake/failed',
|
|
401
446
|
payload: { memberId, taskId: info.taskId ?? null, reason: info.reason ?? null, messageId: info.messageId ?? null, error: info.error ?? null },
|
|
402
447
|
actor: { kind: 'system' },
|
|
403
|
-
projectId
|
|
448
|
+
projectId,
|
|
404
449
|
at: this.now(),
|
|
405
450
|
};
|
|
406
451
|
await this.ledger.append(event);
|
|
407
452
|
return event;
|
|
408
453
|
}
|
|
409
454
|
|
|
410
|
-
async bindHostSession(sessionId) {
|
|
455
|
+
async bindHostSession(sessionId, projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
411
456
|
if (typeof sessionId !== 'string' || sessionId === '') fail(ERRORS.INVALID_ARGUMENT, 'host sessionId is required');
|
|
412
457
|
await this.ledger.append({
|
|
413
458
|
type: 'team/host-bound',
|
|
414
459
|
payload: { sessionId },
|
|
415
460
|
actor: { kind: 'system' },
|
|
416
|
-
projectId
|
|
461
|
+
projectId,
|
|
417
462
|
at: this.now(),
|
|
418
463
|
});
|
|
419
464
|
return { sessionId };
|
|
@@ -423,6 +468,8 @@ export class TeamService {
|
|
|
423
468
|
if (command.actor.kind !== 'human') fail(ERRORS.UNAUTHORIZED, 'only Human may bind a member session');
|
|
424
469
|
const member = this.state.members?.[payload.memberId];
|
|
425
470
|
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(payload.memberId)} not found`);
|
|
471
|
+
// 退役成员不得经 bind 复活;重新入队必须走 member.rejoin(清 talentPool)。
|
|
472
|
+
if (member.status === 'retired') fail(ERRORS.INVALID_STATE, `member ${member.name ?? member.id} is retired`);
|
|
426
473
|
if (typeof payload.sessionId !== 'string' || payload.sessionId === '') fail(ERRORS.INVALID_ARGUMENT, 'sessionId is required');
|
|
427
474
|
await this.append(command, 'member/session-bound', {
|
|
428
475
|
memberId: member.id,
|
|
@@ -646,6 +693,8 @@ export class TeamService {
|
|
|
646
693
|
async retryMember(command, payload) {
|
|
647
694
|
const member = this.state.members?.[payload.memberId];
|
|
648
695
|
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(payload.memberId)} not found`);
|
|
696
|
+
// 退役成员的重启必须走 member.rejoin(清 talentPool/leftTeamAt)。
|
|
697
|
+
if (member.status === 'retired') fail(ERRORS.INVALID_STATE, `member ${member.name ?? member.id} is retired; use member.rejoin`);
|
|
649
698
|
const project = this.state.projects?.[command.projectId];
|
|
650
699
|
const isHuman = command.actor.kind === 'human';
|
|
651
700
|
const isLead = project?.ownerMemberId === command.actor.memberId || project?.ownerId === command.actor.memberId;
|
|
@@ -658,7 +707,7 @@ export class TeamService {
|
|
|
658
707
|
}
|
|
659
708
|
|
|
660
709
|
/** Host/Waker calls these; actor is system, never a Human decision. */
|
|
661
|
-
async recordMemberFailure(memberId, failure) {
|
|
710
|
+
async recordMemberFailure(memberId, failure, projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
662
711
|
const member = this.state.members?.[memberId];
|
|
663
712
|
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(memberId)} not found`);
|
|
664
713
|
const attempt = Number.isSafeInteger(failure.attempt) ? failure.attempt : (member.lastFailure?.attempt ?? 0) + 1;
|
|
@@ -668,14 +717,21 @@ export class TeamService {
|
|
|
668
717
|
type: 'member/llm-failed',
|
|
669
718
|
payload: { ...recorded, memberId, fatal },
|
|
670
719
|
actor: { kind: 'system' },
|
|
671
|
-
projectId
|
|
720
|
+
projectId,
|
|
672
721
|
at: this.now(),
|
|
673
722
|
});
|
|
723
|
+
this.diag?.append({
|
|
724
|
+
ev: fatal ? 'member.fail' : 'member.llm-failed',
|
|
725
|
+
memberId,
|
|
726
|
+
category: recorded.category,
|
|
727
|
+
attempt,
|
|
728
|
+
provider: recorded.provider ?? null,
|
|
729
|
+
model: recorded.model ?? null,
|
|
730
|
+
projectId,
|
|
731
|
+
});
|
|
674
732
|
let inboxItem = null;
|
|
675
733
|
if (fatal) {
|
|
676
|
-
const humanId = this.
|
|
677
|
-
?? Object.values(this.state.members ?? {}).find((candidate) => candidate.kind === 'human')?.id
|
|
678
|
-
?? null;
|
|
734
|
+
const humanId = this.humanId();
|
|
679
735
|
const duplicate = humanId === null ? undefined : Object.values(this.state.inbox ?? {}).find((item) =>
|
|
680
736
|
item.memberId === humanId
|
|
681
737
|
&& item.requiresDecision === true
|
|
@@ -697,7 +753,7 @@ export class TeamService {
|
|
|
697
753
|
type: 'inbox/item-created',
|
|
698
754
|
payload: { itemId: inboxItem.id, ...inboxItem },
|
|
699
755
|
actor: { kind: 'system' },
|
|
700
|
-
projectId
|
|
756
|
+
projectId,
|
|
701
757
|
at: this.now(),
|
|
702
758
|
});
|
|
703
759
|
}
|
|
@@ -709,7 +765,7 @@ export class TeamService {
|
|
|
709
765
|
}
|
|
710
766
|
|
|
711
767
|
/** Fallback changes are system-authored config events, rollback-able by Human. */
|
|
712
|
-
async applySystemFallback(memberId, patch, reason = 'llm-fallback') {
|
|
768
|
+
async applySystemFallback(memberId, patch, reason = 'llm-fallback', projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
713
769
|
const member = this.state.members?.[memberId];
|
|
714
770
|
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(memberId)} not found`);
|
|
715
771
|
const current = normalizeMemberConfig(member.config ?? {});
|
|
@@ -718,6 +774,13 @@ export class TeamService {
|
|
|
718
774
|
configVersion: member.configVersion ?? 0,
|
|
719
775
|
});
|
|
720
776
|
if (!result.changed) return { member, config: current, changed: false };
|
|
777
|
+
this.diag?.append({
|
|
778
|
+
ev: 'member.fallback',
|
|
779
|
+
memberId,
|
|
780
|
+
reason,
|
|
781
|
+
patch,
|
|
782
|
+
projectId,
|
|
783
|
+
});
|
|
721
784
|
const nextConfig = {
|
|
722
785
|
...result.config,
|
|
723
786
|
updatedBy: 'system',
|
|
@@ -734,22 +797,23 @@ export class TeamService {
|
|
|
734
797
|
reason,
|
|
735
798
|
},
|
|
736
799
|
actor: { kind: 'system' },
|
|
737
|
-
projectId
|
|
800
|
+
projectId,
|
|
738
801
|
at: this.now(),
|
|
739
802
|
});
|
|
740
803
|
return { member: { ...member, config: nextConfig, configVersion: nextConfig.configVersion }, config: nextConfig, changed: true };
|
|
741
804
|
}
|
|
742
805
|
|
|
743
|
-
async recordMemberRecovery(memberId, attempt, effectiveConfig) {
|
|
806
|
+
async recordMemberRecovery(memberId, attempt, effectiveConfig, projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
744
807
|
const member = this.state.members?.[memberId];
|
|
745
808
|
if (member === undefined) fail(ERRORS.NOT_FOUND, `member ${String(memberId)} not found`);
|
|
746
809
|
await this.ledger.append({
|
|
747
810
|
type: 'member/recovered',
|
|
748
811
|
payload: { memberId, attempt, effectiveConfig: effectiveConfig ?? member.config ?? null },
|
|
749
812
|
actor: { kind: 'system' },
|
|
750
|
-
projectId
|
|
813
|
+
projectId,
|
|
751
814
|
at: this.now(),
|
|
752
815
|
});
|
|
816
|
+
this.diag?.append({ ev: 'member.recovered', memberId, attempt, projectId });
|
|
753
817
|
return { member: { ...member, status: 'running', lastRecoveredAt: this.now() } };
|
|
754
818
|
}
|
|
755
819
|
|
|
@@ -809,7 +873,14 @@ export class TeamService {
|
|
|
809
873
|
case 'assign': outcome = assignTask(task, command.actor, command.payload?.assigneeId, { expectedRevision, at: this.now() }); break;
|
|
810
874
|
case 'submit': outcome = submitTask(task, command.actor, { summary: command.payload?.summary, evidence: command.payload?.evidence }, { expectedRevision, at: this.now() }); break;
|
|
811
875
|
case 'accept': {
|
|
812
|
-
|
|
876
|
+
// Agent actors are pre-resolved by dispatch; resolve defensively here
|
|
877
|
+
// so an unknown memberId fails closed instead of skipping the QA gate.
|
|
878
|
+
const reviewer = command.actor.kind === 'agent'
|
|
879
|
+
? this.state.members?.[command.actor.memberId] ?? undefined
|
|
880
|
+
: null;
|
|
881
|
+
if (command.actor.kind === 'agent' && reviewer === undefined) {
|
|
882
|
+
fail(ERRORS.UNAUTHORIZED, `agent actor ${String(command.actor.memberId ?? '?')} is not a Team member`);
|
|
883
|
+
}
|
|
813
884
|
if (task.qaRequired === true && reviewer !== null && !isQaMember(reviewer)) {
|
|
814
885
|
fail(ERRORS.INVALID_STATE, 'this task requires a QA reviewer');
|
|
815
886
|
}
|
|
@@ -921,10 +992,8 @@ export class TeamService {
|
|
|
921
992
|
}
|
|
922
993
|
|
|
923
994
|
/** PMO: answer all pending structured questions with the max-value policy. */
|
|
924
|
-
async autoAnswerDecisions() {
|
|
925
|
-
const humanId = this.
|
|
926
|
-
?? Object.values(this.state.members ?? {}).find((member) => member.kind === 'human')?.id
|
|
927
|
-
?? null;
|
|
995
|
+
async autoAnswerDecisions(projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
996
|
+
const humanId = this.humanId();
|
|
928
997
|
if (humanId === null) return 0;
|
|
929
998
|
const items = Object.values(this.state.inbox ?? {}).filter((item) =>
|
|
930
999
|
item.memberId === humanId
|
|
@@ -934,12 +1003,12 @@ export class TeamService {
|
|
|
934
1003
|
for (const item of items) {
|
|
935
1004
|
const questions = Array.isArray(item.ref?.questions) ? item.ref.questions : [];
|
|
936
1005
|
const answers = chooseDecisionAnswers(questions);
|
|
937
|
-
results.push(await this.autoAnswerDecision(item, answers));
|
|
1006
|
+
results.push(await this.autoAnswerDecision(item, answers, projectId));
|
|
938
1007
|
}
|
|
939
1008
|
return results;
|
|
940
1009
|
}
|
|
941
1010
|
|
|
942
|
-
async autoAnswerDecision(item, answers) {
|
|
1011
|
+
async autoAnswerDecision(item, answers, projectId = Object.keys(this.state.projects ?? {})[0]) {
|
|
943
1012
|
const taskId = typeof item.ref?.taskId === 'string' ? item.ref.taskId : null;
|
|
944
1013
|
const task = taskId === null ? undefined : this.state.tasks?.[taskId];
|
|
945
1014
|
const questionById = new Map((item.ref?.questions ?? []).map((question) => [question.id, question.text]));
|
|
@@ -960,7 +1029,7 @@ export class TeamService {
|
|
|
960
1029
|
type: 'thread/posted',
|
|
961
1030
|
payload: { taskId: task.id, ...outcome.entry },
|
|
962
1031
|
actor: { kind: 'system', memberId: 'mem_pmo' },
|
|
963
|
-
projectId
|
|
1032
|
+
projectId,
|
|
964
1033
|
at: this.now(),
|
|
965
1034
|
});
|
|
966
1035
|
entry = outcome.entry;
|
|
@@ -969,11 +1038,18 @@ export class TeamService {
|
|
|
969
1038
|
disposition: 'done',
|
|
970
1039
|
decision: { answers, auto: true, by: 'mem_pmo', body },
|
|
971
1040
|
}, { at: this.now() });
|
|
1041
|
+
this.diag?.append({
|
|
1042
|
+
ev: 'pmo.auto-answer',
|
|
1043
|
+
itemId: item.id,
|
|
1044
|
+
taskId,
|
|
1045
|
+
answers: answers.length,
|
|
1046
|
+
projectId,
|
|
1047
|
+
});
|
|
972
1048
|
await this.ledger.append({
|
|
973
1049
|
type: 'inbox/acked',
|
|
974
1050
|
payload: { itemId: next.id, memberId: next.memberId, state: next.state, disposition: next.disposition, at: next.ackedAt },
|
|
975
1051
|
actor: { kind: 'system', memberId: 'mem_pmo' },
|
|
976
|
-
projectId
|
|
1052
|
+
projectId,
|
|
977
1053
|
at: this.now(),
|
|
978
1054
|
});
|
|
979
1055
|
return { item: next, entry };
|
|
@@ -981,9 +1057,7 @@ export class TeamService {
|
|
|
981
1057
|
|
|
982
1058
|
/** Surface an explicit Human decision request with all questions inline. */
|
|
983
1059
|
async requestDecision(command, payload) {
|
|
984
|
-
const humanId = this.
|
|
985
|
-
?? Object.values(this.state.members ?? {}).find((member) => member.kind === 'human')?.id
|
|
986
|
-
?? null;
|
|
1060
|
+
const humanId = this.humanId();
|
|
987
1061
|
if (humanId === null) fail(ERRORS.NOT_FOUND, 'Human member not found');
|
|
988
1062
|
const raw = Array.isArray(payload.questions) ? payload.questions : [];
|
|
989
1063
|
if (raw.length === 0 || raw.length > 20) fail(ERRORS.INVALID_ARGUMENT, 'questions must contain 1..20 items');
|