@suifei/dsh-ai-team 0.4.145 → 0.4.147
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 +11 -0
- package/LICENSE +21 -21
- package/README.md +31 -2
- package/client.js +551 -49
- package/cordis.patch.yml +31 -31
- package/icon.svg +12 -12
- package/index.js +105 -25
- package/lib/domain/ids.js +26 -26
- package/lib/domain/inbox.js +93 -93
- package/lib/domain/member-config.js +168 -168
- package/lib/domain/policy.js +8 -1
- package/lib/domain/task.js +2 -0
- package/lib/domain/team-blueprint.js +240 -0
- package/lib/host/decision-report.js +72 -72
- package/lib/host/default-roster.js +18 -0
- package/lib/host/default-workspace.js +69 -69
- package/lib/host/fault-adapter.js +50 -50
- package/lib/host/member-tools.js +6 -1
- package/lib/host/people-view.js +9 -2
- package/lib/host/permission-guard.js +61 -61
- package/lib/host/project-registry.js +104 -104
- package/lib/host/project-runtime.js +2 -2
- package/lib/host/risk-view.js +99 -99
- package/lib/host/service.js +195 -16
- package/lib/host/views.js +42 -1
- package/lib/host/waker.js +38 -8
- package/lib/host/weekly-view.js +86 -86
- package/lib/host/workspace-path.js +94 -94
- package/lib/ledger/checkpoint.js +415 -415
- package/lib/ledger/events.js +398 -398
- package/lib/ledger/fold.js +23 -0
- package/lib/ledger/namespace.js +124 -124
- package/lib/ledger/tools.js +118 -118
- package/lib/ledger/writer.js +1266 -1266
- package/lib/runtime/error-chain.js +31 -31
- package/lib/runtime/shell-guard.js +102 -102
- package/lib/runtime/skill-catalog.js +41 -41
- package/lib/runtime/skill-policy.js +12 -12
- package/lib/runtime/stop-guard.js +17 -2
- package/lib/skillopt/adopt-gate.js +108 -108
- package/lib/skillopt/gate-wiring.js +146 -146
- package/lib/skillopt/jobs.js +135 -135
- package/lib/skillopt/proposal-store.js +33 -33
- package/lib/skillopt/review-runner.js +107 -107
- package/lib/skillopt/rubric.js +189 -189
- package/lib/skillopt/subagent-text.js +101 -101
- package/lib/skillopt/tool-access.js +56 -56
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/package.json +1 -1
|
@@ -1,104 +1,104 @@
|
|
|
1
|
-
/** Lightweight Project index. The ledger remains the source of truth for Project state. */
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
3
|
-
import { join } from 'node:path';
|
|
4
|
-
import { DEFAULT_PROJECT_ID } from './project-runtime.js';
|
|
5
|
-
import { reportFail } from '../diaglog.js';
|
|
6
|
-
|
|
7
|
-
export class ProjectRegistry {
|
|
8
|
-
constructor({ rootDir, projects, activeProjectId }) {
|
|
9
|
-
this.rootDir = rootDir;
|
|
10
|
-
this.projects = Array.isArray(projects) ? projects : [];
|
|
11
|
-
this.activeProjectId = activeProjectId ?? DEFAULT_PROJECT_ID;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
static load(rootDir, options = {}) {
|
|
15
|
-
// 初装时会给一个默认项目;它必须有工作目录,否则成员会话的 cwd 只能落到
|
|
16
|
-
// 插件数据目录(workspace attach 会被拒,成员也在错误目录里干活)。
|
|
17
|
-
const defaultWorkspacePath = typeof options.defaultWorkspacePath === 'string' ? options.defaultWorkspacePath : '';
|
|
18
|
-
mkdirSync(rootDir, { recursive: true });
|
|
19
|
-
const file = join(rootDir, 'projects.json');
|
|
20
|
-
let parsed;
|
|
21
|
-
try {
|
|
22
|
-
parsed = existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : null;
|
|
23
|
-
} catch (error) {
|
|
24
|
-
// A corrupt index must not silently vanish the user's project list:
|
|
25
|
-
// keep the broken file for forensics, then fall back to the default.
|
|
26
|
-
const kept = `${file}.corrupt-${Date.now()}`;
|
|
27
|
-
try { renameSync(file, kept); } catch { /* rename is best-effort */ }
|
|
28
|
-
reportFail('registry.corrupt.fail', { kept }, `[ai-team] projects.json was unreadable (${error?.message ?? error}); kept as ${kept}`);
|
|
29
|
-
parsed = null;
|
|
30
|
-
}
|
|
31
|
-
// 默认项目只在**一个项目都没有**时播种(初装的第一入口)。此前只要列表里
|
|
32
|
-
// 缺少 `prj_main` 就补一条:用户把它删掉后会被静默复活,等于删不掉。
|
|
33
|
-
const projects = Array.isArray(parsed?.projects) && parsed.projects.length > 0
|
|
34
|
-
? parsed.projects.filter((project) => typeof project?.id === 'string' && project.id !== '')
|
|
35
|
-
: [];
|
|
36
|
-
if (projects.length === 0) projects.push({ id: DEFAULT_PROJECT_ID, name: '我的项目', createdAt: 0 });
|
|
37
|
-
const active = projects.some((project) => project.id === parsed?.activeProjectId)
|
|
38
|
-
? parsed.activeProjectId
|
|
39
|
-
: projects[0].id;
|
|
40
|
-
const registry = new ProjectRegistry({ rootDir, projects, activeProjectId: active });
|
|
41
|
-
if (!existsSync(file)) registry.save();
|
|
42
|
-
return registry;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
get activeProject() {
|
|
46
|
-
return this.projects.find((project) => project.id === this.activeProjectId) ?? this.projects[0];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
list() {
|
|
50
|
-
return this.projects.map((project) => ({ ...project, active: project.id === this.activeProjectId }));
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** 更新某个项目的工作目录(与账本的 project/workspace-changed 保持同步)。 */
|
|
54
|
-
setWorkspace(id, workspacePath) {
|
|
55
|
-
const project = this.projects.find((entry) => entry.id === id);
|
|
56
|
-
if (project === undefined) return false;
|
|
57
|
-
const next = typeof workspacePath === 'string' ? workspacePath : '';
|
|
58
|
-
if ((project.workspacePath ?? '') === next) return false;
|
|
59
|
-
project.workspacePath = next;
|
|
60
|
-
this.save();
|
|
61
|
-
return true;
|
|
62
|
-
}
|
|
63
|
-
add({ id, name, createdAt, goal, workspacePath }) {
|
|
64
|
-
if (typeof id !== 'string' || id === '') throw new Error('project id is required');
|
|
65
|
-
if (!this.projects.some((project) => project.id === id)) {
|
|
66
|
-
this.projects.push({ id, name: name ?? id, goal: goal ?? '', workspacePath: workspacePath ?? '', createdAt: createdAt ?? Date.now() });
|
|
67
|
-
this.save();
|
|
68
|
-
}
|
|
69
|
-
return this.projects.find((project) => project.id === id);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
setActive(id) {
|
|
73
|
-
const project = this.projects.find((entry) => entry.id === id);
|
|
74
|
-
if (project === undefined) throw new Error(`unknown project: ${id}`);
|
|
75
|
-
// 归档项目不能成为 active:registry 的 active 会被 bootstrap 当作「默认项目」,
|
|
76
|
-
// 指向一个已归档/无账本的项目会让工作台直接不可达。
|
|
77
|
-
if (project.archived === true) throw new Error(`project ${id} is archived`);
|
|
78
|
-
this.activeProjectId = id;
|
|
79
|
-
this.save();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** Mirror a Project's archive flag into the global index (ledger stays the source of truth). */
|
|
83
|
-
setArchived(id, archived) {
|
|
84
|
-
const project = this.projects.find((entry) => entry.id === id);
|
|
85
|
-
if (project === undefined) throw new Error(`unknown project: ${id}`);
|
|
86
|
-
project.archived = archived === true;
|
|
87
|
-
if (archived === true && this.activeProjectId === id) {
|
|
88
|
-
const fallback = this.projects.find((entry) => entry.id !== id && entry.archived !== true);
|
|
89
|
-
if (fallback !== undefined) this.activeProjectId = fallback.id;
|
|
90
|
-
}
|
|
91
|
-
this.save();
|
|
92
|
-
return project;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
save() {
|
|
96
|
-
const file = join(this.rootDir, 'projects.json');
|
|
97
|
-
const temp = `${file}.tmp`;
|
|
98
|
-
writeFileSync(temp, JSON.stringify({
|
|
99
|
-
activeProjectId: this.activeProjectId,
|
|
100
|
-
projects: this.projects,
|
|
101
|
-
}, null, 2), 'utf8');
|
|
102
|
-
renameSync(temp, file);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
1
|
+
/** Lightweight Project index. The ledger remains the source of truth for Project state. */
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { DEFAULT_PROJECT_ID } from './project-runtime.js';
|
|
5
|
+
import { reportFail } from '../diaglog.js';
|
|
6
|
+
|
|
7
|
+
export class ProjectRegistry {
|
|
8
|
+
constructor({ rootDir, projects, activeProjectId }) {
|
|
9
|
+
this.rootDir = rootDir;
|
|
10
|
+
this.projects = Array.isArray(projects) ? projects : [];
|
|
11
|
+
this.activeProjectId = activeProjectId ?? DEFAULT_PROJECT_ID;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static load(rootDir, options = {}) {
|
|
15
|
+
// 初装时会给一个默认项目;它必须有工作目录,否则成员会话的 cwd 只能落到
|
|
16
|
+
// 插件数据目录(workspace attach 会被拒,成员也在错误目录里干活)。
|
|
17
|
+
const defaultWorkspacePath = typeof options.defaultWorkspacePath === 'string' ? options.defaultWorkspacePath : '';
|
|
18
|
+
mkdirSync(rootDir, { recursive: true });
|
|
19
|
+
const file = join(rootDir, 'projects.json');
|
|
20
|
+
let parsed;
|
|
21
|
+
try {
|
|
22
|
+
parsed = existsSync(file) ? JSON.parse(readFileSync(file, 'utf8')) : null;
|
|
23
|
+
} catch (error) {
|
|
24
|
+
// A corrupt index must not silently vanish the user's project list:
|
|
25
|
+
// keep the broken file for forensics, then fall back to the default.
|
|
26
|
+
const kept = `${file}.corrupt-${Date.now()}`;
|
|
27
|
+
try { renameSync(file, kept); } catch { /* rename is best-effort */ }
|
|
28
|
+
reportFail('registry.corrupt.fail', { kept }, `[ai-team] projects.json was unreadable (${error?.message ?? error}); kept as ${kept}`);
|
|
29
|
+
parsed = null;
|
|
30
|
+
}
|
|
31
|
+
// 默认项目只在**一个项目都没有**时播种(初装的第一入口)。此前只要列表里
|
|
32
|
+
// 缺少 `prj_main` 就补一条:用户把它删掉后会被静默复活,等于删不掉。
|
|
33
|
+
const projects = Array.isArray(parsed?.projects) && parsed.projects.length > 0
|
|
34
|
+
? parsed.projects.filter((project) => typeof project?.id === 'string' && project.id !== '')
|
|
35
|
+
: [];
|
|
36
|
+
if (projects.length === 0) projects.push({ id: DEFAULT_PROJECT_ID, name: '我的项目', createdAt: 0 });
|
|
37
|
+
const active = projects.some((project) => project.id === parsed?.activeProjectId)
|
|
38
|
+
? parsed.activeProjectId
|
|
39
|
+
: projects[0].id;
|
|
40
|
+
const registry = new ProjectRegistry({ rootDir, projects, activeProjectId: active });
|
|
41
|
+
if (!existsSync(file)) registry.save();
|
|
42
|
+
return registry;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
get activeProject() {
|
|
46
|
+
return this.projects.find((project) => project.id === this.activeProjectId) ?? this.projects[0];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
list() {
|
|
50
|
+
return this.projects.map((project) => ({ ...project, active: project.id === this.activeProjectId }));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 更新某个项目的工作目录(与账本的 project/workspace-changed 保持同步)。 */
|
|
54
|
+
setWorkspace(id, workspacePath) {
|
|
55
|
+
const project = this.projects.find((entry) => entry.id === id);
|
|
56
|
+
if (project === undefined) return false;
|
|
57
|
+
const next = typeof workspacePath === 'string' ? workspacePath : '';
|
|
58
|
+
if ((project.workspacePath ?? '') === next) return false;
|
|
59
|
+
project.workspacePath = next;
|
|
60
|
+
this.save();
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
add({ id, name, createdAt, goal, workspacePath }) {
|
|
64
|
+
if (typeof id !== 'string' || id === '') throw new Error('project id is required');
|
|
65
|
+
if (!this.projects.some((project) => project.id === id)) {
|
|
66
|
+
this.projects.push({ id, name: name ?? id, goal: goal ?? '', workspacePath: workspacePath ?? '', createdAt: createdAt ?? Date.now() });
|
|
67
|
+
this.save();
|
|
68
|
+
}
|
|
69
|
+
return this.projects.find((project) => project.id === id);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
setActive(id) {
|
|
73
|
+
const project = this.projects.find((entry) => entry.id === id);
|
|
74
|
+
if (project === undefined) throw new Error(`unknown project: ${id}`);
|
|
75
|
+
// 归档项目不能成为 active:registry 的 active 会被 bootstrap 当作「默认项目」,
|
|
76
|
+
// 指向一个已归档/无账本的项目会让工作台直接不可达。
|
|
77
|
+
if (project.archived === true) throw new Error(`project ${id} is archived`);
|
|
78
|
+
this.activeProjectId = id;
|
|
79
|
+
this.save();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Mirror a Project's archive flag into the global index (ledger stays the source of truth). */
|
|
83
|
+
setArchived(id, archived) {
|
|
84
|
+
const project = this.projects.find((entry) => entry.id === id);
|
|
85
|
+
if (project === undefined) throw new Error(`unknown project: ${id}`);
|
|
86
|
+
project.archived = archived === true;
|
|
87
|
+
if (archived === true && this.activeProjectId === id) {
|
|
88
|
+
const fallback = this.projects.find((entry) => entry.id !== id && entry.archived !== true);
|
|
89
|
+
if (fallback !== undefined) this.activeProjectId = fallback.id;
|
|
90
|
+
}
|
|
91
|
+
this.save();
|
|
92
|
+
return project;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
save() {
|
|
96
|
+
const file = join(this.rootDir, 'projects.json');
|
|
97
|
+
const temp = `${file}.tmp`;
|
|
98
|
+
writeFileSync(temp, JSON.stringify({
|
|
99
|
+
activeProjectId: this.activeProjectId,
|
|
100
|
+
projects: this.projects,
|
|
101
|
+
}, null, 2), 'utf8');
|
|
102
|
+
renameSync(temp, file);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -26,7 +26,7 @@ export class ProjectRuntime {
|
|
|
26
26
|
this.service = service;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
static async open({ rootDir, projectId = DEFAULT_PROJECT_ID, now = undefined, seed = true, projectName = '我的项目', projectGoal = '把事交给团队', workspacePath = '', diag = null, pmoMaxRounds = undefined, teamPreset = null }) {
|
|
29
|
+
static async open({ rootDir, projectId = DEFAULT_PROJECT_ID, now = undefined, seed = true, projectName = '我的项目', projectGoal = '把事交给团队', workspacePath = '', diag = null, pmoMaxRounds = undefined, teamPreset = null, blueprint = null }) {
|
|
30
30
|
const dir = join(rootDir, 'projects', projectId);
|
|
31
31
|
mkdirSync(dir, { recursive: true });
|
|
32
32
|
const ledger = openLedger({
|
|
@@ -87,7 +87,7 @@ export class ProjectRuntime {
|
|
|
87
87
|
projectId,
|
|
88
88
|
// 播种是内部路径:允许先建一个「待设定目录」的默认项目,用户在首次使用
|
|
89
89
|
// 引导里选定目录后再 project.workspace.update。
|
|
90
|
-
|
|
90
|
+
payload: { id: projectId, name: projectName, goal: projectGoal, workspacePath, seed: true, blueprint },
|
|
91
91
|
});
|
|
92
92
|
}
|
|
93
93
|
await seedDefaultRoster(service, projectId, { humanId: HUMAN_ID, ...(teamPreset ? { preset: teamPreset } : {}) });
|
package/lib/host/risk-view.js
CHANGED
|
@@ -1,99 +1,99 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 风险清单投影(R-07 / 视图组合纪律 V1):三类需要 Human 注意的风险,
|
|
3
|
-
* 全部从既有账本投影只读聚合,零新事件、零新数据结构——
|
|
4
|
-
* 1. 受阻任务:pending 且依赖未完成(isTaskBlocked);
|
|
5
|
-
* 2. 失败成员:agent 带未清除的 lastFailure(或 status=failed);
|
|
6
|
-
* 3. 待决超时:Human 收件箱中 unread 的 decision_required,超过 staleHours。
|
|
7
|
-
*
|
|
8
|
-
* 输出行复用 quadrantItem 的形状约定(kind/taskId/taskNumber/title/...),
|
|
9
|
-
* 前端零新交互模式即可渲染。
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { isTaskBlocked } from '../ledger/fold.js';
|
|
13
|
-
|
|
14
|
-
const RISK_KINDS = Object.freeze({
|
|
15
|
-
BLOCKED_TASK: 'blocked_task',
|
|
16
|
-
FAILED_MEMBER: 'failed_member',
|
|
17
|
-
STALE_DECISION: 'stale_decision',
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
export function riskView(state, { now = Date.now(), staleHours = 24 } = {}) {
|
|
21
|
-
const humanId = state.humanMemberId
|
|
22
|
-
?? Object.values(state.members ?? {}).find((member) => member.kind === 'human')?.id
|
|
23
|
-
?? null;
|
|
24
|
-
const cutoff = now - staleHours * 60 * 60 * 1000;
|
|
25
|
-
|
|
26
|
-
const blocked = [];
|
|
27
|
-
for (const task of Object.values(state.tasks ?? {})) {
|
|
28
|
-
if (task.tombstone === true) continue;
|
|
29
|
-
if (task.status !== 'pending') continue;
|
|
30
|
-
if (!isTaskBlocked(state, task)) continue;
|
|
31
|
-
blocked.push({
|
|
32
|
-
kind: RISK_KINDS.BLOCKED_TASK,
|
|
33
|
-
taskId: task.id,
|
|
34
|
-
taskNumber: task.number ?? null,
|
|
35
|
-
title: task.title ?? '',
|
|
36
|
-
ownerId: task.ownerId ?? null,
|
|
37
|
-
ownerName: state.members?.[task.ownerId]?.name ?? task.ownerId ?? null,
|
|
38
|
-
blockedBy: Array.isArray(task.blockedBy) ? [...task.blockedBy] : [],
|
|
39
|
-
waitingSince: task.updatedAt ?? null,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const failures = [];
|
|
44
|
-
for (const member of Object.values(state.members ?? {})) {
|
|
45
|
-
if (member.kind !== 'agent') continue;
|
|
46
|
-
const failure = member.lastFailure;
|
|
47
|
-
if (member.status !== 'failed' && (failure === undefined || failure === null)) continue;
|
|
48
|
-
// Ledgers written before the fold cleared lastFailure on recovery:
|
|
49
|
-
// a failure older than the last recovery is not an open risk.
|
|
50
|
-
if (failure !== undefined && failure !== null
|
|
51
|
-
&& Number.isFinite(member.lastRecoveredAt) && Number.isFinite(failure.at)
|
|
52
|
-
&& member.lastRecoveredAt >= failure.at) continue;
|
|
53
|
-
failures.push({
|
|
54
|
-
kind: RISK_KINDS.FAILED_MEMBER,
|
|
55
|
-
memberId: member.id,
|
|
56
|
-
name: member.name ?? member.id,
|
|
57
|
-
role: member.role ?? '',
|
|
58
|
-
status: member.status ?? 'idle',
|
|
59
|
-
category: failure?.category ?? null,
|
|
60
|
-
attempt: failure?.attempt ?? (member.attempts ?? 0),
|
|
61
|
-
message: failure?.message ?? member.failure ?? null,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const staleDecisions = [];
|
|
66
|
-
for (const item of Object.values(state.inbox ?? {})) {
|
|
67
|
-
if (humanId !== null && item.memberId !== humanId) continue;
|
|
68
|
-
if (item.type !== 'decision_required' || item.requiresDecision !== true) continue;
|
|
69
|
-
if (item.state !== 'unread') continue;
|
|
70
|
-
const createdAt = item.createdAt ?? null;
|
|
71
|
-
const waitingHours = createdAt !== null && Number.isFinite(createdAt)
|
|
72
|
-
? Math.max(0, Math.round((now - createdAt) / (60 * 60 * 1000)))
|
|
73
|
-
: null;
|
|
74
|
-
if (createdAt === null || now - createdAt < staleHours * 60 * 60 * 1000) continue;
|
|
75
|
-
staleDecisions.push({
|
|
76
|
-
kind: RISK_KINDS.STALE_DECISION,
|
|
77
|
-
itemId: item.id,
|
|
78
|
-
title: item.ref?.title ?? item.type ?? 'decision_required',
|
|
79
|
-
taskId: item.ref?.taskId ?? null,
|
|
80
|
-
waitingHours,
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
staleDecisions.sort((left, right) => (right.waitingHours ?? 0) - (left.waitingHours ?? 0));
|
|
84
|
-
|
|
85
|
-
return {
|
|
86
|
-
rev: state.lastSeq ?? 0,
|
|
87
|
-
generatedAt: now,
|
|
88
|
-
blocked,
|
|
89
|
-
failures,
|
|
90
|
-
staleDecisions,
|
|
91
|
-
counts: {
|
|
92
|
-
blocked: blocked.length,
|
|
93
|
-
failures: failures.length,
|
|
94
|
-
staleDecisions: staleDecisions.length,
|
|
95
|
-
},
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export { RISK_KINDS };
|
|
1
|
+
/**
|
|
2
|
+
* 风险清单投影(R-07 / 视图组合纪律 V1):三类需要 Human 注意的风险,
|
|
3
|
+
* 全部从既有账本投影只读聚合,零新事件、零新数据结构——
|
|
4
|
+
* 1. 受阻任务:pending 且依赖未完成(isTaskBlocked);
|
|
5
|
+
* 2. 失败成员:agent 带未清除的 lastFailure(或 status=failed);
|
|
6
|
+
* 3. 待决超时:Human 收件箱中 unread 的 decision_required,超过 staleHours。
|
|
7
|
+
*
|
|
8
|
+
* 输出行复用 quadrantItem 的形状约定(kind/taskId/taskNumber/title/...),
|
|
9
|
+
* 前端零新交互模式即可渲染。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { isTaskBlocked } from '../ledger/fold.js';
|
|
13
|
+
|
|
14
|
+
const RISK_KINDS = Object.freeze({
|
|
15
|
+
BLOCKED_TASK: 'blocked_task',
|
|
16
|
+
FAILED_MEMBER: 'failed_member',
|
|
17
|
+
STALE_DECISION: 'stale_decision',
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export function riskView(state, { now = Date.now(), staleHours = 24 } = {}) {
|
|
21
|
+
const humanId = state.humanMemberId
|
|
22
|
+
?? Object.values(state.members ?? {}).find((member) => member.kind === 'human')?.id
|
|
23
|
+
?? null;
|
|
24
|
+
const cutoff = now - staleHours * 60 * 60 * 1000;
|
|
25
|
+
|
|
26
|
+
const blocked = [];
|
|
27
|
+
for (const task of Object.values(state.tasks ?? {})) {
|
|
28
|
+
if (task.tombstone === true) continue;
|
|
29
|
+
if (task.status !== 'pending') continue;
|
|
30
|
+
if (!isTaskBlocked(state, task)) continue;
|
|
31
|
+
blocked.push({
|
|
32
|
+
kind: RISK_KINDS.BLOCKED_TASK,
|
|
33
|
+
taskId: task.id,
|
|
34
|
+
taskNumber: task.number ?? null,
|
|
35
|
+
title: task.title ?? '',
|
|
36
|
+
ownerId: task.ownerId ?? null,
|
|
37
|
+
ownerName: state.members?.[task.ownerId]?.name ?? task.ownerId ?? null,
|
|
38
|
+
blockedBy: Array.isArray(task.blockedBy) ? [...task.blockedBy] : [],
|
|
39
|
+
waitingSince: task.updatedAt ?? null,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const failures = [];
|
|
44
|
+
for (const member of Object.values(state.members ?? {})) {
|
|
45
|
+
if (member.kind !== 'agent') continue;
|
|
46
|
+
const failure = member.lastFailure;
|
|
47
|
+
if (member.status !== 'failed' && (failure === undefined || failure === null)) continue;
|
|
48
|
+
// Ledgers written before the fold cleared lastFailure on recovery:
|
|
49
|
+
// a failure older than the last recovery is not an open risk.
|
|
50
|
+
if (failure !== undefined && failure !== null
|
|
51
|
+
&& Number.isFinite(member.lastRecoveredAt) && Number.isFinite(failure.at)
|
|
52
|
+
&& member.lastRecoveredAt >= failure.at) continue;
|
|
53
|
+
failures.push({
|
|
54
|
+
kind: RISK_KINDS.FAILED_MEMBER,
|
|
55
|
+
memberId: member.id,
|
|
56
|
+
name: member.name ?? member.id,
|
|
57
|
+
role: member.role ?? '',
|
|
58
|
+
status: member.status ?? 'idle',
|
|
59
|
+
category: failure?.category ?? null,
|
|
60
|
+
attempt: failure?.attempt ?? (member.attempts ?? 0),
|
|
61
|
+
message: failure?.message ?? member.failure ?? null,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const staleDecisions = [];
|
|
66
|
+
for (const item of Object.values(state.inbox ?? {})) {
|
|
67
|
+
if (humanId !== null && item.memberId !== humanId) continue;
|
|
68
|
+
if (item.type !== 'decision_required' || item.requiresDecision !== true) continue;
|
|
69
|
+
if (item.state !== 'unread') continue;
|
|
70
|
+
const createdAt = item.createdAt ?? null;
|
|
71
|
+
const waitingHours = createdAt !== null && Number.isFinite(createdAt)
|
|
72
|
+
? Math.max(0, Math.round((now - createdAt) / (60 * 60 * 1000)))
|
|
73
|
+
: null;
|
|
74
|
+
if (createdAt === null || now - createdAt < staleHours * 60 * 60 * 1000) continue;
|
|
75
|
+
staleDecisions.push({
|
|
76
|
+
kind: RISK_KINDS.STALE_DECISION,
|
|
77
|
+
itemId: item.id,
|
|
78
|
+
title: item.ref?.title ?? item.type ?? 'decision_required',
|
|
79
|
+
taskId: item.ref?.taskId ?? null,
|
|
80
|
+
waitingHours,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
staleDecisions.sort((left, right) => (right.waitingHours ?? 0) - (left.waitingHours ?? 0));
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
rev: state.lastSeq ?? 0,
|
|
87
|
+
generatedAt: now,
|
|
88
|
+
blocked,
|
|
89
|
+
failures,
|
|
90
|
+
staleDecisions,
|
|
91
|
+
counts: {
|
|
92
|
+
blocked: blocked.length,
|
|
93
|
+
failures: failures.length,
|
|
94
|
+
staleDecisions: staleDecisions.length,
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export { RISK_KINDS };
|