dsh-bufeng 0.0.0-stage → 0.64.1
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/LICENSE +21 -0
- package/README.md +68 -2
- package/bufeng.patch.yml +3 -0
- package/client/build-client.mjs +159 -0
- package/client/client.js +4038 -0
- package/client/src/board.js +133 -0
- package/client/src/capabilities.js +787 -0
- package/client/src/cards.js +263 -0
- package/client/src/data.js +43 -0
- package/client/src/graph.js +558 -0
- package/client/src/icons.js +88 -0
- package/client/src/projects.js +467 -0
- package/client/src/prompt.js +55 -0
- package/client/src/react.js +7 -0
- package/client/src/settings.js +407 -0
- package/client/src/styles.js +611 -0
- package/client/src/taskboard.js +321 -0
- package/client/src/taskgraph.js +145 -0
- package/client/src/ui.js +72 -0
- package/lib/acceptance-subject.js +71 -0
- package/lib/board/server.js +546 -0
- package/lib/board/snapshot.js +133 -0
- package/lib/board/taskview.js +151 -0
- package/lib/bootstrap.js +77 -0
- package/lib/catalog.js +385 -0
- package/lib/core.js +478 -0
- package/lib/data/capability-alias-catalog.json +301 -0
- package/lib/data/capability-alias.json +25 -0
- package/lib/domain.js +1342 -0
- package/lib/host-probe.js +176 -0
- package/lib/identity-hook.js +490 -0
- package/lib/index.js +119 -0
- package/lib/lessons.js +134 -0
- package/lib/metering.js +109 -0
- package/lib/scope.js +389 -0
- package/lib/session/child-watch.js +86 -0
- package/lib/session/evidence.js +118 -0
- package/lib/session/node-session.js +335 -0
- package/lib/session/queue.js +307 -0
- package/lib/session/reconcile.js +500 -0
- package/lib/tools/capabilities.js +1461 -0
- package/lib/tools/governance.js +579 -0
- package/lib/tools/index.js +33 -0
- package/lib/tools/project.js +500 -0
- package/lib/tools/readonly.js +566 -0
- package/lib/tools/recover.js +402 -0
- package/lib/tools/run.js +1609 -0
- package/lib/tools/skills.js +162 -0
- package/lib/tools/trees.js +637 -0
- package/lib/workspace-rmw.js +365 -0
- package/package.json +42 -4
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/tools/index.js — bufeng_* 工具的注册编排(按域分文件,本文件只做组装)。
|
|
3
|
+
* 闭包持有 boot 时的 ledger 引用;execute 内部仍按「磁盘 JSON 为真身」纪律重读账本。
|
|
4
|
+
* v0.12.1:TOOL_COUNT 集中声明(注册完成事件动态引用,不再散落硬编码);
|
|
5
|
+
* 冒烟里有「18 工具守恒」断言盯着——增删工具必须同步这里与 tests/。
|
|
6
|
+
* v0.29.3 计数如实化:registerAllTools 返回实数 {ok, failed, total}——注册尾注据实播报,
|
|
7
|
+
* 静态 TOOL_COUNT 不再单独充当「全数」口径(bufeng_skill schema 事故:注册 17 只仍报 18)。
|
|
8
|
+
*/
|
|
9
|
+
import { registerReadonlyTools } from './readonly.js';
|
|
10
|
+
import { registerTreeTools } from './trees.js';
|
|
11
|
+
import { registerRunTools } from './run.js';
|
|
12
|
+
import { registerGovernanceTools } from './governance.js';
|
|
13
|
+
import { registerProjectTools } from './project.js';
|
|
14
|
+
import { registerCapabilitiesTools } from './capabilities.js';
|
|
15
|
+
import { registerSkillTools } from './skills.js';
|
|
16
|
+
import { registerRecoverTools } from './recover.js'; // v0.51.0 P3 重连:第 20 只工具 bufeng_task_recover(孤儿任务收口)
|
|
17
|
+
import { resetRegisterStats, getRegisterStats } from '../core.js'; // v0.29.3:实数登记簿(tools→core 合法方向)
|
|
18
|
+
|
|
19
|
+
export const TOOL_COUNT = 20; // v0.51.0 P3 重连:bufeng_task_recover 入编(19→20,孤儿任务证据链收口 inspect/relink/close);此前教训管线批次1 bufeng_lesson(18→19)
|
|
20
|
+
|
|
21
|
+
export function registerAllTools(ctx, ledger) {
|
|
22
|
+
resetRegisterStats(); // v0.29.3 计数如实化:每轮注册实数清零(冒烟多次 apply 互不串账)
|
|
23
|
+
registerReadonlyTools(ctx, ledger);
|
|
24
|
+
registerTreeTools(ctx, ledger);
|
|
25
|
+
registerRunTools(ctx, ledger);
|
|
26
|
+
registerGovernanceTools(ctx, ledger);
|
|
27
|
+
registerProjectTools(ctx, ledger);
|
|
28
|
+
registerCapabilitiesTools(ctx, ledger);
|
|
29
|
+
registerSkillTools(ctx, ledger);
|
|
30
|
+
registerRecoverTools(ctx, ledger); // v0.51.0 P3:第 20 只(判据锚 m1/m13 工具数 20)
|
|
31
|
+
const s = getRegisterStats();
|
|
32
|
+
return { ok: s.ok.length, failed: s.failed, total: TOOL_COUNT }; // v0.29.3:实数(全成=TOOL_COUNT 现值、失败=N/总数+名单,尾注据此播报)
|
|
33
|
+
}
|
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/tools/project.js — v0.15.0 项目制:bufeng_project 工具(项目实体的增改查)。
|
|
3
|
+
*
|
|
4
|
+
* 设计(项目管理方案-v1.md):编制(树/都管/成员)与项目(业务目录+说明)两维分离。
|
|
5
|
+
* 项目只是「一个目录 + 一段业务说明」,派单时通过 projectId 关联(run.js dispatchCore 解析);
|
|
6
|
+
* 不再与树/节点绑定(v0.8.0 的树绑定工作区已退役,bufeng_workspace 删除)。
|
|
7
|
+
*
|
|
8
|
+
* 目录来源三档(v0.23.0 工作区归位):
|
|
9
|
+
* - 客户显式给 path → 用给定路径(项目可以在任何地方,新逻辑零介入);
|
|
10
|
+
* - 只给 name 且 gate.projectsRoot 已显式改配(≠初始缺省 GATE.projectsRoot)→ 落改配根(bufeng_config 意愿优先);
|
|
11
|
+
* - 只给 name 且未改配 → 落 DSH 工作区根(<工作区根>/<项目名>);发现失败回退旧缺省 ~/.dsh/bufeng/projects。
|
|
12
|
+
* (bufeng_project list 仍显示 gate.projectsRoot 当前配置值)
|
|
13
|
+
* v0.32.0-A delete(k-mvgeqnuu0e5,永久删除登记):只删账本 projects 行,项目目录物理文件零触碰;
|
|
14
|
+
* 身份闸=总管专属(与 bufeng_config 同口径:任一 active 树总管名单锚点命中,即 resolveCaller 的 isRoot 判定
|
|
15
|
+
* 在跨树全局实体上的扫描形态);护栏=未收口任务(running/queued/pending)在挂时拒删(文案指路「先收口」);
|
|
16
|
+
* 关联引用处置(零丢失红线):历史任务行与 tasks[].projectId 引用原样保留(审计线索),读取侧全部悬挂容错。
|
|
17
|
+
* v0.32.0-B unbind_workspace(k-mvgeqoix0f3,陈旧绑定治根通道):解绑 v0.8.0 workspaces 表里的陈旧绑定行
|
|
18
|
+
* (boundBy 指向已归档树、零在挂任务),治 reconcile 启动巡检 missing 噪音的根。挂面择优=项目制是工作区绑定
|
|
19
|
+
* 的继任实体(本文件头注在案)且 delete 已立同款治理范式,action 值扩张=最小面改动(不新增常设工具、不动
|
|
20
|
+
* 18 工具计数);workspaces 表本体仍是只读投影(本 action 是显式治理动作,非常规写面,看板侧零改动)。
|
|
21
|
+
* 安全条件(缺一拒解绑,文案全指路):行存在(path 全等)+taskCount=0+无未收口任务挂该路径+树引用非空
|
|
22
|
+
* 且全部可解析(悬挂=拒上报)+存在树全部 archived(活树绑定拒)。宿主 registry 协同=尽力而为软失败:
|
|
23
|
+
* resolveByPath 命中且 sessionIds 空数组才调宿主 delete(旧宿主无 API 跳过,reconcile.js 同款 typeof 先例);
|
|
24
|
+
* 零宿主文件写入(不碰 workspace.json)。事件 kind=bufeng.workspace.unbound,note 携被删行全量 JSON(审计可重建)。
|
|
25
|
+
* v0.38.0 项目绑定都管(k-mvi3wr3g0bg 需求①②):project 行新增可选字段 leaderNodeId(缺省 null,旧账本行不迁移照常读)。
|
|
26
|
+
* create 传 leaderNodeId=人工选择(校验:账本内 active 树 kind=leader 且未退役)后绑定;缺省=按项目 name+desc 语料
|
|
27
|
+
* 自动匹配(matchLeaderForProject:name×3/role×2/systemPrompt×1 加权,闸=matchedTokens≥2 且(核心域≥1 或 加权分≥3)),
|
|
28
|
+
* 命中→绑定并在返回体 note+bufeng.project.leader.bound 事件披露命中词与过闸候选;未命中→不绑定+返回体给
|
|
29
|
+
* 「bufeng_add_node kind=leader 建都管+成员 → bufeng_project update { id, leaderNodeId } 默认绑定」指引。
|
|
30
|
+
* update:leaderNodeId 传 string=换绑(同校验);显式 null=解绑;undefined=不动(区分键存在与否)。changed 与
|
|
31
|
+
* bufeng.project.updated 随动,绑/解绑落 bufeng.project.leader.bound / bufeng.project.leader.unbound 事件。
|
|
32
|
+
* list:每行富化 leader:{nodeId,name,treeId,status}|null;悬挂(id 不存在/非都管/已退役)→leader=null+leaderStale
|
|
33
|
+
* 标注不炸,note 给换绑指引。派单侧路由闸见 run.js dispatchCore(需求③)。
|
|
34
|
+
* v0.41.0 看板都管下拉(客户需求「项目中的督管分配,添加下拉选,可以由用户指定」):绑定/解绑核心抽
|
|
35
|
+
* leaderChangeOf(update 分支与看板共用同一校验+文案,锁内单一权威),导出 setProjectLeaderCore 供看板
|
|
36
|
+
* POST /bufeng/api/project/leader 调用(看板=总管代行,actor 入事件史)。v0.38.0 ADR
|
|
37
|
+
* 「2026-10-22-项目督管可见与未命中指引」曾定「看板保持只读红线、绑定走工具通道」,本版经客户明示需求推翻。
|
|
38
|
+
*/
|
|
39
|
+
import { mkdir, readdir } from 'node:fs/promises';
|
|
40
|
+
import { existsSync, statSync } from 'node:fs';
|
|
41
|
+
import { isAbsolute, join, resolve } from 'node:path';
|
|
42
|
+
import { GATE, BUFENG_DIR, resolveDshWorkspaceRoot, loadLedger, saveLedger, withLedgerLock, recordEvent, newId, safeRegister, objSchema, jsonRender } from '../core.js';
|
|
43
|
+
import { isRosterRoot, resolveProjectLeader, matchLeaderForProject, coverageReportOf } from '../domain.js'; // v0.32.0 delete 身份闸:active 树总管名单(与 bufeng_config/resolveCaller isRoot 同源判定);v0.38.0 项目绑定都管:正身解析+自动匹配(domain 纯函数);v0.43.0 覆盖矩阵报告(list 富化共用)
|
|
44
|
+
import { guardedRegistryMutation } from '../workspace-rmw.js'; // v0.62.1(ADR 2026-11-03):宿主 workspace.json lost-update 竞态守卫叶子
|
|
45
|
+
|
|
46
|
+
function normalizePath(p) {
|
|
47
|
+
const t = String(p ?? '').trim();
|
|
48
|
+
if (!t) return null;
|
|
49
|
+
return resolve(isAbsolute(t) ? t : join(process.cwd(), t));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function dirExists(p) {
|
|
53
|
+
try { return existsSync(p) && statSync(p).isDirectory(); } catch { return false; }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** v0.41.0 项目都管绑定/解绑核心(锁内单一权威):bufeng_project update leaderNodeId 与看板都管下拉
|
|
57
|
+
* (POST /bufeng/api/project/leader → setProjectLeaderCore)共用——leaderArg 三态:string=绑定/换绑
|
|
58
|
+
* (resolveProjectLeader 校验,失败抛错文案两侧一致,updateNodeModelCore 同款纪律);null=解绑(幂等);
|
|
59
|
+
* undefined=不动。changed 原位收集变更描述(update 工具同款文案),返回 { bindHit, unbindFrom }。 */
|
|
60
|
+
function leaderChangeOf(cur, p, leaderArg, changed) {
|
|
61
|
+
let bindHit = null;
|
|
62
|
+
let unbindFrom = null;
|
|
63
|
+
if (leaderArg === undefined) return { bindHit, unbindFrom };
|
|
64
|
+
if (leaderArg === null) {
|
|
65
|
+
if (p.leaderNodeId) { unbindFrom = p.leaderNodeId; changed.push(`解绑都管 ${p.leaderNodeId}`); p.leaderNodeId = null; }
|
|
66
|
+
return { bindHit, unbindFrom };
|
|
67
|
+
}
|
|
68
|
+
const hit = resolveProjectLeader(cur, leaderArg);
|
|
69
|
+
if (!hit) {
|
|
70
|
+
throw new Error(`leaderNodeId「${leaderArg}」不是可绑定的都管(须为账本内 active 树 kind=leader 且未退役的节点)。可用都管经 bufeng_trees 查询;新建都管:bufeng_add_node { treeId, name, role, kind: "leader" }`);
|
|
71
|
+
}
|
|
72
|
+
if (p.leaderNodeId !== hit.node.id) {
|
|
73
|
+
bindHit = hit;
|
|
74
|
+
changed.push(`绑定都管 ${hit.node.name}(${hit.node.id})`);
|
|
75
|
+
p.leaderNodeId = hit.node.id;
|
|
76
|
+
}
|
|
77
|
+
return { bindHit, unbindFrom };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** v0.41.0 看板返回体项目行:绑定正身富化投影(list 同款 leader/leaderStale,前端下拉即时对齐用)。 */
|
|
81
|
+
function projectLeaderView(cur, p) {
|
|
82
|
+
const bind = resolveProjectLeader(cur, p.leaderNodeId);
|
|
83
|
+
const leader = bind ? { nodeId: bind.node.id, name: bind.node.name, treeId: bind.tree.id, status: bind.node.status } : null;
|
|
84
|
+
return { ...p, leader, ...(!!p.leaderNodeId && !bind ? { leaderStale: true } : {}) };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 项目都管写核心(v0.41.0,客户需求「项目中的督管分配,添加下拉选,可以由用户指定」):
|
|
89
|
+
* 看板 POST /bufeng/api/project/leader 专用入口,与 bufeng_project update leaderNodeId 走同一条
|
|
90
|
+
* 校验路径(leaderChangeOf 锁内落账,单一权威)。leaderNodeId:string=绑定/换绑;null=解绑。
|
|
91
|
+
* 看板=总管代行(同 /api/retire 模式),actor 标注操作来源进事件史。
|
|
92
|
+
*/
|
|
93
|
+
export async function setProjectLeaderCore({ projectId, leaderNodeId, actor }) {
|
|
94
|
+
const arg = leaderNodeId === null ? null : String(leaderNodeId ?? '').trim();
|
|
95
|
+
if (arg !== null && !arg) throw new Error('leaderNodeId 须为非空字符串(绑定/换绑)或显式 null(解绑)');
|
|
96
|
+
return withLedgerLock(async () => {
|
|
97
|
+
const cur = await loadLedger();
|
|
98
|
+
cur.projects ??= [];
|
|
99
|
+
const p = cur.projects.find((x) => x.id === String(projectId ?? '').trim());
|
|
100
|
+
if (!p) throw new Error(`项目 ${projectId} 不存在(bufeng_project list 查询)`);
|
|
101
|
+
const prior = p.leaderNodeId ?? null;
|
|
102
|
+
const changed = [];
|
|
103
|
+
const { bindHit, unbindFrom } = leaderChangeOf(cur, p, arg, changed);
|
|
104
|
+
if (!changed.length) {
|
|
105
|
+
return { ok: true, project: projectLeaderView(cur, p), note: '没有需要修改的字段(当前绑定已是该都管)' };
|
|
106
|
+
}
|
|
107
|
+
const via = `看板下拉${actor ? `,${actor}` : ''}`;
|
|
108
|
+
recordEvent(cur, 'bufeng.project.updated', `项目「${p.name}」${p.id}:${changed.join(';')}`);
|
|
109
|
+
if (bindHit) recordEvent(cur, 'bufeng.project.leader.bound', `项目「${p.name}」${p.id} ${prior ? '换绑' : '绑定'}都管「${bindHit.node.name}」(${bindHit.node.id},树「${bindHit.tree.name}」)——人工选择(${via})`);
|
|
110
|
+
if (unbindFrom) recordEvent(cur, 'bufeng.project.leader.unbound', `项目「${p.name}」${p.id} 解绑都管(原 ${unbindFrom})——显式 null(${via})`);
|
|
111
|
+
await saveLedger(cur);
|
|
112
|
+
return { ok: true, project: projectLeaderView(cur, p), note: `已更新:${changed.join(';')}` };
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** v0.43.0 覆盖矩阵归一化(纯函数):接受 ①{stages:[...]} ②裸数组 [...] ③{环节:[...]}(中文键别名)
|
|
117
|
+
* ④以上任一形态的 JSON 字符串;归一化后返回 {stages:[{name,role,nodeId?,capability?}]};undefined/null/
|
|
118
|
+
* 空白串 → null(create 缺省=无矩阵,现有项目零迁移);v0.43.1 技术债a:归一化后 stages 为空数组({stages:[]}/[]/{环节:[]}/其 JSON 串)同样写侧归一 null(与读侧 no-plan 语义一致,update 传空 stages 等价清空,避免 0 环节假齐备矩阵)。环节校验:name 非空(trim 后 ≤60 字);role 缺省
|
|
119
|
+
* '主责'、仅接受 '主责'|'备份';nodeId/capability 至少一个非空字符串(都给时 nodeId 优先,两个都保留落账、
|
|
120
|
+
* 解析侧 nodeId 优先);总数 ≤64;非法即抛错(文案含格式模板与下一步指引 bufeng_capabilities coverage)。
|
|
121
|
+
* 调用点在锁外早拒(非法输入零副作用)。 */
|
|
122
|
+
const COVERAGE_PLAN_TEMPLATE = '格式模板:{"stages":[{"name":"环节名","role":"主责","nodeId":"节点id"}]}(name 必填 trim 后 ≤60 字;role 仅 主责|备份 缺省主责;nodeId 与 capability 至少一个非空字符串,都给时 nodeId 优先;≤64 环节)。下一步:bufeng_capabilities { action: "coverage" } 查看现有环节×席位对照';
|
|
123
|
+
|
|
124
|
+
function normalizeCoveragePlan(input) {
|
|
125
|
+
if (input === undefined || input === null) return null;
|
|
126
|
+
let v = input;
|
|
127
|
+
if (typeof v === 'string') {
|
|
128
|
+
const s = v.trim();
|
|
129
|
+
if (!s) return null;
|
|
130
|
+
try {
|
|
131
|
+
v = JSON.parse(s);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
throw new Error(`coveragePlan 不是合法 JSON:${err.message}。${COVERAGE_PLAN_TEMPLATE}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (v === null) return null;
|
|
137
|
+
let stages = null;
|
|
138
|
+
if (Array.isArray(v)) stages = v;
|
|
139
|
+
else if (v && typeof v === 'object' && Array.isArray(v.stages)) stages = v.stages;
|
|
140
|
+
else if (v && typeof v === 'object' && Array.isArray(v['环节'])) stages = v['环节'];
|
|
141
|
+
else throw new Error(`coveragePlan 形态不认:接受 {stages:[...]}、裸数组 [...]、{环节:[...]} 或任一形态的 JSON 字符串。${COVERAGE_PLAN_TEMPLATE}`);
|
|
142
|
+
if (stages.length > 64) throw new Error(`coveragePlan 环节数超限(${stages.length}>64)。${COVERAGE_PLAN_TEMPLATE}`);
|
|
143
|
+
if (!stages.length) return null; // v0.43.1 技术债a:归一化后 0 环节=无矩阵(写侧归一 null,与读侧 no-plan 一致;update 传空 stages 等价清空——避免 0 环节假齐备 present/complete 矩阵)
|
|
144
|
+
const out = [];
|
|
145
|
+
for (const st of stages) {
|
|
146
|
+
if (!st || typeof st !== 'object' || Array.isArray(st)) throw new Error(`coveragePlan 环节须为对象。${COVERAGE_PLAN_TEMPLATE}`);
|
|
147
|
+
const name = typeof st.name === 'string' ? st.name.trim() : '';
|
|
148
|
+
if (!name) throw new Error(`coveragePlan 环节 name 必填(非空字符串)。${COVERAGE_PLAN_TEMPLATE}`);
|
|
149
|
+
if (name.length > 60) throw new Error(`coveragePlan 环节 name 超长(${name.length}>60 字):「${name}」。${COVERAGE_PLAN_TEMPLATE}`);
|
|
150
|
+
let role = st.role === undefined || st.role === null || st.role === '' ? '主责' : st.role;
|
|
151
|
+
if (typeof role !== 'string') throw new Error(`coveragePlan 环节 role 须为字符串(主责|备份)。${COVERAGE_PLAN_TEMPLATE}`);
|
|
152
|
+
role = role.trim();
|
|
153
|
+
if (role !== '主责' && role !== '备份') throw new Error(`coveragePlan 环节 role 非法:「${st.role}」(仅 主责|备份,缺省主责)。${COVERAGE_PLAN_TEMPLATE}`);
|
|
154
|
+
const nodeId = typeof st.nodeId === 'string' && st.nodeId.trim() ? st.nodeId.trim() : null;
|
|
155
|
+
const capability = typeof st.capability === 'string' && st.capability.trim() ? st.capability.trim() : null;
|
|
156
|
+
if (!nodeId && !capability) throw new Error(`coveragePlan 环节「${name}」nodeId 与 capability 至少一个非空字符串(都给时 nodeId 优先)。${COVERAGE_PLAN_TEMPLATE}`);
|
|
157
|
+
out.push({ name, role, ...(nodeId ? { nodeId } : {}), ...(capability ? { capability } : {}) });
|
|
158
|
+
}
|
|
159
|
+
return { stages: out };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function registerProjectTools(ctx, ledger) {
|
|
163
|
+
// v0.15.2 教训:必须走 safeRegister + 声明 output——宿主 Tool.register 校验器强校验
|
|
164
|
+
// output { schema, render },且裸 register 抛错会炸掉整个 apply(bufeng.apply.error 事故)。
|
|
165
|
+
safeRegister(ctx, ledger, {
|
|
166
|
+
name: 'bufeng_project',
|
|
167
|
+
description:
|
|
168
|
+
'捕风工坊:项目管理(v0.15.0 项目制)。action=create 新建项目(name 必填;path 可选,缺省放项目根下同名目录;desc 一句话业务说明);action=list 列出全部项目、统计与当前项目根地址;action=update 改名称/说明/路径;action=delete 永久删除项目登记(v0.32.0:总管专属;只删账本登记,项目目录物理文件零触碰;有未收口任务(running/queued/pending)时拒删,先收口再删;历史任务记录保留审计);action=unbind_workspace 解绑陈旧工作区绑定(v0.32.0:总管专属;只允许解绑 boundBy 指向已归档树且零在挂任务的 workspaces 陈旧绑定行,操作留痕;活树绑定/有未收口任务/树引用悬挂均拒解绑并给指路)。项目与编制分离:派单时 bufeng_dispatch 传 projectId 关联,任务会话将自动按项目目录开工(cwd 随项目走)。v0.38.0 项目绑定都管:create 可传 leaderNodeId 显式绑定(缺省按项目语料自动匹配 active 树 kind=leader 都管,无匹配不绑定并给建都管+绑定指引);update 的 leaderNodeId 传 string 换绑、显式 null 解绑、不传不动;list 每行富化 leader:{nodeId,name,treeId,status}|null(悬挂/退役=leaderStale 标注+换绑指引);绑定项目的派单一律按绑定都管路由(见 bufeng_dispatch 路由闸)。v0.22.0:派单绑定项目时自动确保项目 tmp/ 目录并保证 .gitignore 覆盖(若有)。v0.43.0 编制覆盖机制:create/update 增可选参数 coveragePlan(环节×席位覆盖矩阵,接受 {stages:[...]} / 裸数组 / {环节:[...]} / 任一形态的 JSON 字符串,归一化后存 {stages:[{name,role,nodeId|capability}]};不传或显式 null=null 零迁移);update 三态:不传=不动、显式 null=清空、传值=覆盖;list 每行富化 coverage 键(present/complete/stageCount/gapCount/gaps);派单侧矩阵不齐备只告警不阻断(bufeng.task.coverageGap 事件)。',
|
|
169
|
+
parameters: {
|
|
170
|
+
type: 'object',
|
|
171
|
+
properties: {
|
|
172
|
+
action: { type: 'string', enum: ['create', 'list', 'update', 'delete', 'unbind_workspace'], description: 'create=新建;list=列出;update=修改;delete=永久删除登记(v0.32.0:只删账本行、目录文件不动、总管专属);unbind_workspace=解绑陈旧工作区绑定行(v0.32.0:path 必填、总管专属、安全条件缺一拒)' },
|
|
173
|
+
name: { type: 'string', description: '项目名(create 必填;list 时可作过滤关键词)' },
|
|
174
|
+
path: { type: 'string', description: '项目目录绝对路径(create/update 可选;unbind_workspace 必填=要解绑的绑定行路径,逐字抄自报错清单)' },
|
|
175
|
+
desc: { type: 'string', description: '一句话业务说明(create/update 可选)' },
|
|
176
|
+
leaderNodeId: { type: 'string', description: 'v0.38.0 绑定都管节点 id(create 可选:显式绑定,缺省自动匹配;update 可选:传 string=换绑、传 null=解绑、不传=不动)。须为账本内 active 树 kind=leader 且未退役的节点' },
|
|
177
|
+
coveragePlan: { type: 'object', description: 'v0.43.0 覆盖矩阵(create/update 可选):{stages:[{name,role,nodeId|capability}]} 或裸数组/中文键 {环节:[...]} 或其 JSON 字符串;环节 name 必填(trim 后 ≤60 字)、role 仅 主责|备份(缺省主责)、nodeId 与 capability 至少一个(都给时 nodeId 优先);≤64 环节。update:不传=不动、显式 null=清空' },
|
|
178
|
+
id: { type: 'string', description: '项目 id(update/delete 必填,如 p-mv3xxxxx)' }
|
|
179
|
+
},
|
|
180
|
+
required: ['action']
|
|
181
|
+
},
|
|
182
|
+
output: {
|
|
183
|
+
schema: objSchema({
|
|
184
|
+
ok: { type: 'boolean' },
|
|
185
|
+
project: { type: 'object' },
|
|
186
|
+
projectsRoot: { type: 'string' },
|
|
187
|
+
count: { type: 'number' },
|
|
188
|
+
projects: { type: 'array' },
|
|
189
|
+
workspace: { type: 'object' },
|
|
190
|
+
note: { type: 'string' }
|
|
191
|
+
}),
|
|
192
|
+
render: jsonRender
|
|
193
|
+
},
|
|
194
|
+
async execute(args, exec) {
|
|
195
|
+
const action = String(args?.action ?? '');
|
|
196
|
+
// v0.27 名单化:observer 预检随席位语义退役删除(create/update 恢复 v0.24 无身份闸行为,P2 挂账既有方向)
|
|
197
|
+
|
|
198
|
+
if (action === 'create') {
|
|
199
|
+
const name = String(args?.name ?? '').trim();
|
|
200
|
+
if (!name) throw new Error('项目名(name)必填');
|
|
201
|
+
const desc = String(args?.desc ?? '').trim() || null;
|
|
202
|
+
// v0.38.0 项目绑定都管(需求①②):显式 leaderNodeId=人工选择;缺省=自动匹配。参数形态早拒(锁外无副作用)
|
|
203
|
+
const leaderRaw = args?.leaderNodeId;
|
|
204
|
+
if (leaderRaw !== undefined && leaderRaw !== null && (typeof leaderRaw !== 'string' || !leaderRaw.trim())) {
|
|
205
|
+
throw new Error('leaderNodeId 须为非空字符串(账本内 active 树 kind=leader 且未退役的节点 id)或省略(缺省按项目语料自动匹配都管)');
|
|
206
|
+
}
|
|
207
|
+
const leaderArg = typeof leaderRaw === 'string' ? leaderRaw.trim() : null;
|
|
208
|
+
const coveragePlan = normalizeCoveragePlan(args?.coveragePlan); // v0.43.0 覆盖矩阵:锁外归一化早拒(非法输入零副作用);缺省/null → null
|
|
209
|
+
const explicit = normalizePath(args?.path);
|
|
210
|
+
return withLedgerLock(async () => {
|
|
211
|
+
const cur = await loadLedger();
|
|
212
|
+
cur.projects ??= [];
|
|
213
|
+
// v0.23.0 目录三档:显式改配 > DSH 工作区根 > 旧缺省 <BUFENG_DIR>/projects(发现失败回退)
|
|
214
|
+
const configuredRoot = cur.config?.gate?.projectsRoot;
|
|
215
|
+
let root;
|
|
216
|
+
if (configuredRoot && configuredRoot !== GATE.projectsRoot) {
|
|
217
|
+
root = configuredRoot; // 客户经 bufeng_config 显式改配,意愿优先
|
|
218
|
+
} else {
|
|
219
|
+
const wsRoot = await resolveDshWorkspaceRoot();
|
|
220
|
+
root = wsRoot === BUFENG_DIR ? join(BUFENG_DIR, 'projects') : wsRoot;
|
|
221
|
+
}
|
|
222
|
+
const path = explicit ?? join(root, name);
|
|
223
|
+
if (cur.projects.some((p) => p.name === name)) {
|
|
224
|
+
throw new Error(`项目「${name}」已存在(${cur.projects.find((p) => p.name === name).id}),直接用它的 id 派单即可`);
|
|
225
|
+
}
|
|
226
|
+
const canon = resolve(path);
|
|
227
|
+
const clash = cur.projects.find((p) => resolve(p.path) === canon);
|
|
228
|
+
if (clash) throw new Error(`目录已登记为项目「${clash.name}」(${clash.id}),一个目录一个项目`);
|
|
229
|
+
if (dirExists(canon)) {
|
|
230
|
+
const entries = await readdir(canon).catch(() => []);
|
|
231
|
+
if (entries.length > 0) throw new Error(`目录已存在且非空:${canon}(${entries.length} 个条目)。要复用现有目录,请用 update 的 path 绑定它,或换一个名字,避免误装进别人项目`);
|
|
232
|
+
}
|
|
233
|
+
// v0.38.0 绑定解析(人工选择/自动匹配;在 mkdir 之前,非法绑定零副作用)。
|
|
234
|
+
// 命中→绑定+披露命中词与过闸候选;未命中→不绑定+建都管与绑定指引
|
|
235
|
+
let bind = null; // { nodeId, leaderName, treeName, matched:[], passed:[], auto }
|
|
236
|
+
let leaderMiss = false;
|
|
237
|
+
if (leaderArg) {
|
|
238
|
+
const hit = resolveProjectLeader(cur, leaderArg);
|
|
239
|
+
if (!hit) {
|
|
240
|
+
throw new Error(`leaderNodeId「${leaderArg}」不是可绑定的都管(须为账本内 active 树 kind=leader 且未退役的节点)。可用都管经 bufeng_trees 查询;新建都管:bufeng_add_node { treeId, name, role, kind: "leader" }`);
|
|
241
|
+
}
|
|
242
|
+
bind = { nodeId: hit.node.id, leaderName: hit.node.name, treeName: hit.tree.name, matched: [], passed: [], auto: false };
|
|
243
|
+
} else {
|
|
244
|
+
const m = matchLeaderForProject(name, desc, cur.trees);
|
|
245
|
+
if (m.best) bind = { nodeId: m.best.leader.id, leaderName: m.best.leader.name, treeName: m.best.tree.name, matched: m.best.matched, passed: m.passed, auto: true };
|
|
246
|
+
else leaderMiss = true;
|
|
247
|
+
}
|
|
248
|
+
await mkdir(canon, { recursive: true });
|
|
249
|
+
const project = { id: newId('p'), name, path: canon, desc, missing: false, createdAt: new Date().toISOString(), leaderNodeId: bind?.nodeId ?? null, coveragePlan };
|
|
250
|
+
cur.projects.push(project);
|
|
251
|
+
recordEvent(cur, 'bufeng.project.created', `项目「${name}」${project.id} → ${canon}${desc ? `(${desc})` : ''}${bind ? `;绑定都管「${bind.leaderName}」(${bind.nodeId})——${bind.auto ? '自动匹配' : '人工选择'}` : ';自动匹配未命中都管(未绑定)'}${coveragePlan ? `;覆盖矩阵 ${coveragePlan.stages.length} 环节` : ';覆盖矩阵未定义'}`);
|
|
252
|
+
if (bind) {
|
|
253
|
+
recordEvent(cur, 'bufeng.project.leader.bound', `项目「${name}」${project.id} 绑定都管「${bind.leaderName}」(${bind.nodeId},树「${bind.treeName}」)——${bind.auto ? `自动匹配:命中词 ${bind.matched.join('、')};过闸候选:${bind.passed.join('、')}` : '人工选择(create leaderNodeId)'}`);
|
|
254
|
+
}
|
|
255
|
+
await saveLedger(cur);
|
|
256
|
+
const leaderBindNote = bind
|
|
257
|
+
? `${bind.auto ? '自动匹配绑定' : '已绑定'}都管「${bind.leaderName}」(${bind.nodeId},树「${bind.treeName}」)${bind.auto ? `:命中词 ${bind.matched.join('、')};过闸候选:${bind.passed.join('、')}` : '——人工选择'}`
|
|
258
|
+
: null;
|
|
259
|
+
const leaderMissNote = leaderMiss
|
|
260
|
+
? `自动匹配未命中都管(未绑定)。指引:bufeng_add_node { treeId, name, role, kind: "leader" } 新建都管并按需建名下成员 → bufeng_project { action: "update", id: "${project.id}", leaderNodeId: 新都管id } 默认绑定新增者`
|
|
261
|
+
: null;
|
|
262
|
+
const leaderMissPayload = leaderMiss ? {
|
|
263
|
+
projectId: project.id,
|
|
264
|
+
suggestedName: `${name}都管`,
|
|
265
|
+
next: [
|
|
266
|
+
`bufeng_add_node { treeId, name: "${name}都管", role: "项目都管:${name}", kind: "leader" }`,
|
|
267
|
+
`可选:在新都管下建成员(bufeng_add_node { treeId, name, role, kind: "member" })`,
|
|
268
|
+
`bufeng_project { action: "update", id: "${project.id}", leaderNodeId: "<新都管 id>" }`
|
|
269
|
+
]
|
|
270
|
+
} : null;
|
|
271
|
+
return { ok: true, project, projectsRoot: root, ...(leaderMissPayload ? { leaderMiss: leaderMissPayload } : {}), note: `项目已就绪。派单:bufeng_dispatch { projectId: "${project.id}", ... },节点会话将自动以 ${canon} 为工作目录${leaderBindNote ? `。${leaderBindNote}` : ''}${leaderMissNote ? `。${leaderMissNote}` : ''}` };
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (action === 'list') {
|
|
276
|
+
const cur = await loadLedger();
|
|
277
|
+
cur.projects ??= [];
|
|
278
|
+
const root = cur.config?.gate?.projectsRoot ?? GATE.projectsRoot;
|
|
279
|
+
const kw = String(args?.name ?? '').trim();
|
|
280
|
+
let list = cur.projects;
|
|
281
|
+
if (kw) list = list.filter((p) => p.name.includes(kw) || (p.desc ?? '').includes(kw));
|
|
282
|
+
const items = list.map((p) => {
|
|
283
|
+
const tasks = cur.tasks.filter((t) => t.projectId === p.id);
|
|
284
|
+
// v0.38.0 富化:绑定都管正身 {nodeId,name,treeId,status}|null;悬挂(登记 id 不存在/非都管/已退役)→leader=null+leaderStale 标注不炸
|
|
285
|
+
const bind = resolveProjectLeader(cur, p.leaderNodeId);
|
|
286
|
+
const leader = bind ? { nodeId: bind.node.id, name: bind.node.name, treeId: bind.tree.id, status: bind.node.status } : null;
|
|
287
|
+
const leaderStale = !!p.leaderNodeId && !bind;
|
|
288
|
+
const cov = coverageReportOf(p, cur); // v0.43.0 覆盖矩阵富化(旧项目无 coveragePlan 键=无矩阵,读取侧按 null 容错零迁移)
|
|
289
|
+
return {
|
|
290
|
+
...p,
|
|
291
|
+
leader,
|
|
292
|
+
...(leaderStale ? { leaderStale: true } : {}),
|
|
293
|
+
live: dirExists(p.path), // 当前目录是否真实存在(不入账)
|
|
294
|
+
taskCount: tasks.length,
|
|
295
|
+
running: tasks.filter((t) => t.status === 'running').length,
|
|
296
|
+
done: tasks.filter((t) => t.status === 'done').length,
|
|
297
|
+
lastTaskAt: tasks.length ? tasks[tasks.length - 1].createdAt : null,
|
|
298
|
+
coverage: { present: cov.present, complete: cov.complete, stageCount: cov.stages.length, gapCount: cov.gaps.length, gaps: cov.gaps }
|
|
299
|
+
};
|
|
300
|
+
});
|
|
301
|
+
const stale = items.filter((x) => x.leaderStale);
|
|
302
|
+
const staleNote = stale.length
|
|
303
|
+
? `。⚠️ 悬挂绑定:${stale.map((x) => `「${x.name}」(${x.id}→${x.leaderNodeId})`).join('、')} 登记的都管已失效(不存在/非都管/已退役)——换绑:bufeng_project { action: "update", id, leaderNodeId: 新都管id }(或显式 null 解绑)`
|
|
304
|
+
: '';
|
|
305
|
+
return {
|
|
306
|
+
ok: true,
|
|
307
|
+
projectsRoot: root,
|
|
308
|
+
count: items.length,
|
|
309
|
+
projects: items,
|
|
310
|
+
note: items.length
|
|
311
|
+
? `派单时把 projectId 传给 bufeng_dispatch;目录缺失(live=false)的项目先恢复目录或 update 改路径;绑定项目的派单按绑定都管路由${staleNote}`
|
|
312
|
+
: `项目库为空(项目根:${root})。新建:bufeng_project create { name, desc? }(总管代建:识别客户业务后建项目再匹配都管)${staleNote}`
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (action === 'update') {
|
|
317
|
+
const id = String(args?.id ?? '').trim();
|
|
318
|
+
if (!id) throw new Error('update 需要 id(bufeng_project list 查询)');
|
|
319
|
+
const name = typeof args?.name === 'string' && args.name.trim() ? args.name.trim() : null;
|
|
320
|
+
const descRaw = args?.desc;
|
|
321
|
+
// v0.38.0(需求①):leaderNodeId 传 string=人工选择/换绑;显式 null=解绑;undefined=不动(区分「键存在值为 null」与「键不存在」)
|
|
322
|
+
const leaderRaw = args?.leaderNodeId;
|
|
323
|
+
if (leaderRaw !== undefined && leaderRaw !== null && (typeof leaderRaw !== 'string' || !leaderRaw.trim())) {
|
|
324
|
+
throw new Error('leaderNodeId 须为非空字符串(人工选择/换绑)或显式 null(解绑);不传=不动当前绑定');
|
|
325
|
+
}
|
|
326
|
+
const leaderArg = leaderRaw === undefined ? undefined : (leaderRaw === null ? null : leaderRaw.trim());
|
|
327
|
+
// v0.43.0 覆盖矩阵三态:undefined=不动;显式 null=清空(写 null);传值=覆盖(锁外归一化早拒,同值幂等)
|
|
328
|
+
const coverageArg = args?.coveragePlan === undefined ? undefined : (args.coveragePlan === null ? null : normalizeCoveragePlan(args.coveragePlan));
|
|
329
|
+
const path = normalizePath(args?.path);
|
|
330
|
+
return withLedgerLock(async () => {
|
|
331
|
+
const cur = await loadLedger();
|
|
332
|
+
cur.projects ??= [];
|
|
333
|
+
const p = cur.projects.find((x) => x.id === id);
|
|
334
|
+
if (!p) throw new Error(`项目 ${id} 不存在(bufeng_project list 查询)`);
|
|
335
|
+
const changed = [];
|
|
336
|
+
if (name && name !== p.name) {
|
|
337
|
+
if (cur.projects.some((x) => x.id !== id && x.name === name)) throw new Error(`项目名「${name}」已被占用`);
|
|
338
|
+
changed.push(`名称 ${p.name}→${name}`);
|
|
339
|
+
p.name = name;
|
|
340
|
+
}
|
|
341
|
+
if (typeof descRaw === 'string') { changed.push('说明'); p.desc = descRaw.trim() || null; }
|
|
342
|
+
if (path && resolve(path) !== resolve(p.path)) {
|
|
343
|
+
const canon = resolve(path);
|
|
344
|
+
if (cur.projects.some((x) => x.id !== id && resolve(x.path) === canon)) throw new Error('该目录已登记为其他项目');
|
|
345
|
+
changed.push(`路径 ${p.path}→${canon}`);
|
|
346
|
+
p.path = canon;
|
|
347
|
+
await mkdir(canon, { recursive: true }).catch(() => {});
|
|
348
|
+
}
|
|
349
|
+
// v0.38.0 绑定变更:string=换绑(同 create 校验);显式 null=解绑(已解绑时幂等无改动);undefined=不动。
|
|
350
|
+
// v0.41.0:绑定/解绑核心抽 leaderChangeOf——与看板都管下拉(setProjectLeaderCore)共用同一校验与文案
|
|
351
|
+
const { bindHit, unbindFrom } = leaderChangeOf(cur, p, leaderArg, changed);
|
|
352
|
+
// v0.43.0 覆盖矩阵变更:同值幂等无改动;写 null=清空;写值=覆盖(changed 文案「覆盖计划(N 环节)」)
|
|
353
|
+
if (coverageArg !== undefined) {
|
|
354
|
+
const same = JSON.stringify(coverageArg) === JSON.stringify(p.coveragePlan ?? null);
|
|
355
|
+
if (!same) {
|
|
356
|
+
changed.push(coverageArg ? `覆盖计划(${coverageArg.stages.length} 环节)` : '覆盖计划清空');
|
|
357
|
+
p.coveragePlan = coverageArg;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (!changed.length) return { ok: true, project: p, note: '没有需要修改的字段' };
|
|
361
|
+
p.missing = false; // 路径更新后视为健康,等下次巡检复核
|
|
362
|
+
recordEvent(cur, 'bufeng.project.updated', `项目「${p.name}」${id}:${changed.join(';')}`);
|
|
363
|
+
if (bindHit) recordEvent(cur, 'bufeng.project.leader.bound', `项目「${p.name}」${id} 换绑都管「${bindHit.node.name}」(${bindHit.node.id},树「${bindHit.tree.name}」)——人工选择(update leaderNodeId)`);
|
|
364
|
+
if (unbindFrom) recordEvent(cur, 'bufeng.project.leader.unbound', `项目「${p.name}」${id} 解绑都管(原 ${unbindFrom})——显式 null(update leaderNodeId)`);
|
|
365
|
+
await saveLedger(cur);
|
|
366
|
+
return { ok: true, project: p, note: `已更新:${changed.join(';')}` };
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (action === 'delete') {
|
|
371
|
+
// v0.32.0-A(k-mvgeqnuu0e5):永久删除项目登记——只删账本 projects 行,项目目录物理文件零触碰。
|
|
372
|
+
// 身份治理(总管专属):项目是跨树全局实体,取 resolveCaller 的 isRoot 判定(isRosterRoot)
|
|
373
|
+
// 的全局扫描形态——与 bufeng_config 完全同口径:任一 active 树总管名单锚点命中即总管。
|
|
374
|
+
const id = String(args?.id ?? '').trim();
|
|
375
|
+
if (!id) throw new Error('delete 需要 id(bufeng_project list 查询)');
|
|
376
|
+
const callerId = exec?.agent?.id ?? null;
|
|
377
|
+
const probe = await loadLedger();
|
|
378
|
+
const isRoot = !!callerId && probe.trees.some((t) => t.status === 'active' && isRosterRoot(t, callerId));
|
|
379
|
+
if (!isRoot) {
|
|
380
|
+
throw new Error('项目删除(delete)是总管专属:当前会话不在任何 active 树的总管名单内。请由名单内总管(客户会话)执行删除;需要加入名单时 bufeng_adopt { treeId }(autoAdopt=false 且名单非空须客户确认 confirm:true)。节点/成员需要删除项目时,写进 bufeng_report 的 summary 上报总管。');
|
|
381
|
+
}
|
|
382
|
+
return withLedgerLock(async () => {
|
|
383
|
+
const cur = await loadLedger();
|
|
384
|
+
cur.projects ??= [];
|
|
385
|
+
const idx = cur.projects.findIndex((x) => x.id === id);
|
|
386
|
+
if (idx < 0) throw new Error(`项目 ${id} 不存在(bufeng_project list 查询;若此前已删除则为重复删除)`);
|
|
387
|
+
const p = cur.projects[idx];
|
|
388
|
+
// 护栏:未收口任务在挂时拒删——先收口。running/queued 为规格明列;
|
|
389
|
+
// pending(投递中占位,v0.25 批2 状态语义)同样算在挂,一并堵删除竞态窗口。
|
|
390
|
+
const live = (cur.tasks ?? []).filter((k) => k.projectId === id && ['running', 'queued', 'pending'].includes(k.status));
|
|
391
|
+
if (live.length) {
|
|
392
|
+
const byS = ['running', 'queued', 'pending']
|
|
393
|
+
.map((s) => `${s}=${live.filter((k) => k.status === s).length}`)
|
|
394
|
+
.filter((s) => !s.endsWith('=0'))
|
|
395
|
+
.join('、');
|
|
396
|
+
throw new Error(`项目「${p.name}」(${id})还有未收口任务(${byS}),不能删除:先收口——bufeng_tasks 查看进度,执行节点用 bufeng_report 回报收尾(done/failed),排队单随收尾自动接力清空,全部收口后再删`);
|
|
397
|
+
}
|
|
398
|
+
// 关联引用处置(账本既有数据零丢失红线):历史任务行与其 projectId 引用原样保留(审计线索)。
|
|
399
|
+
// 读取侧全部悬挂容错:queue.js/snapshot/taskview 对查不到的项目按 null 兜底或按 id 过滤;
|
|
400
|
+
// 新派单引用已删 id 会被 dispatchCore 按未知项目拒(run.js)。项目目录物理文件零触碰(要物理清理目录请客户自行处理)。
|
|
401
|
+
cur.projects.splice(idx, 1);
|
|
402
|
+
const kept = (cur.tasks ?? []).filter((k) => k.projectId === id).length;
|
|
403
|
+
recordEvent(cur, 'bufeng.project.deleted', `项目「${p.name}」${id} 登记已删除(目录未动:${p.path}${kept ? `;历史任务 ${kept} 条保留审计` : ''})`);
|
|
404
|
+
await saveLedger(cur);
|
|
405
|
+
return {
|
|
406
|
+
ok: true,
|
|
407
|
+
project: p,
|
|
408
|
+
count: cur.projects.length,
|
|
409
|
+
note: `登记已删除(仅账本行,项目目录文件未动:${p.path})。${kept ? `历史任务 ${kept} 条保留(审计线索,读取侧悬挂容错)。` : ''}后续派单不要再传该 id(未知 projectId 会拒派)`
|
|
410
|
+
};
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (action === 'unbind_workspace') {
|
|
415
|
+
// v0.32.0-B(k-mvgeqoix0f3):workspaces 陈旧绑定治根通道——显式治理动作(表本体仍只读投影)。
|
|
416
|
+
const rawPath = String(args?.path ?? '').trim();
|
|
417
|
+
if (!rawPath) throw new Error('unbind_workspace 需要 path(要解绑的工作区绑定行路径,逐字抄自报错返回的绑定行清单)');
|
|
418
|
+
// 身份治理(总管专属):与 delete 同口径(跨树全局实体上的 isRosterRoot 全局扫描形态)。
|
|
419
|
+
const callerId = exec?.agent?.id ?? null;
|
|
420
|
+
const probe = await loadLedger();
|
|
421
|
+
const isRoot = !!callerId && probe.trees.some((t) => t.status === 'active' && isRosterRoot(t, callerId));
|
|
422
|
+
if (!isRoot) {
|
|
423
|
+
throw new Error('工作区解绑(unbind_workspace)是总管专属:当前会话不在任何 active 树的总管名单内。请由名单内总管(客户会话)执行解绑;需要加入名单时 bufeng_adopt { treeId }(autoAdopt=false 且名单非空须客户确认 confirm:true)。节点/成员需要解绑时,写进 bufeng_report 的 summary 上报总管。');
|
|
424
|
+
}
|
|
425
|
+
return withLedgerLock(async () => {
|
|
426
|
+
const cur = await loadLedger();
|
|
427
|
+
cur.workspaces ??= [];
|
|
428
|
+
const canon = resolve(rawPath);
|
|
429
|
+
const idx = cur.workspaces.findIndex((w) => resolve(w.path) === canon);
|
|
430
|
+
if (idx < 0) {
|
|
431
|
+
const listing = cur.workspaces.length
|
|
432
|
+
? cur.workspaces.map((w) => ` - ${w.path}${w.missing ? '(missing=true 陈旧绑定)' : ''}`).join('\n')
|
|
433
|
+
: ' (workspaces 表为空,没有需要解绑的行)';
|
|
434
|
+
throw new Error(`未找到绑定行:${canon}(path 全等匹配)。当前绑定行清单:\n${listing}\n请逐字复制清单中的 path 重试;没有目标行就无需解绑`);
|
|
435
|
+
}
|
|
436
|
+
const row = cur.workspaces[idx];
|
|
437
|
+
// 安全条件①:taskCount 必须为零(v0.8.0 绑定计数器;missing 标志不参与判定——只影响巡检噪音不改变陈旧性)
|
|
438
|
+
if ((row.taskCount ?? 0) !== 0) {
|
|
439
|
+
throw new Error(`安全条件不满足:绑定行 taskCount=${row.taskCount}≠0(${canon})。本通道只解绑零任务的陈旧绑定;若计数器疑似陈旧(实际无任务),先 bufeng_report 上报总管核查,不要强解`);
|
|
440
|
+
}
|
|
441
|
+
// 安全条件②:未收口任务兜底扫描——防计数器陈旧为零但任务仍在挂(running/queued/pending 同 delete 护栏口径)
|
|
442
|
+
const live = (cur.tasks ?? []).filter((k) => k.workspace && resolve(k.workspace) === canon && ['running', 'queued', 'pending'].includes(k.status));
|
|
443
|
+
if (live.length) {
|
|
444
|
+
throw new Error(`安全条件不满足:仍有 ${live.length} 条未收口任务(running/queued/pending)挂在该工作区(${canon})。先收口——bufeng_tasks 查看进度,执行节点用 bufeng_report 回报收尾(done/failed),全部收口后再解绑`);
|
|
445
|
+
}
|
|
446
|
+
// 安全条件③:树引用非空(boundBy/treeIds 均空=来历不明,拒并指路上报)
|
|
447
|
+
const refs = [...new Set([row.boundBy, ...(Array.isArray(row.treeIds) ? row.treeIds : [])].filter(Boolean))];
|
|
448
|
+
if (!refs.length) {
|
|
449
|
+
throw new Error(`安全条件不满足:绑定行无可校验树引用(boundBy/treeIds 均空,${canon})。来历不明的行请先 bufeng_report 上报总管核查,本通道只解绑可验证的陈旧绑定`);
|
|
450
|
+
}
|
|
451
|
+
// 安全条件④:引用全部可在账本 trees 内解析(悬挂=数据来历不明,拒并指路上报)
|
|
452
|
+
const dangling = refs.filter((r) => !cur.trees.some((t) => t.id === r));
|
|
453
|
+
if (dangling.length) {
|
|
454
|
+
throw new Error(`安全条件不满足:树引用悬挂(${dangling.join('、')} 不在账本 trees 内,${canon})。请先 bufeng_report 上报总管核查悬挂引用,本通道只解绑可验证的陈旧绑定`);
|
|
455
|
+
}
|
|
456
|
+
// 安全条件⑤:存在树必须全部已归档(活树仍在用该工作区,拒解绑)
|
|
457
|
+
const activeRefs = refs.filter((r) => { const t = cur.trees.find((x) => x.id === r); return t && t.status !== 'archived'; });
|
|
458
|
+
if (activeRefs.length) {
|
|
459
|
+
throw new Error(`安全条件不满足:活树绑定(${activeRefs.join('、')} 非 archived,${canon})。活树仍在使用该工作区不能解绑;确要解绑请先收口全部任务并归档整树后重试`);
|
|
460
|
+
}
|
|
461
|
+
// 宿主 registry 协同(尽力而为软失败,不阻断账本解绑):生产形状=ctx 顶层注入,旧嵌套 sc.workspaceRegistry 兼容
|
|
462
|
+
// (trees.js 同款读法);resolveByPath 命中且 sessionIds 为空数组才调宿主 delete——sessionIds 非空/形态未知
|
|
463
|
+
// 保守跳过;旧宿主无 delete API 跳过(reconcile.js 同款 typeof 先例)。零宿主文件写入(不碰 workspace.json)。
|
|
464
|
+
const reg = ctx?.workspaceRegistry ?? ctx?.sessionController?.workspaceRegistry;
|
|
465
|
+
let hostNote = '宿主 registry 不可用(无 resolveByPath):仅解绑账本行,宿主侧请在 GUI 核对';
|
|
466
|
+
if (reg && typeof reg.resolveByPath === 'function') {
|
|
467
|
+
try {
|
|
468
|
+
const hit = await reg.resolveByPath(canon);
|
|
469
|
+
if (!hit) {
|
|
470
|
+
hostNote = '宿主侧无该路径登记(无需动作)';
|
|
471
|
+
} else if (typeof reg.delete !== 'function') {
|
|
472
|
+
hostNote = '宿主登记保留:旧宿主无 delete API,请在宿主 GUI 手动处理';
|
|
473
|
+
} else if (Array.isArray(hit.sessionIds) && hit.sessionIds.length === 0) {
|
|
474
|
+
// v0.62.1:写前重读合并守卫包裹宿主 delete(解绑也是 registry 变更点;守卫事件用锁内在途 cur 直记,同下方 unbound 事件先例)
|
|
475
|
+
try { await guardedRegistryMutation({ workspaceRegistry: reg }, () => reg.delete(hit.id), { onEvent: (kind, note) => recordEvent(cur, kind, note) }); hostNote = `宿主登记同步删除(${hit.id},sessionIds 空)`; }
|
|
476
|
+
catch (e) { hostNote = `宿主登记删除软失败(${hit.id}:${e?.message ?? e})——请在宿主 GUI 手动删除`; }
|
|
477
|
+
} else {
|
|
478
|
+
hostNote = `宿主登记保守保留(${hit.id}:sessionIds 非空或形态未知);如需处理请在宿主 GUI 手动删除,全局默认工作区也请在 GUI 调整`;
|
|
479
|
+
}
|
|
480
|
+
} catch {
|
|
481
|
+
hostNote = '宿主侧探查失败(软失败,不影响账本解绑;可在宿主 GUI 核对)';
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
// 落账:删行+事件留痕(note 携被删行全量 JSON+宿主侧处置结果,审计可重建)
|
|
485
|
+
cur.workspaces.splice(idx, 1);
|
|
486
|
+
recordEvent(cur, 'bufeng.workspace.unbound', `工作区绑定已解绑:${canon} 行=${JSON.stringify(row)};${hostNote}`);
|
|
487
|
+
await saveLedger(cur);
|
|
488
|
+
return {
|
|
489
|
+
ok: true,
|
|
490
|
+
workspace: row,
|
|
491
|
+
count: cur.workspaces.length,
|
|
492
|
+
note: `已解绑陈旧绑定(${canon}):账本行已删,剩余绑定 ${cur.workspaces.length} 行。宿主侧:${hostNote}。下次启动巡检(reconcile)不再对该路径报 missing`
|
|
493
|
+
};
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
throw new Error(`未知 action:${action}(create / list / update / delete / unbind_workspace)`);
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
}
|