dsh-bufeng 0.0.0-stage → 0.64.0
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,365 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/workspace-rmw.js — 宿主 workspace.json 写前重读合并守卫(v0.62.1 lost-update 竞态根治叶子模块;
|
|
3
|
+
* v0.62.2 守卫补全:在途监读+收口后补偿,堵 S2 残余窗)。
|
|
4
|
+
* ADR:docs/决策/2026-11-03-v0.62.1-lost-update竞态根治.md(设计冻结,D1-D8;v0.62.2 补全节同文件)。
|
|
5
|
+
*
|
|
6
|
+
* 根因:宿主 registry 全量载内存、变更后按内存态全量回写不重读盘——外部写者(客户 apply / GUI)
|
|
7
|
+
* 在插件任务运行期间写入 workspace.json 的 archivedSessionIds 增量会被下一次回写冲掉(lost-update,
|
|
8
|
+
* 实锤 k-mvzys1bd02h:外部 +23(976→999)被收口回写冲回 976)。
|
|
9
|
+
*
|
|
10
|
+
* 守卫策略(对 archivedSessionIds 一律并集语义,只增不删,D6):
|
|
11
|
+
* merged = 可变形态(数组/Set 同引用)先重读盘面把缺失条目原位并入内存,再执行变更(D2);
|
|
12
|
+
* healed = 不可变形态(getter 副本/冻结)先快照变更前盘面→执行变更→对比变更后盘面,
|
|
13
|
+
* 逐枚补调 archiveSession(有界 50,超限 capped 如实披露:余量不自动重试(无 carry-over),外部写者按静止窗口重放纪律补齐)+收敛验证(D3);
|
|
14
|
+
* degraded = 盘不可读/registry 缺失/形状异常——不抛错,变更照常执行,guardDegraded 留痕(D4)。
|
|
15
|
+
* 残余窗口:无文件锁,毫秒级窗口只缩不消(D7);外部工具共存纪律=静止窗口(零在跑任务)重放。
|
|
16
|
+
*
|
|
17
|
+
* v0.62.2 守卫补全(S2 残余窗):v0.62.1 两臂只覆盖「守卫快照读之前已落盘」的增量(S1 早写窗)——
|
|
18
|
+
* 外部写者落在「守卫快照读之后/收口回写落盘之前」的在途窗时,不在任何写前快照里(merged 臂 added=0
|
|
19
|
+
* 静默放行、immutable 臂 before 快照同样不含),宿主按内存态全量回写将其冲掉(实锤形态:外部 apply
|
|
20
|
+
* 1015 枚→收口回写冲回 992、幸存 0/24)。补全=守卫变更窗内持续监读盘面(在途观测并集),
|
|
21
|
+
* 收口回写落盘后重读→lost 并集→逐枚补归档(复用 heal 原语),merged 臂补全后带 heal 结果返回。
|
|
22
|
+
*
|
|
23
|
+
* 依赖面(D5):只依赖 node 内建 + core 的 dshWorkspaceJsonPath 路径解析;对 workspace.json
|
|
24
|
+
* 零直接写(只读不变量,D1)。事件面(D8)四种经 opts.onEvent(kind, note) 产出,由调用方落账:
|
|
25
|
+
* bufeng.workspace.rehydrated / bufeng.workspace.healed / bufeng.workspace.guardDegraded / bufeng.workspace.guardDiverged
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { readFile } from 'node:fs/promises';
|
|
29
|
+
import { dshWorkspaceJsonPath } from './core.js';
|
|
30
|
+
|
|
31
|
+
/** 回补批量上限(对齐 core.js FAILED_ARCHIVE_BATCH_LIMIT=50 先例;叶子不反向 import core 常量,本地定义) */
|
|
32
|
+
const HEAL_BATCH_LIMIT = 50;
|
|
33
|
+
|
|
34
|
+
/** guardDegraded 同因节流窗(10 分钟,对齐 core.shouldRecordArchiveFail 先例):降级态(如盘文件不存在的新装宿主)
|
|
35
|
+
* 下逐次变更都落事件会把 EVENT_LIMIT=500 审计预算刷穿、驱逐老事件(m9 P4-Q7b 实证、core.js:351 同源教训)——
|
|
36
|
+
* 同原因窗内只落首条,suppressed 计数滚入下次实落 note。只节流 guardDegraded(唯一高频事件);
|
|
37
|
+
* rehydrated/healed/diverged 只在真实合并/回补发生时才产生,天然低频不节流。叶子自建节流器,
|
|
38
|
+
* 不 import core(守住 D5 依赖面=node 内建+dshWorkspaceJsonPath)。 */
|
|
39
|
+
const DEGRADE_THROTTLE_MS = 10 * 60 * 1000;
|
|
40
|
+
const degradeThrottle = new Map(); // reason → { lastFire: number, suppressed: number }
|
|
41
|
+
function shouldEmitDegraded(reason) {
|
|
42
|
+
const now = Date.now();
|
|
43
|
+
const rec = degradeThrottle.get(reason);
|
|
44
|
+
if (!rec || now - rec.lastFire >= DEGRADE_THROTTLE_MS) {
|
|
45
|
+
const suppressed = rec?.suppressed ?? 0;
|
|
46
|
+
degradeThrottle.set(reason, { lastFire: now, suppressed: 0 });
|
|
47
|
+
return { fire: true, suppressed };
|
|
48
|
+
}
|
|
49
|
+
rec.suppressed += 1;
|
|
50
|
+
return { fire: false, suppressed: rec.suppressed };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function emitDegraded(emit, reason, note) {
|
|
54
|
+
const dedupe = shouldEmitDegraded(reason);
|
|
55
|
+
if (!dedupe.fire) return;
|
|
56
|
+
const throttleNote = dedupe.suppressed > 0 ? `;前 ${dedupe.suppressed} 次同类降级已节流合并(10 分钟窗,防事件预算刷穿)` : '';
|
|
57
|
+
emit('bufeng.workspace.guardDegraded', `${note ?? `宿主 registry 守卫软降级(原因=${reason}):写前合并/回补未执行,本单变更照常执行,不阻断主流程(v0.62.1 lost-update 守卫)`}${throttleNote}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** ids 展示截断:超 8 枚只列前 8 枚 + 总数(防长清单刷爆事件 note) */
|
|
61
|
+
function idsPreview(ids, cap = 8) {
|
|
62
|
+
const list = Array.isArray(ids) ? ids : [];
|
|
63
|
+
if (list.length <= cap) return list.join('、') || '(无)';
|
|
64
|
+
return `${list.slice(0, cap).join('、')} 等 ${list.length} 枚`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function sizeOfIds(ids) {
|
|
68
|
+
return Array.isArray(ids) ? ids.length : (ids instanceof Set ? ids.size : 0);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function idsHave(ids, id) {
|
|
72
|
+
return Array.isArray(ids) ? ids.includes(id) : (ids instanceof Set ? ids.has(id) : false);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** ids 归一(字符串化+去重保序):在途监读/补偿基线的公共口径(与 readArchivedIdsSnapshot 同形) */
|
|
76
|
+
function normalizeIds(values) {
|
|
77
|
+
const out = [];
|
|
78
|
+
const seen = new Set();
|
|
79
|
+
for (const v of Array.isArray(values) ? values : []) {
|
|
80
|
+
const s = String(v ?? '');
|
|
81
|
+
if (seen.has(s)) continue;
|
|
82
|
+
seen.add(s);
|
|
83
|
+
out.push(s);
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 多源 ids 并集(保序:先到先得),用于「内存基线∪在途观测」补偿目标集 */
|
|
89
|
+
function unionIds(...lists) {
|
|
90
|
+
const out = [];
|
|
91
|
+
const seen = new Set();
|
|
92
|
+
for (const list of lists) {
|
|
93
|
+
for (const v of Array.isArray(list) ? list : []) {
|
|
94
|
+
const s = String(v ?? '');
|
|
95
|
+
if (seen.has(s)) continue;
|
|
96
|
+
seen.add(s);
|
|
97
|
+
out.push(s);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** 宿主内存态 archivedSessionIds 冻结副本(merged 臂守卫窗口基线;活数组/Set 归一为字符串数组) */
|
|
104
|
+
function liveIdsCopy(reg) {
|
|
105
|
+
const mem = reg?.archivedSessionIds;
|
|
106
|
+
if (Array.isArray(mem)) return normalizeIds(mem);
|
|
107
|
+
if (mem instanceof Set) return normalizeIds([...mem]);
|
|
108
|
+
return [];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
112
|
+
const yieldBeat = () => new Promise((resolve) => setImmediate(resolve));
|
|
113
|
+
|
|
114
|
+
/** 在途监读限速参数:全速拍数上限 + 超限后拍间隔(异常长变更窗兜底,防监读自身长期占事件循环) */
|
|
115
|
+
const WATCH_FULL_RATE_BEATS = 2000;
|
|
116
|
+
const WATCH_THROTTLED_INTERVAL_MS = 5;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 在途监读(v0.62.2 守卫补全,S2 残余窗补偿的观测原语):守卫变更窗(守卫入口→mutateFn→收口回写落盘)
|
|
120
|
+
* 内以事件循环间隙节拍持续读盘快照,累积 archivedSessionIds 观测并集(软失败拍跳过,绝不抛错)。
|
|
121
|
+
* 观测并集∪写前基线=补偿目标集,收口回写落盘后经 heal 逐枚补归档。
|
|
122
|
+
* 停转语义:stop() 幂等(重复调用返回已观测并集副本),停转前不丢弃未完成拍,终读一拍吃住最后落盘;
|
|
123
|
+
* 无文件系统句柄(纯 readFile 轮询),节拍有界(全速 WATCH_FULL_RATE_BEATS 拍后转 WATCH_THROTTLED_INTERVAL_MS 限速)。
|
|
124
|
+
* @returns {{ stop: () => Promise<string[]> }}
|
|
125
|
+
*/
|
|
126
|
+
function startInflightArchivedWatch() {
|
|
127
|
+
const observed = [];
|
|
128
|
+
const seen = new Set();
|
|
129
|
+
let stopped = false;
|
|
130
|
+
let spinning = true;
|
|
131
|
+
let beats = 0;
|
|
132
|
+
const absorb = (snap) => {
|
|
133
|
+
if (!snap?.ok) return;
|
|
134
|
+
for (const id of snap.ids) {
|
|
135
|
+
if (seen.has(id)) continue;
|
|
136
|
+
seen.add(id);
|
|
137
|
+
observed.push(id);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const spin = (async () => {
|
|
141
|
+
while (spinning) {
|
|
142
|
+
if (beats >= WATCH_FULL_RATE_BEATS) await sleep(WATCH_THROTTLED_INTERVAL_MS); // 长窗限速兜底
|
|
143
|
+
beats += 1;
|
|
144
|
+
absorb(await readArchivedIdsSnapshot());
|
|
145
|
+
await yieldBeat();
|
|
146
|
+
}
|
|
147
|
+
})().catch(() => { /* 监读失败不反噬守卫主流程(观测是尽力而为) */ });
|
|
148
|
+
const stop = async () => {
|
|
149
|
+
if (stopped) return observed.slice();
|
|
150
|
+
spinning = false;
|
|
151
|
+
stopped = true;
|
|
152
|
+
try { await spin; } catch { /* 同上 */ }
|
|
153
|
+
absorb(await readArchivedIdsSnapshot()); // 终读:停转后补一拍,吃住最后落盘状态
|
|
154
|
+
return observed.slice();
|
|
155
|
+
};
|
|
156
|
+
return { stop };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 读盘快照:按 dshWorkspaceJsonPath() 解析 workspace.json,取 global.archivedSessionIds
|
|
161
|
+
* (字符串化、去重、保序)。缺文件/坏 JSON/字段缺席/形状异常 → ok:false(软,绝不抛错)。
|
|
162
|
+
* @returns {Promise<{ok:true, ids:string[]}|{ok:false, reason:string}>}
|
|
163
|
+
*/
|
|
164
|
+
export async function readArchivedIdsSnapshot() {
|
|
165
|
+
try {
|
|
166
|
+
let raw;
|
|
167
|
+
try {
|
|
168
|
+
raw = await readFile(dshWorkspaceJsonPath(), 'utf8');
|
|
169
|
+
} catch (e) {
|
|
170
|
+
return { ok: false, reason: `read-failed:${e?.code ?? e?.message ?? 'unknown'}` };
|
|
171
|
+
}
|
|
172
|
+
let json;
|
|
173
|
+
try {
|
|
174
|
+
json = JSON.parse(raw);
|
|
175
|
+
} catch {
|
|
176
|
+
return { ok: false, reason: 'json-parse-failed' };
|
|
177
|
+
}
|
|
178
|
+
const v = json?.global?.archivedSessionIds;
|
|
179
|
+
if (!Array.isArray(v)) return { ok: false, reason: 'field-absent-or-shape' };
|
|
180
|
+
const ids = [];
|
|
181
|
+
const seen = new Set();
|
|
182
|
+
for (const item of v) {
|
|
183
|
+
const s = String(item ?? '');
|
|
184
|
+
if (seen.has(s)) continue;
|
|
185
|
+
seen.add(s);
|
|
186
|
+
ids.push(s);
|
|
187
|
+
}
|
|
188
|
+
return { ok: true, ids };
|
|
189
|
+
} catch (e) {
|
|
190
|
+
return { ok: false, reason: `unexpected:${String(e?.message ?? e).slice(0, 120)}` };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 写前重读合并(rehydrate-before-mutate,D2):
|
|
196
|
+
* 判定序:registry 缺失 / archivedSessionIds 形态异常(非 Array 非 Set)→ degraded;
|
|
197
|
+
* 盘面快照失败 → degraded;双读引用互异(getter 副本)→ immutable(不做无效写入);
|
|
198
|
+
* 可变形态原位并入缺失条目 + 重读验证 → merged(验证不过 → immutable)。
|
|
199
|
+
* 只读盘零写盘;并入只增不删(并集语义,D6);绝不抛错(软降级,D4)。
|
|
200
|
+
* @returns {Promise<{mode:'merged'|'immutable'|'degraded', added:string[], diskCount:number, memCount:number, reason?:string}>}
|
|
201
|
+
*/
|
|
202
|
+
export async function rehydrateArchivedIds(ctx) {
|
|
203
|
+
const degraded = (reason) => ({ mode: 'degraded', added: [], diskCount: 0, memCount: 0, reason });
|
|
204
|
+
try {
|
|
205
|
+
const reg = ctx?.workspaceRegistry;
|
|
206
|
+
if (!reg) return degraded('registry-missing');
|
|
207
|
+
const mem = reg.archivedSessionIds;
|
|
208
|
+
if (!(Array.isArray(mem) || mem instanceof Set)) return degraded('archived-ids-shape');
|
|
209
|
+
const snap = await readArchivedIdsSnapshot();
|
|
210
|
+
if (!snap.ok) return degraded(`snapshot:${snap.reason}`);
|
|
211
|
+
const r1 = reg.archivedSessionIds;
|
|
212
|
+
const r2 = reg.archivedSessionIds;
|
|
213
|
+
const diskCount = snap.ids.length;
|
|
214
|
+
const memCount = sizeOfIds(mem);
|
|
215
|
+
const immutable = (reason) => ({ mode: 'immutable', added: [], diskCount, memCount, reason });
|
|
216
|
+
if (r1 !== r2) return immutable('getter-copy');
|
|
217
|
+
const missing = snap.ids.filter((id) => !idsHave(mem, id));
|
|
218
|
+
if (missing.length === 0) {
|
|
219
|
+
return { mode: 'merged', added: [], diskCount, memCount };
|
|
220
|
+
}
|
|
221
|
+
try {
|
|
222
|
+
for (const id of missing) {
|
|
223
|
+
if (Array.isArray(mem)) mem.push(id);
|
|
224
|
+
else mem.add(id);
|
|
225
|
+
}
|
|
226
|
+
} catch {
|
|
227
|
+
return immutable('mutation-failed'); // 冻结数组等严格模式抛错:判不可变,走 heal 降级
|
|
228
|
+
}
|
|
229
|
+
const fresh = reg.archivedSessionIds; // 重读验证(确认宿主真吃到并入)
|
|
230
|
+
const verified = missing.every((id) => idsHave(fresh, id));
|
|
231
|
+
if (!verified) return immutable('verify-failed');
|
|
232
|
+
return { mode: 'merged', added: missing, diskCount, memCount: sizeOfIds(fresh) };
|
|
233
|
+
} catch (e) {
|
|
234
|
+
return degraded(`unexpected:${String(e?.message ?? e).slice(0, 120)}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 写后对账回补(heal,D3):lost=beforeIds−变更后盘面(保序全量);逐枚补调
|
|
240
|
+
* archiveSession(id,{stopActivity:true})(单枚抛错记 failed 不中断);批量上限
|
|
241
|
+
* HEAL_BATCH_LIMIT(可经 opts.healBatchLimit 覆盖),超限 capped=true 余量不自动重试(无 carry-over),外部写者按静止窗口重放纪律补齐;
|
|
242
|
+
* 补后重读盘面定 converged(beforeIds 全部在场=收敛)。绝不抛错。
|
|
243
|
+
* @returns {Promise<{lost:string[], healed:string[], failed:string[], capped:boolean, converged:boolean, reason?:string}>}
|
|
244
|
+
*/
|
|
245
|
+
export async function healLostArchivedIds(ctx, beforeIds, opts = {}) {
|
|
246
|
+
const base = { lost: [], healed: [], failed: [], capped: false, converged: false };
|
|
247
|
+
try {
|
|
248
|
+
const before = Array.isArray(beforeIds) ? beforeIds : [];
|
|
249
|
+
const reg = ctx?.workspaceRegistry;
|
|
250
|
+
const afterSnap = await readArchivedIdsSnapshot();
|
|
251
|
+
if (!reg || typeof reg.archiveSession !== 'function') {
|
|
252
|
+
return { ...base, reason: 'registry-or-archive-missing' };
|
|
253
|
+
}
|
|
254
|
+
if (!afterSnap.ok) {
|
|
255
|
+
return { ...base, reason: `snapshot:${afterSnap.reason}` };
|
|
256
|
+
}
|
|
257
|
+
const after = afterSnap.ids;
|
|
258
|
+
const lost = before.filter((id) => !after.includes(id));
|
|
259
|
+
const limit = Number.isInteger(opts.healBatchLimit) && opts.healBatchLimit > 0 ? opts.healBatchLimit : HEAL_BATCH_LIMIT;
|
|
260
|
+
const capped = lost.length > limit;
|
|
261
|
+
const targets = capped ? lost.slice(0, limit) : lost;
|
|
262
|
+
const healed = [];
|
|
263
|
+
const failed = [];
|
|
264
|
+
for (const id of targets) {
|
|
265
|
+
try {
|
|
266
|
+
await reg.archiveSession(id, { stopActivity: true });
|
|
267
|
+
healed.push(id);
|
|
268
|
+
} catch {
|
|
269
|
+
failed.push(id); // 单枚失败不中断(D3)
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const verifySnap = await readArchivedIdsSnapshot();
|
|
273
|
+
const converged = verifySnap.ok ? before.every((id) => verifySnap.ids.includes(id)) : false;
|
|
274
|
+
return { lost, healed, failed, capped, converged };
|
|
275
|
+
} catch (e) {
|
|
276
|
+
return { ...base, reason: `unexpected:${String(e?.message ?? e).slice(0, 120)}` };
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* 守卫变更入口(guarded mutation,ADR §4):把「对宿主 registry 的一次变更」包进写前合并/写后回补守卫。
|
|
282
|
+
* merged = 先合并后变更(rehydrate.mode=merged 且 added>0 → rehydrated 事件);
|
|
283
|
+
* v0.62.2 补全:变更窗内持续在途监读盘面,收口回写落盘后重读→lost 并集→逐枚补归档
|
|
284
|
+
* (healed>0 → healed 事件;未收敛 → guardDiverged 事件恰 1 条)——堵 S2 残余窗
|
|
285
|
+
* (外部增量落在守卫快照读之后/收口回写落盘之前,不在任何写前快照里,v0.62.1 静默冲丢)。
|
|
286
|
+
* 无在途丢失(lost=∅)时返回形状与 v0.62.1 全等(不带 heal 键,健康路径零字节漂移);
|
|
287
|
+
* 收口后复核快照读取失败 → guardDegraded 如实披露(沿 10 分钟同因节流;零补偿语义与返回形状零变化,返工 P2-1)。
|
|
288
|
+
* healed = 内存态不可变——先快照变更前盘面→变更→对账回补(healed>0 → healed 事件;
|
|
289
|
+
* 未收敛 → guardDiverged 事件恰 1 条,绝不循环重试);v0.62.2 起回补目标集=
|
|
290
|
+
* before 快照∪在途观测并集(覆盖「before 快照之后落盘」的在途增量)。
|
|
291
|
+
* degraded = 快照失败/registry 缺失/形态异常——变更照常执行 + guardDegraded 事件留痕。
|
|
292
|
+
* mutateFn 抛错原样重抛(不叠加回补写——变更没成功就无丢失可对账;在途监读照常停转);
|
|
293
|
+
* onEvent 自身异常吞掉(观测不反噬)。
|
|
294
|
+
* @param {object} ctx 携 workspaceRegistry 的宿主上下文
|
|
295
|
+
* @param {() => Promise<any>} mutateFn 变更闭包(如 () => registry.archiveSession(id, {stopActivity:true}))
|
|
296
|
+
* @param {{onEvent?: (kind: string, note: string) => void, healBatchLimit?: number}} [opts]
|
|
297
|
+
* @returns {Promise<{guard:'merged'|'healed'|'degraded', result:any, rehydrate?:object, heal?:object}>}
|
|
298
|
+
*/
|
|
299
|
+
export async function guardedRegistryMutation(ctx, mutateFn, opts = {}) {
|
|
300
|
+
const onEvent = typeof opts.onEvent === 'function' ? opts.onEvent : () => {};
|
|
301
|
+
const emit = (kind, note) => { try { onEvent(kind, note); } catch { /* 观测事件不反噬主流程 */ } };
|
|
302
|
+
// v0.62.2 在途监读:覆盖守卫入口→收口回写落盘的整个变更窗(含 rehydrate 快照读之后的在途增量)
|
|
303
|
+
const watch = startInflightArchivedWatch();
|
|
304
|
+
let observed = [];
|
|
305
|
+
const runMutate = async () => {
|
|
306
|
+
try {
|
|
307
|
+
return await mutateFn();
|
|
308
|
+
} finally {
|
|
309
|
+
observed = await watch.stop(); // 抛错路径也停转(只停转不补偿:变更没成功就无丢失可对账)
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
try {
|
|
313
|
+
const rehydrate = await rehydrateArchivedIds(ctx);
|
|
314
|
+
if (rehydrate.mode === 'merged') {
|
|
315
|
+
const memAtMutation = liveIdsCopy(ctx?.workspaceRegistry); // 守卫窗口内存基线(含 rehydrate 已并入条目)
|
|
316
|
+
const result = await runMutate();
|
|
317
|
+
// v0.62.2 S2 残余窗补偿:收口回写落盘后重读盘面,lost=(内存基线∪在途观测)−盘面,逐枚补归档
|
|
318
|
+
if (rehydrate.added.length > 0) {
|
|
319
|
+
emit('bufeng.workspace.rehydrated', `宿主 registry 写前重读合并(rehydrate-before-mutate,v0.62.1 lost-update 守卫):盘面 archivedSessionIds ${rehydrate.diskCount} 枚,内存缺失 ${rehydrate.added.length} 枚已原位并入(${idsPreview(rehydrate.added)})——外部写者增量将随本次宿主回写一并保留`);
|
|
320
|
+
}
|
|
321
|
+
const afterSnap = await readArchivedIdsSnapshot();
|
|
322
|
+
const baseline = unionIds(memAtMutation, observed);
|
|
323
|
+
const lost = afterSnap.ok ? baseline.filter((id) => !afterSnap.ids.includes(id)) : [];
|
|
324
|
+
let heal = null;
|
|
325
|
+
if (!afterSnap.ok) {
|
|
326
|
+
// v0.62.2 P2-1(返工批 k-mw0jtr0s0g2):收口后复核快照读取失败原先静默 lost=∅(与「零丢失」不可区分)——
|
|
327
|
+
// 改发 guardDegraded 如实披露(沿 10 分钟同因节流);零补偿语义与返回形状零变化。
|
|
328
|
+
emitDegraded(emit, `post-mutate-snapshot:${afterSnap.reason}`, `宿主 registry 守卫降级(原因=post-mutate-snapshot:${afterSnap.reason}):收口回写落盘后的复核快照读取失败,在途丢失对账未执行(零补偿),本单变更已照常完成,不阻断主流程(v0.62.2 守卫补全)`);
|
|
329
|
+
} else if (lost.length > 0) {
|
|
330
|
+
heal = await healLostArchivedIds(ctx, baseline, opts);
|
|
331
|
+
if (heal.healed.length > 0) {
|
|
332
|
+
emit('bufeng.workspace.healed', `宿主 registry 收口后补偿回补(merged 臂在途窗补偿,v0.62.2 守卫补全):收口回写落盘前在途新增 ${heal.lost.length} 枚被宿主按内存态全量回写冲掉,已逐枚补归档 ${heal.healed.length} 枚(${idsPreview(heal.healed)}${heal.failed.length ? `;失败 ${heal.failed.length} 枚:${idsPreview(heal.failed)}` : ''})`);
|
|
333
|
+
}
|
|
334
|
+
if (heal.converged === false) {
|
|
335
|
+
emit('bufeng.workspace.guardDiverged', `宿主 registry 收口后补偿未收敛(diverged):丢失 ${heal.lost.length} 枚/补归档 ${heal.healed.length} 枚/失败 ${heal.failed.length} 枚/capped=${heal.capped}${heal.reason ? `;原因=${heal.reason}` : ''};守卫有界不循环,余量不自动重试(无 carry-over),外部写者按静止窗口重放纪律补齐(v0.62.2 守卫补全)`);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return heal ? { guard: 'merged', result, rehydrate, heal } : { guard: 'merged', result, rehydrate };
|
|
339
|
+
}
|
|
340
|
+
if (rehydrate.mode === 'immutable') {
|
|
341
|
+
const before = await readArchivedIdsSnapshot();
|
|
342
|
+
if (!before.ok) {
|
|
343
|
+
const result = await runMutate();
|
|
344
|
+
emitDegraded(emit, `snapshot:${before.reason}`);
|
|
345
|
+
return { guard: 'degraded', result };
|
|
346
|
+
}
|
|
347
|
+
const result = await runMutate();
|
|
348
|
+
const windowIds = unionIds(before.ids, observed); // v0.62.2:before 快照∪在途观测(堵 before 快照后的在途窗)
|
|
349
|
+
const heal = await healLostArchivedIds(ctx, windowIds, opts);
|
|
350
|
+
if (heal.healed.length > 0) {
|
|
351
|
+
emit('bufeng.workspace.healed', `宿主 registry 写后对账回补(heal,内存态不可变降级):回写丢失 ${heal.lost.length} 枚,已逐枚补归档 ${heal.healed.length} 枚(${idsPreview(heal.healed)}${heal.failed.length ? `;失败 ${heal.failed.length} 枚:${idsPreview(heal.failed)}` : ''})`);
|
|
352
|
+
}
|
|
353
|
+
if (heal.converged === false) {
|
|
354
|
+
emit('bufeng.workspace.guardDiverged', `宿主 registry 回补后未收敛(diverged):丢失 ${heal.lost.length} 枚/补归档 ${heal.healed.length} 枚/失败 ${heal.failed.length} 枚/capped=${heal.capped}${heal.reason ? `;原因=${heal.reason}` : ''};守卫有界不循环,余量不自动重试(无 carry-over),外部写者按静止窗口重放纪律补齐(v0.62.1 lost-update 守卫)`);
|
|
355
|
+
}
|
|
356
|
+
return { guard: 'healed', result, heal };
|
|
357
|
+
}
|
|
358
|
+
// degraded:软降级——变更照常执行(D4),留痕不阻断(同因 10 分钟窗节流,防事件预算刷穿)
|
|
359
|
+
const result = await runMutate();
|
|
360
|
+
emitDegraded(emit, rehydrate.reason ?? 'unknown');
|
|
361
|
+
return { guard: 'degraded', result };
|
|
362
|
+
} finally {
|
|
363
|
+
await watch.stop(); // 双保险(幂等停转;正常路径已在 runMutate 内停转)
|
|
364
|
+
}
|
|
365
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-bufeng",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.64.0",
|
|
4
|
+
"description": "捕风工坊 · 无限扩展智能体工作台 —— DSH 桌面插件(原名林台;20 工具 + 项目制 + 司南觉醒与入口注入 + 原生会话通道 + 三轴装备(preset/工具/技能)+ 三级编排治理 + 侧栏看板)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/index.js",
|
|
9
|
+
"./client": "./client/client.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"lib",
|
|
13
|
+
"client",
|
|
14
|
+
"bufeng.patch.yml",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"keywords": [
|
|
19
|
+
"dsh",
|
|
20
|
+
"dsh-plugin",
|
|
21
|
+
"bufeng",
|
|
22
|
+
"agent",
|
|
23
|
+
"multi-agent",
|
|
24
|
+
"orchestration"
|
|
25
|
+
],
|
|
26
|
+
"author": "jiawenkai",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://gitee.com/jiawenkai/dsh-bufeng.git"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=20"
|
|
34
|
+
},
|
|
35
|
+
"dsh": {
|
|
36
|
+
"bundle": {
|
|
37
|
+
"patch": "./bufeng.patch.yml"
|
|
38
|
+
},
|
|
39
|
+
"client": {
|
|
40
|
+
"platform": "web",
|
|
41
|
+
"inject": []
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|