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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +68 -2
  3. package/bufeng.patch.yml +3 -0
  4. package/client/build-client.mjs +159 -0
  5. package/client/client.js +4038 -0
  6. package/client/src/board.js +133 -0
  7. package/client/src/capabilities.js +787 -0
  8. package/client/src/cards.js +263 -0
  9. package/client/src/data.js +43 -0
  10. package/client/src/graph.js +558 -0
  11. package/client/src/icons.js +88 -0
  12. package/client/src/projects.js +467 -0
  13. package/client/src/prompt.js +55 -0
  14. package/client/src/react.js +7 -0
  15. package/client/src/settings.js +407 -0
  16. package/client/src/styles.js +611 -0
  17. package/client/src/taskboard.js +321 -0
  18. package/client/src/taskgraph.js +145 -0
  19. package/client/src/ui.js +72 -0
  20. package/lib/acceptance-subject.js +71 -0
  21. package/lib/board/server.js +546 -0
  22. package/lib/board/snapshot.js +133 -0
  23. package/lib/board/taskview.js +151 -0
  24. package/lib/bootstrap.js +77 -0
  25. package/lib/catalog.js +385 -0
  26. package/lib/core.js +478 -0
  27. package/lib/data/capability-alias-catalog.json +301 -0
  28. package/lib/data/capability-alias.json +25 -0
  29. package/lib/domain.js +1342 -0
  30. package/lib/host-probe.js +176 -0
  31. package/lib/identity-hook.js +490 -0
  32. package/lib/index.js +119 -0
  33. package/lib/lessons.js +134 -0
  34. package/lib/metering.js +109 -0
  35. package/lib/scope.js +389 -0
  36. package/lib/session/child-watch.js +86 -0
  37. package/lib/session/evidence.js +118 -0
  38. package/lib/session/node-session.js +335 -0
  39. package/lib/session/queue.js +307 -0
  40. package/lib/session/reconcile.js +500 -0
  41. package/lib/tools/capabilities.js +1461 -0
  42. package/lib/tools/governance.js +579 -0
  43. package/lib/tools/index.js +33 -0
  44. package/lib/tools/project.js +500 -0
  45. package/lib/tools/readonly.js +566 -0
  46. package/lib/tools/recover.js +402 -0
  47. package/lib/tools/run.js +1609 -0
  48. package/lib/tools/skills.js +162 -0
  49. package/lib/tools/trees.js +637 -0
  50. package/lib/workspace-rmw.js +365 -0
  51. package/package.json +42 -4
@@ -0,0 +1,176 @@
1
+ /**
2
+ * lib/host-probe.js — 宿主版本感知(T2:boot 期,只提醒不自动改)。
3
+ * 叶子模块:只依赖 node 内建 + ./core.js;不 import tools/board/session(§2 依赖方向)。
4
+ * 行为契约(冻结,T3 测试据此编写):
5
+ * · HOST_EVENT 三事件 kind:baseline / changed / error(见常量)。
6
+ * · readHostProbe:读 <resources>/runtime/primary-runtime/runtime.json 的 desktopVersion
7
+ * + app.asar stat(sizeBytes/mtimeUtc);sha256 仅显式 withHash=true 才算(boot 期轻量红线:
8
+ * 禁每次启动扫 182MB 算哈希)。失败抛错,由调用方捕获。
9
+ * · decideHostVersion:纯函数三态判定(baseline/changed/same),不改入参。
10
+ * · checkHostVersion:全包 try/catch——任何异常→action='error'+error 事件(note 截断),
11
+ * 绝不 throw、不写文件(账本由调用方落盘)、不联网、不 spawn;同版本零事件零 meta 变更。
12
+ * meta(账本惰性字段,首写即建):{ lastHostVersion, lastHostAsar:{sizeBytes,mtimeUtc,sha256}|null,
13
+ * firstSeenAt, lastHostChangedAt:string|null }——版本变化才更新 lastHostVersion,避免每次启动重复刷。
14
+ */
15
+ import path from 'node:path';
16
+ import fs from 'node:fs';
17
+ import crypto from 'node:crypto';
18
+ import { recordEvent } from './core.js';
19
+
20
+ export const HOST_EVENT = {
21
+ baseline: 'bufeng.host.versionBaseline',
22
+ changed: 'bufeng.host.versionChanged',
23
+ error: 'bufeng.host.versionProbeError'
24
+ };
25
+
26
+ /** resources 目录 = app.asar 所在目录(asar 同层);入参无效返回 null(不抛错)。 */
27
+ export function hostResourcesDir(asarPath) {
28
+ if (typeof asarPath !== 'string' || !asarPath) return null;
29
+ return path.dirname(asarPath);
30
+ }
31
+
32
+ /** 默认 asar 路径推导:env.DSH_HOST_ASAR 优先,否则 LOCALAPPDATA 下标准安装位;无法推导返回 null。 */
33
+ export function defaultHostAsarPath(env = process.env) {
34
+ if (!env || typeof env !== 'object') return null;
35
+ if (typeof env.DSH_HOST_ASAR === 'string' && env.DSH_HOST_ASAR) return env.DSH_HOST_ASAR;
36
+ if (typeof env.LOCALAPPDATA === 'string' && env.LOCALAPPDATA) {
37
+ return path.join(env.LOCALAPPDATA, 'Programs', 'DeepSeek Harness', 'resources', 'app.asar');
38
+ }
39
+ return null;
40
+ }
41
+
42
+ function sha256File(filePath) {
43
+ return new Promise((resolve, reject) => {
44
+ const hash = crypto.createHash('sha256');
45
+ const stream = fs.createReadStream(filePath);
46
+ stream.on('error', reject);
47
+ stream.on('data', (chunk) => hash.update(chunk));
48
+ stream.on('end', () => resolve(hash.digest('hex')));
49
+ });
50
+ }
51
+
52
+ /**
53
+ * 探测宿主当前版本:读 runtime.json 的 desktopVersion + app.asar stat。
54
+ * 失败抛错(调用方捕获转 error 事件);sha256 仅 withHash=true 时计算(boot 期默认 false)。
55
+ */
56
+ export async function readHostProbe({ asarPath = defaultHostAsarPath(), withHash = false } = {}) {
57
+ const resolved = typeof asarPath === 'string' && asarPath ? asarPath : defaultHostAsarPath();
58
+ if (!resolved) throw new Error('宿主 asar 路径不可推导(未设置 DSH_HOST_ASAR 且无 LOCALAPPDATA)');
59
+ const runtimeJsonPath = path.join(path.dirname(resolved), 'runtime', 'primary-runtime', 'runtime.json');
60
+ let raw;
61
+ try {
62
+ raw = await fs.promises.readFile(runtimeJsonPath, 'utf8');
63
+ } catch (err) {
64
+ throw new Error('runtime.json 读取失败(' + runtimeJsonPath + '):' + (err && err.code ? err.code : (err && err.message ? err.message : String(err))));
65
+ }
66
+ let runtime = null;
67
+ try {
68
+ runtime = JSON.parse(raw);
69
+ } catch (err) {
70
+ throw new Error('runtime.json 解析失败(' + runtimeJsonPath + '):' + (err && err.message ? err.message : String(err)));
71
+ }
72
+ const desktopVersion = runtime && typeof runtime.desktopVersion === 'string' ? runtime.desktopVersion : null;
73
+ if (!desktopVersion) throw new Error('runtime.json 缺 desktopVersion 字段(' + runtimeJsonPath + ')');
74
+ let stat;
75
+ try {
76
+ stat = await fs.promises.stat(resolved);
77
+ } catch (err) {
78
+ throw new Error('宿主 asar stat 失败(' + resolved + '):' + (err && err.code ? err.code : (err && err.message ? err.message : String(err))));
79
+ }
80
+ const sha256 = withHash ? await sha256File(resolved) : null;
81
+ return {
82
+ desktopVersion,
83
+ asarPath: resolved,
84
+ runtimeJsonPath,
85
+ sizeBytes: stat.size,
86
+ mtimeUtc: stat.mtime.toISOString(),
87
+ sha256
88
+ };
89
+ }
90
+
91
+ function asarBrief(current) {
92
+ const cur = current && typeof current === 'object' ? current : null;
93
+ const size = cur && typeof cur.sizeBytes === 'number' ? cur.sizeBytes : null;
94
+ const mtime = cur && typeof cur.mtimeUtc === 'string' ? cur.mtimeUtc : null;
95
+ return 'asar sizeBytes=' + size + ', mtimeUtc=' + mtime;
96
+ }
97
+
98
+ /** 三态判定(纯函数,不改入参):无基线→baseline;版本不同→changed(note 含老基线文档提示);相同→same 零事件。 */
99
+ export function decideHostVersion({ lastHostVersion, current } = {}) {
100
+ const cur = current && typeof current === 'object' && !Array.isArray(current) ? current : null;
101
+ const version = cur && typeof cur.desktopVersion === 'string' && cur.desktopVersion ? cur.desktopVersion : '';
102
+ const last = typeof lastHostVersion === 'string' && lastHostVersion ? lastHostVersion : '';
103
+ if (!last) {
104
+ return {
105
+ action: 'baseline',
106
+ eventKind: HOST_EVENT.baseline,
107
+ note: '首次登记宿主版本基线:' + (version || '(未知版本)') + '(' + asarBrief(cur) + ')'
108
+ };
109
+ }
110
+ if (last !== version) {
111
+ const shaPart = cur && typeof cur.sha256 === 'string' && cur.sha256
112
+ ? 'sha256 前 12 位 ' + cur.sha256.slice(0, 12)
113
+ : 'sha256 未计算(boot 期轻量)';
114
+ return {
115
+ action: 'changed',
116
+ eventKind: HOST_EVENT.changed,
117
+ note: '宿主版本变化:' + last + ' → ' + (version || '(未知版本)') + '(' + asarBrief(cur) + ';' + shaPart
118
+ + ');老基线对照:docs/host-poc/injection-report.md(「当前基线」行)与 docs/host-poc/monitoring.md'
119
+ };
120
+ }
121
+ return { action: 'same', eventKind: null, note: '' };
122
+ }
123
+
124
+ function truncateMsg(text, limit = 300) {
125
+ const s = text == null ? '' : String(text);
126
+ return s.length > limit ? s.slice(0, limit) + '…' : s;
127
+ }
128
+
129
+ function asarFingerprint(probeResult) {
130
+ return {
131
+ sizeBytes: probeResult && typeof probeResult.sizeBytes === 'number' ? probeResult.sizeBytes : null,
132
+ mtimeUtc: probeResult && typeof probeResult.mtimeUtc === 'string' ? probeResult.mtimeUtc : null,
133
+ sha256: probeResult && typeof probeResult.sha256 === 'string' ? probeResult.sha256 : null
134
+ };
135
+ }
136
+
137
+ /**
138
+ * boot 期宿主版本感知主入口:探测→比对→(baseline/changed 时)更新 meta+落一条事件。
139
+ * 全包 try/catch:任何异常→action='error'+HOST_EVENT.error 事件(note 含原因并截断);
140
+ * 绝不 throw、不写文件(只改内存账本,由调用方 saveLedger 落盘)、不联网、不 spawn。
141
+ * 同版本(same):零事件零 note 零 meta 变更。探测失败:meta 保持原样(不覆盖 lastHostVersion)。
142
+ */
143
+ export async function checkHostVersion({ ledger, probe = readHostProbe, logger, now = () => new Date(), withHash = false } = {}) {
144
+ try {
145
+ if (!ledger || typeof ledger !== 'object' || Array.isArray(ledger)) throw new Error('账本对象不可用');
146
+ if (!Array.isArray(ledger.events)) throw new Error('账本 events 不可用');
147
+ const probeResult = await probe({ withHash });
148
+ if (!probeResult || typeof probeResult !== 'object' || Array.isArray(probeResult)) throw new Error('宿主探测结果不可用');
149
+ const version = typeof probeResult.desktopVersion === 'string' && probeResult.desktopVersion ? probeResult.desktopVersion : null;
150
+ if (!version) throw new Error('宿主探测结果缺 desktopVersion');
151
+ const meta = ledger.meta && typeof ledger.meta === 'object' && !Array.isArray(ledger.meta) ? ledger.meta : null;
152
+ const lastHostVersion = meta && typeof meta.lastHostVersion === 'string' && meta.lastHostVersion ? meta.lastHostVersion : null;
153
+ const decision = decideHostVersion({ lastHostVersion, current: probeResult });
154
+ if (decision.action === 'same') {
155
+ return { action: 'same', eventKind: null, note: decision.note, from: lastHostVersion, to: version, probe: probeResult };
156
+ }
157
+ const nowIso = (typeof now === 'function' ? now() : new Date()).toISOString();
158
+ if (!ledger.meta || typeof ledger.meta !== 'object' || Array.isArray(ledger.meta)) ledger.meta = {};
159
+ ledger.meta.lastHostVersion = version;
160
+ ledger.meta.lastHostAsar = asarFingerprint(probeResult);
161
+ if (!ledger.meta.firstSeenAt) ledger.meta.firstSeenAt = nowIso;
162
+ if (decision.action === 'changed') ledger.meta.lastHostChangedAt = nowIso;
163
+ else if (ledger.meta.lastHostChangedAt === undefined) ledger.meta.lastHostChangedAt = null;
164
+ recordEvent(ledger, decision.eventKind, decision.note);
165
+ return { action: decision.action, eventKind: decision.eventKind, note: decision.note, from: lastHostVersion, to: version, probe: probeResult };
166
+ } catch (err) {
167
+ const note = '宿主版本探测失败:' + truncateMsg(err && err.message ? err.message : String(err));
168
+ try {
169
+ recordEvent(ledger, HOST_EVENT.error, note);
170
+ } catch {
171
+ // 账本本身不可用时无法落事件:静默,绝不 throw
172
+ }
173
+ return { action: 'error', eventKind: HOST_EVENT.error, note, from: null, to: null, probe: null };
174
+ }
175
+ }
176
+