@modusensus/dsh-mneme 0.8.16 → 0.9.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.
Files changed (135) hide show
  1. package/README.md +91 -16
  2. package/assets/icon.svg +2 -2
  3. package/bin/cli.mjs +8 -2
  4. package/docs/CONFIGURATION.md +27 -1
  5. package/docs/CONTINUITY.md +18 -3
  6. package/docs/SEMANTIC.md +3 -3
  7. package/docs/WORKFLOWS.md +299 -0
  8. package/lib/api-standalone.js +78 -12
  9. package/lib/api.js +403 -62
  10. package/lib/candidates.js +454 -0
  11. package/lib/capabilities.js +819 -0
  12. package/lib/client.js +2294 -423
  13. package/lib/config.js +63 -6
  14. package/lib/continuity.js +35 -6
  15. package/lib/dream/decisions.js +17 -1
  16. package/lib/dream/sleep.js +10 -2
  17. package/lib/dream.js +5 -1
  18. package/lib/embedding.js +116 -15
  19. package/lib/entities/extractor.js +4 -0
  20. package/lib/index.js +81 -12
  21. package/lib/inject.js +40 -4
  22. package/lib/lang.js +124 -0
  23. package/lib/pipeline-activities.js +178 -0
  24. package/lib/pipeline-attribution.js +165 -0
  25. package/lib/pipeline-command.js +279 -0
  26. package/lib/pipeline-confirm.js +142 -0
  27. package/lib/pipeline-exec.js +174 -0
  28. package/lib/pipeline-nudge.js +1281 -0
  29. package/lib/pipeline-observations.js +181 -0
  30. package/lib/pipeline-observe.js +133 -0
  31. package/lib/pipeline-runner.js +346 -0
  32. package/lib/pipeline-runs.js +442 -0
  33. package/lib/pipeline.js +655 -0
  34. package/lib/scope.js +16 -4
  35. package/lib/semantic.js +66 -10
  36. package/lib/serve.js +1 -1
  37. package/lib/service.js +59 -18
  38. package/lib/session-usage.js +350 -0
  39. package/lib/settings.js +138 -1
  40. package/lib/store.js +132 -6
  41. package/lib/summarize.js +30 -0
  42. package/lib/tools.js +11 -2
  43. package/package.json +3 -3
  44. package/scripts/changelog-prep.mjs +62 -7
  45. package/scripts/check-sync.js +8 -6
  46. package/scripts/publish-gate.mjs +197 -0
  47. package/scripts/release-docs.mjs +204 -0
  48. package/scripts/release-notes.mjs +162 -0
  49. package/src/api-standalone.js +78 -12
  50. package/src/api.js +403 -62
  51. package/src/candidates.js +454 -0
  52. package/src/capabilities.js +819 -0
  53. package/src/config.js +63 -6
  54. package/src/continuity.js +35 -6
  55. package/src/dream/decisions.js +17 -1
  56. package/src/dream/sleep.js +10 -2
  57. package/src/dream.js +5 -1
  58. package/src/embedding.js +116 -15
  59. package/src/entities/extractor.js +4 -0
  60. package/src/index.js +81 -12
  61. package/src/inject.js +40 -4
  62. package/src/lang.js +124 -0
  63. package/src/pipeline-activities.js +178 -0
  64. package/src/pipeline-attribution.js +165 -0
  65. package/src/pipeline-command.js +279 -0
  66. package/src/pipeline-confirm.js +142 -0
  67. package/src/pipeline-exec.js +174 -0
  68. package/src/pipeline-nudge.js +1281 -0
  69. package/src/pipeline-observations.js +181 -0
  70. package/src/pipeline-observe.js +133 -0
  71. package/src/pipeline-runner.js +346 -0
  72. package/src/pipeline-runs.js +442 -0
  73. package/src/pipeline.js +655 -0
  74. package/src/scope.js +16 -4
  75. package/src/semantic.js +66 -10
  76. package/src/serve.js +1 -1
  77. package/src/service.js +59 -18
  78. package/src/session-usage.js +350 -0
  79. package/src/settings.js +138 -1
  80. package/src/store.js +132 -6
  81. package/src/summarize.js +30 -0
  82. package/src/tools.js +11 -2
  83. package/test/api-hardening.test.js +173 -0
  84. package/test/api.test.js +244 -7
  85. package/test/candidates.test.js +345 -0
  86. package/test/capabilities.test.js +577 -0
  87. package/test/changelog-prep.test.js +74 -8
  88. package/test/cli-enums.test.js +37 -0
  89. package/test/client.test.js +510 -2
  90. package/test/continuity.test.js +84 -0
  91. package/test/docs-config-keys.test.js +65 -0
  92. package/test/embed-ready-poll.test.js +94 -0
  93. package/test/entities.test.js +13 -2
  94. package/test/failure-query.test.js +147 -0
  95. package/test/failure-retention.test.js +105 -0
  96. package/test/graph-api.test.js +36 -2
  97. package/test/helpers/write-admission-samples.js +6 -2
  98. package/test/inject-recall.test.js +32 -4
  99. package/test/lib-smoke.test.js +20 -0
  100. package/test/llm-audit.test.js +92 -1
  101. package/test/mint-source.test.js +202 -0
  102. package/test/persona-api.test.js +172 -0
  103. package/test/persona-flag.test.js +36 -0
  104. package/test/persona-inject.test.js +145 -0
  105. package/test/persona-settings.test.js +145 -0
  106. package/test/pipeline-activities.test.js +155 -0
  107. package/test/pipeline-attribution.test.js +175 -0
  108. package/test/pipeline-command.test.js +243 -0
  109. package/test/pipeline-confirm.test.js +148 -0
  110. package/test/pipeline-exec.test.js +172 -0
  111. package/test/pipeline-nudge.test.js +1036 -0
  112. package/test/pipeline-observations.test.js +157 -0
  113. package/test/pipeline-observe.test.js +127 -0
  114. package/test/pipeline-r1r2.test.js +149 -0
  115. package/test/pipeline-runner.test.js +442 -0
  116. package/test/pipeline-runs.test.js +767 -0
  117. package/test/pipeline.test.js +636 -0
  118. package/test/placement.test.js +1 -1
  119. package/test/publish-gate.test.js +272 -0
  120. package/test/recall-retention.test.js +128 -0
  121. package/test/reindex-backfill.test.js +83 -4
  122. package/test/release-docs.test.js +130 -0
  123. package/test/release-notes.test.js +240 -0
  124. package/test/release-workflow.test.js +93 -0
  125. package/test/route-policy.test.js +113 -0
  126. package/test/scope-distill-stamp.test.js +135 -0
  127. package/test/security-doc.test.js +139 -0
  128. package/test/sensitive-scan.test.js +44 -0
  129. package/test/serve.test.js +1 -1
  130. package/test/session-usage.test.js +308 -0
  131. package/test/store-write-path-columns.test.js +191 -0
  132. package/test/summarize.test.js +176 -0
  133. package/test/supersede-state-tracking.test.js +4 -4
  134. package/test/tools.test.js +16 -1
  135. package/test/workflow-guards.test.js +156 -0
@@ -0,0 +1,442 @@
1
+ /**
2
+ * M.O.(Modus Operandi · 手法)工作流的**运行账**。
3
+ *
4
+ * 读侧(`src/pipeline.js`)已经能把「一条工作流跑了怎么样」算成四态,但账本一直没人写,
5
+ * 于是 `runs` 恒为空数组、节点状态恒为 `candidate`。本模块补的就是这一环:把一次 run
6
+ * 落到盘上、再读回来成读侧认得的那份形状。
7
+ *
8
+ * **落点为什么跟手写的 `.json` 分开**:`<项目>/.mneme/pipelines/<id>.json` 是人手写、
9
+ * 要 review、要提交的;账本是每跑一次就追加一行的流水。挤进同一个文件,一次 run 就会
10
+ * 把人的 diff 冲掉,而人改过的内容与机器追加的内容此后再也分不开。
11
+ *
12
+ * **为什么是 JSONL 不是一个大 JSON 数组**:追加一条要重写整个数组,而这个文件会被
13
+ * 「刚刚跑完一次」这种**不能失败**的动作写。一行一条时,写到一半断电只毁最后一行;
14
+ * 整份重写时,同样的故障会毁掉全部历史。
15
+ *
16
+ * 形状以读侧的 `tallyRuns` 为准(那里是权威):只有 `skipped` 与 `detoured` 都为假才算
17
+ * 一次 `uses`,两者各自独立累加,`refuted` 只当布尔用,`step.at` 取最大值当 `lastUsedAt`。
18
+ *
19
+ * **读盘与写盘刻意不共用一条校验路径**(两者要的东西不一样):
20
+ * - 写盘是**我们自己在说话**,缺的字段意思明确(没写就是没有),补默认值是替它把话说完;
21
+ * - 读盘是**听别人说话**,缺一个布尔就等于「这一步照图走了」这条正面证据是凭空来的
22
+ * ——读侧宁可不认这条记录(报噪 + 跳过),也不能替它补一个含义。
23
+ * 早期版本两处共用一个函数,于是一行只有 `{id, at}` 的记录被算成一次成功使用、把节点
24
+ * 染成 `trusted`:账本没说话,面板却在作证。这类错比读不出来严重得多。
25
+ *
26
+ * **一批读不出来 ≠ 整份账本报废**:读盘逐行收,坏行进 `problems`、好行照收。一份账本
27
+ * 因为一行被截断就永久读不出来,等于让一次断电抹掉全部历史,而写侧还会继续报成功。
28
+ */
29
+
30
+ import {
31
+ appendFileSync,
32
+ closeSync,
33
+ existsSync,
34
+ fstatSync,
35
+ mkdirSync,
36
+ openSync,
37
+ readFileSync,
38
+ readSync
39
+ } from "node:fs";
40
+ import { isAbsolute, join, resolve } from "node:path";
41
+ import { ACTORS } from "./pipeline.js";
42
+
43
+ /** 账本的后缀:与手写工作流的 `.json` 分开,正是为了避免同 id 撞名。 */
44
+ export const RUNS_SUFFIX = ".runs.jsonl";
45
+
46
+ /**
47
+ * 账本的五种状态。存在的理由是**「读不出来」与「还没跑过」在节点状态上完全同形**
48
+ * (都是全体 `candidate`):不把它们分开,面板就会把一次「历史读不出来了」画成
49
+ * 「这条工作流还没被验证过」——这是两种完全不同的说法,而后者让人安心。
50
+ *
51
+ * absent 盘上没有这个账本文件
52
+ * empty 文件在、一条 run 都没有(有人碰过它,但还没写进一次运行)
53
+ * ok 读得干净,至少一条 run
54
+ * partial 收到一部分,另有一些行读不出来(行号在 problems 里)
55
+ * broken 文件在,但一条可用的 run 都收不到(含整份读不进来)
56
+ */
57
+ export const RUNS_STATES = ["absent", "empty", "ok", "partial", "broken"];
58
+
59
+ /** 校验失败的统一出口。名字与 `PipelineError` 成对:一个是工作流写坏了,一个是账本写坏了。 */
60
+ export class RunError extends Error {
61
+ constructor(message) {
62
+ super(message);
63
+ this.name = "RunError";
64
+ }
65
+ }
66
+
67
+ const bad = (msg) => {
68
+ throw new RunError(msg);
69
+ };
70
+
71
+ /** 报错时回显原始行/值的上限:账本行可能很长,别把整份文件塞进一条错误。 */
72
+ const MAX_ECHO = 120;
73
+
74
+ const echo = (value) => JSON.stringify(value).slice(0, MAX_ECHO);
75
+
76
+ /**
77
+ * id 只当**一个文件名片段**用,绝不当路径用。
78
+ *
79
+ * 拒掉而不是清洗:清洗会把 `../../x` 悄悄变成 `x`,于是写到了调用方没指定的文件上
80
+ * ——那类错在界面上看不出来,是最难查的一种。`..` 单独一个也要拒:它合法地通过
81
+ * 「无分隔符」检查,却仍然指向上级目录。
82
+ */
83
+ function isSafeSegment(id) {
84
+ if (typeof id !== "string") return false;
85
+ const value = id.trim();
86
+ if (value === "" || value === "." || value === "..") return false;
87
+ if (value.includes("/") || value.includes("\\")) return false;
88
+ if (isAbsolute(value)) return false;
89
+ return true;
90
+ }
91
+
92
+ /**
93
+ * 账本的家:`<项目>/.mneme/pipelines/`。目录名与读侧同一处来源(同一段路径拼接),
94
+ * 免得两边各写一份常量、改一处漏一处。
95
+ */
96
+ export function runsDir(projectRoot) {
97
+ if (typeof projectRoot !== "string" || projectRoot.trim() === "") {
98
+ bad("projectRoot 必须是项目根的路径");
99
+ }
100
+ return join(projectRoot, ".mneme", "pipelines");
101
+ }
102
+
103
+ /** 一个 id 的账本文件:`<项目>/.mneme/pipelines/<id>.runs.jsonl`。 */
104
+ export function runsPath(projectRoot, id) {
105
+ const dir = runsDir(projectRoot);
106
+ if (!isSafeSegment(id)) bad(`id 不是合法的文件名片段:${echo(id)}`);
107
+ const full = resolve(dir, id.trim() + RUNS_SUFFIX);
108
+ // 双保险:片段检查已经挡掉了穿越,这里再确认落点确实在账本目录里
109
+ // ——将来有人放宽上面的检查时,这一层会先变红。
110
+ if (!full.startsWith(resolve(dir))) bad(`id 会写到账本目录之外:${echo(id)}`);
111
+ return full;
112
+ }
113
+
114
+ /**
115
+ * 时间戳收成规范的 ISO 毫秒 UTC 串。
116
+ *
117
+ * 收成**唯一一种**形状是刻意的:`step.at` 会被读侧按字符串比较取最大值当 `lastUsedAt`,
118
+ * 同一条账里混着 `2026-10-15T09:00:01Z` 与 `2026-10-15T09:00:01.500Z` 时,比较结果
119
+ * 与人对时间先后的直觉不一致。写进来/读回来的那一刻就把它们拉齐。
120
+ */
121
+ function normalizeAt(value, where) {
122
+ if (value instanceof Date) {
123
+ if (Number.isNaN(value.getTime())) bad(`${where} 是一个无效的 Date`);
124
+ return value.toISOString();
125
+ }
126
+ if (typeof value === "number" && Number.isFinite(value)) return new Date(value).toISOString();
127
+ if (typeof value === "string") {
128
+ const trimmed = value.trim();
129
+ if (trimmed === "") bad(`${where} 不能是空字符串`);
130
+ const ms = Date.parse(trimmed);
131
+ if (!Number.isFinite(ms)) bad(`${where} 不是能解析的时间:${echo(value)}`);
132
+ return new Date(ms).toISOString();
133
+ }
134
+ bad(`${where} 必须是 ISO 时间串、Date 或毫秒数,收到 ${value === null ? "null" : typeof value}`);
135
+ }
136
+
137
+ function requireRecord(value, where) {
138
+ if (!value || typeof value !== "object" || Array.isArray(value)) bad(`${where} 必须是一个对象`);
139
+ return value;
140
+ }
141
+
142
+ /**
143
+ * 写盘的布尔:缺了补 `false`。调用方是「我们自己」,没写就是不打算标注。
144
+ */
145
+ function writeBool(value, where) {
146
+ if (value === undefined || value === null) return false;
147
+ if (typeof value !== "boolean") bad(`${where} 必须是布尔`);
148
+ return value;
149
+ }
150
+
151
+ /**
152
+ * 读盘的布尔:**缺了就是错**,绝不补默认。
153
+ *
154
+ * 补 `false` 的后果不是「少一个标注」而是「多一条证据」:`tallyRuns` 只有看到两个布尔
155
+ * 都为假才算一次 `uses`,于是缺字段的行会被读成「照图走了」,节点由 `candidate` 变
156
+ * `trusted`。账本没说话,面板却在作证——这是读侧能犯的最重的一类错。
157
+ */
158
+ function readBool(step, key, where) {
159
+ if (typeof step[key] !== "boolean") {
160
+ bad(`${where}.${key} 缺失或不是布尔——账本必须自己说清这一步的结局,读侧不替它补`);
161
+ }
162
+ return step[key];
163
+ }
164
+
165
+ /**
166
+ * `kind` 是绕行的**旧写法别名**(读侧 `tallyRuns` 至今认 `step.kind === "detour"`)。
167
+ *
168
+ * 别名只有被认下来才有意义:归一化只留契约那六个键,一个不认识的 `kind` 被默默丢掉时,
169
+ * 丢掉的是**这一步的结局本身**——一次绕行会在「写→读」往返后变成一次成功使用。
170
+ * 所以这里不但要认 `"detour"`,还要把不认识的取值当场拒掉,而不是放它过去。
171
+ */
172
+ function isDetourAlias(step, where) {
173
+ if (step.kind === undefined || step.kind === null) return false;
174
+ if (step.kind === "detour") return true;
175
+ bad(`${where}.kind 只认 "detour"(其余取值读侧会忽略,等于丢掉这一步的结局),收到 ${echo(step.kind)}`);
176
+ }
177
+
178
+ /**
179
+ * 把一条 run 收成契约形状(**写盘路径**):缺的补默认、多余的丢掉、坏的直接抛。
180
+ *
181
+ * 「坏的直接抛」与读侧 `loadPipelines` 同一纪律:**静默跳过最难查**——账本里少一条,
182
+ * 面板上看起来只是「这条工作流好像没跑过」,而真相是有一次运行丢了。
183
+ *
184
+ * `expectedStepIds` 给的是「图上有哪些步骤」。给了却不查,写错的 step id 会落盘、能被
185
+ * 读出来,却没有任何节点拿到它——症状与「从没跑过」同形,比丢掉更隐蔽。
186
+ */
187
+ export function normalizeRun(raw, { expectedStepIds = null } = {}) {
188
+ const run = requireRecord(raw, "run");
189
+ const at = normalizeAt(raw.at, "run.at");
190
+
191
+ const by = typeof raw.by === "string" ? raw.by.trim() : raw.by;
192
+ if (!ACTORS.includes(by)) bad(`run.by 必须是 ${ACTORS.join(" / ")} 之一,收到 ${echo(raw.by)}`);
193
+
194
+ const triggeredBy = raw.triggeredBy === undefined || raw.triggeredBy === null ? "" : raw.triggeredBy;
195
+ if (typeof triggeredBy !== "string") {
196
+ bad(`run.triggeredBy 必须是字符串(trigger.value 或空串),收到 ${typeof raw.triggeredBy}`);
197
+ }
198
+
199
+ if (!Array.isArray(raw.steps) || raw.steps.length === 0) {
200
+ bad("run.steps 至少要有一条——一次什么步骤都没走过的 run 不提供任何账");
201
+ }
202
+ const steps = normalizeSteps(raw.steps, { expectedStepIds, writing: true });
203
+ return { at, by, triggeredBy, steps };
204
+ }
205
+
206
+ /**
207
+ * 绕行有两个来源:`detoured:true` 与旧写法 `kind:"detour"`(读侧至今认它)。
208
+ * 两者是**或**的关系,所以「只给了 kind」不算缺字段;但一个都不给、给错类型、
209
+ * 或者给了一个读侧不认识的 kind,都必须当场报出来——那丢的是这一步的结局本身。
210
+ */
211
+ function detouredOf(step, where, writing) {
212
+ const alias = isDetourAlias(step, where);
213
+ if (writing) return writeBool(step.detoured, `${where}.detoured`) || alias;
214
+ if (typeof step.detoured === "boolean") return step.detoured || alias;
215
+ if (alias) return true;
216
+ bad(`${where}.detoured 缺失或不是布尔——账本必须自己说清这一步的结局,读侧不替它补`);
217
+ }
218
+
219
+ /**
220
+ * 步骤表的**共同骨架**:写盘(补默认)与读盘(缺就报错)只差「布尔缺了怎么办」,
221
+ * 其余校验两边必须一致——不一致的地方就是语义分叉的地方。
222
+ */
223
+ function normalizeSteps(rawSteps, { expectedStepIds = null, writing = false } = {}) {
224
+ const expected = expectedStepIds ? new Set(expectedStepIds) : null;
225
+ const seen = new Set();
226
+ return rawSteps.map((rawStep, i) => {
227
+ const where = `run.steps[${i}]`;
228
+ const step = requireRecord(rawStep, `${where} 必须是一个对象`);
229
+ const id = typeof step.id === "string" ? step.id.trim() : step.id;
230
+ if (!isSafeSegment(id)) bad(`${where}.id 不是合法的步骤 id:${echo(step.id)}`);
231
+ // 同一条 run 里重复的 step id 会被 `tallyRuns` 数两次,使用次数于是虚高。
232
+ if (seen.has(id)) bad(`${where}.id "${id}" 在同一条 run 里出现了两次`);
233
+ seen.add(id);
234
+ // 图里没有的步骤 id:账落在一个没有节点的地方——数得出来,面板上永远看不见。
235
+ if (expected && !expected.has(id)) {
236
+ bad(`${where}.id "${id}" 不在工作流的步骤表里(${[...expected].join(" / ")})`);
237
+ }
238
+ const boolAt = (key) => (writing ? writeBool(step[key], `${where}.${key}`) : readBool(step, key, where));
239
+ const note = step.note === undefined || step.note === null ? "" : step.note;
240
+ if (typeof note !== "string") bad(`${where}.note 必须是字符串`);
241
+ if (writing && step.at === undefined) bad(`${where}.at 缺失——时间不做「补默认」:一次 run 什么时候跑的只有调用方知道`);
242
+ return {
243
+ id,
244
+ at: normalizeAt(step.at, `${where}.at`),
245
+ skipped: boolAt("skipped"),
246
+ detoured: detouredOf(step, where, writing),
247
+ refuted: boolAt("refuted"),
248
+ note
249
+ };
250
+ });
251
+ }
252
+
253
+ /**
254
+ * 读盘路径的校验:**一条记录的形状**。缺布尔、缺 step、id 越界、kind 不认识,都抛
255
+ * (由 `readRuns` 收成 problems 里的行号 + 原因)。
256
+ *
257
+ * `by` 只在**给了**的时候校验:它是描述字段,读侧投影不消费它;而 `at` 与三个布尔是
258
+ * 被消费的、会让计数变样的字段,必须齐。
259
+ */
260
+ function readStoredRun(raw, { expectedStepIds = null } = {}) {
261
+ const run = requireRecord(raw, "顶层必须是一个对象");
262
+ if (run.at === undefined) bad("run.at 缺失");
263
+ const at = normalizeAt(run.at, "run.at");
264
+ if (run.by !== undefined) {
265
+ const by = typeof run.by === "string" ? run.by.trim() : run.by;
266
+ if (!ACTORS.includes(by)) bad(`run.by 必须是 ${ACTORS.join(" / ")} 之一,收到 ${echo(run.by)}`);
267
+ }
268
+ const triggeredBy = run.triggeredBy === undefined || run.triggeredBy === null ? "" : run.triggeredBy;
269
+ if (typeof triggeredBy !== "string") bad("run.triggeredBy 必须是字符串");
270
+ if (!Array.isArray(run.steps) || run.steps.length === 0) bad("run.steps 至少要有一条");
271
+ return {
272
+ at,
273
+ by: run.by === undefined || run.by === null ? "" : String(run.by).trim(),
274
+ triggeredBy,
275
+ steps: normalizeSteps(run.steps, { expectedStepIds, writing: false })
276
+ };
277
+ }
278
+
279
+ /** 一行的问题:行号(文件级问题为 null)、是否只是一截尾巴、给人看的原因。 */
280
+ function problem(line, detail, truncated) {
281
+ return { line, truncated: truncated === true, detail };
282
+ }
283
+
284
+ function stateOf(runs, problems) {
285
+ if (problems.length === 0) return runs.length ? "ok" : "empty";
286
+ return runs.length ? "partial" : "broken";
287
+ }
288
+
289
+ /**
290
+ * 读一个项目的账本,**逐行收**:好行照收,坏行进 `problems`,并给出一个 `state`。
291
+ *
292
+ * 为什么坏行不再「一条就抛」:一条被截断的尾行(写到一半断电、编辑器只保存了一半)
293
+ * 会让整份账本永久读不出来,而写侧还在继续报成功——一次断电就抹掉全部历史。
294
+ * 那条尾行是**可判定**的(最后一行 + 文件不以换行结尾),所以报噪、跳过、其余照收;
295
+ * 中间的坏行同样报噪跳过,不再判死整份文件。**报噪 ≠ 静默**:行号与原因都在 `problems` 里,
296
+ * 端点把它带成 `runsErrors`,状态降成 partial/broken。
297
+ *
298
+ * 目录不存在、文件不存在、文件是空的 → 空数组 + `absent`/`empty`:一条工作流还没跑过
299
+ * 是正常状态,面板据此画空态而不是画成「读不出来」。
300
+ */
301
+ export function readRuns(projectRoot, id, { expectedStepIds = null } = {}) {
302
+ const file = runsPath(projectRoot, id);
303
+ if (!existsSync(file)) return { runs: [], problems: [], state: "absent" };
304
+ let text;
305
+ try {
306
+ text = readFileSync(file, "utf8");
307
+ } catch (error) {
308
+ // 文件在、读不出来:这是「坏了」,不是「还没跑过」——两者在面板上必须分得开。
309
+ return { runs: [], problems: [problem(null, `读不出来:${error.message}`, false)], state: "broken" };
310
+ }
311
+ // UTF-8 BOM 不是数据,但它会让第一行 `JSON.parse` 失败、整份账本从此读不出来。
312
+ // 这是**可修复**的故障:BOM 是写文件的工具塞进去的,不是账本的内容。
313
+ if (text.charCodeAt(0) === 0xfeff) text = text.slice(1);
314
+ if (text.trim() === "") return { runs: [], problems: [], state: "empty" };
315
+
316
+ const endsWithNewline = /(\r\n|\n|\r)$/.test(text);
317
+ const lines = text.split(/\r\n|\n|\r/);
318
+ // 文件末尾的空行没有数据(多按一次回车就会产生),不算「一行账」;
319
+ // **中间**的空行是坏行,下面照报。
320
+ while (lines.length && lines[lines.length - 1] === "") lines.pop();
321
+
322
+ const runs = [];
323
+ const problems = [];
324
+ lines.forEach((line, index) => {
325
+ const lineNo = index + 1;
326
+ if (line.trim() === "") {
327
+ problems.push(problem(lineNo, "空行", false));
328
+ return;
329
+ }
330
+ let parsed;
331
+ try {
332
+ parsed = JSON.parse(line);
333
+ } catch (error) {
334
+ // 可判定的截断:最后一行 + 文件不以换行结尾。这只可能是写到一半断了,
335
+ // 与「中间某行是垃圾」不是一回事,但两者都只影响自己这一行。
336
+ const truncated = index === lines.length - 1 && !endsWithNewline;
337
+ problems.push(problem(
338
+ lineNo,
339
+ `${truncated ? "末行被截断" : "JSON 解析失败"}(${error.message});这一行是 ${line.slice(0, MAX_ECHO)}`,
340
+ truncated
341
+ ));
342
+ return;
343
+ }
344
+ try {
345
+ runs.push(readStoredRun(parsed, { expectedStepIds }));
346
+ } catch (error) {
347
+ problems.push(problem(lineNo, error.message, false));
348
+ }
349
+ });
350
+
351
+ return { runs, problems, state: stateOf(runs, problems) };
352
+ }
353
+
354
+ /**
355
+ * 只看账本**尾 1 字节**:上一条有没有以换行收尾。
356
+ *
357
+ * 为什么不能整份读:这是每次追加都要做的一步,而账本只增不减——整份读会让热路径的
358
+ * 代价随历史长度线性增长(一次 run 一次全文读)。尾字节用 open + fstat + read 取,
359
+ * 与账本多大无关。
360
+ *
361
+ * 读尾字节失败时退回「不补换行」:这里不是失败该现形的地方,真正的写入失败会在
362
+ * `appendFileSync` 那一步抛出来。
363
+ */
364
+ function tailNeedsNewline(file) {
365
+ let fd;
366
+ try {
367
+ fd = openSync(file, "r");
368
+ } catch {
369
+ return false; // 文件还不存在 = 空账本,不需要补
370
+ }
371
+ try {
372
+ const size = fstatSync(fd).size;
373
+ if (size === 0) return false;
374
+ const tail = Buffer.allocUnsafe(1);
375
+ readSync(fd, tail, 0, 1, size - 1);
376
+ return tail[0] !== 0x0a; // 0x0a = "\n"
377
+ } catch {
378
+ return false;
379
+ } finally {
380
+ closeSync(fd);
381
+ }
382
+ }
383
+
384
+ /**
385
+ * 追加一条 run(一次 run 一行)。返回值是归一化后的那条,便于调用方回执。
386
+ *
387
+ * 补换行的理由:少了它,追加会把上一条与本条**粘成一行**,于是一行坏 JSON 让整个
388
+ * 账本读不出来——而账本是「刚刚跑完一次」时才动的文件,恰恰最不该在这种地方失败。
389
+ *
390
+ * 写入是严格的:`expectedStepIds` 给了就当场拒(写错的 step id 落到盘上之后,
391
+ * 面板上跟「从没跑过」长得一模一样,那时再查就晚了)。
392
+ */
393
+ export function appendRun(projectRoot, id, run, { expectedStepIds = null } = {}) {
394
+ const file = runsPath(projectRoot, id);
395
+ const normalized = normalizeRun(run, { expectedStepIds });
396
+ mkdirSync(runsDir(projectRoot), { recursive: true });
397
+ const prefix = tailNeedsNewline(file) ? "\n" : "";
398
+ try {
399
+ appendFileSync(file, prefix + JSON.stringify(normalized) + "\n", "utf8");
400
+ } catch (error) {
401
+ throw new RunError(`${id}${RUNS_SUFFIX}: 写不进去:${error.message}`);
402
+ }
403
+ return normalized;
404
+ }
405
+
406
+ /**
407
+ * 一次问一批 id 的账本,供 `/pipeline` 端点喂给 `readPipelineDoc`。
408
+ *
409
+ * 一条账本坏了不该让整个面板打不开(与「项目层坏一个 .json 不拖死全局层」同一口径),
410
+ * 但**也不能假装它没坏**:坏行改从 `errors` 里如实回来(带行号),状态经由 `statesById`
411
+ * 显式带出去。缺账本与坏账本因此长得不一样。
412
+ *
413
+ * `expectedStepIdsById` 是「每条工作流的步骤表」:给了就顺手把「账本里写了图上没有的
414
+ * step id」变成可见的噪音——那种行能落盘、能数出来,却没有任何节点拿到它。
415
+ */
416
+ export function loadRunsById(projectRoot, ids, { expectedStepIdsById = null } = {}) {
417
+ const runsById = Object.create(null);
418
+ const statesById = Object.create(null);
419
+ const errors = [];
420
+ for (const id of ids || []) {
421
+ const expected = expectedStepIdsById && expectedStepIdsById[id] ? expectedStepIdsById[id] : null;
422
+ try {
423
+ const { runs, problems, state } = readRuns(projectRoot, id, { expectedStepIds: expected });
424
+ runsById[id] = runs;
425
+ statesById[id] = state;
426
+ for (const p of problems) {
427
+ errors.push({
428
+ id,
429
+ line: p.line,
430
+ truncated: p.truncated,
431
+ detail: `${id}${RUNS_SUFFIX}${p.line === null ? "" : ` 第 ${p.line} 行`}: ${p.detail}`
432
+ });
433
+ }
434
+ } catch (error) {
435
+ // 连文件都定位不到(id 本身不安全之类):如实记一条,状态按「读不出来」算。
436
+ runsById[id] = [];
437
+ statesById[id] = "broken";
438
+ errors.push({ id, line: null, truncated: false, detail: String((error && error.message) || error) });
439
+ }
440
+ }
441
+ return { runsById, errors, statesById };
442
+ }