openfeel 1.5.8 → 1.5.11

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 (69) hide show
  1. package/README.md +2 -1
  2. package/dist/cli/index.d.ts.map +1 -1
  3. package/dist/cli/index.js +2 -0
  4. package/dist/cli/index.js.map +1 -1
  5. package/dist/commands/chore.d.ts +7 -0
  6. package/dist/commands/chore.d.ts.map +1 -1
  7. package/dist/commands/chore.js +43 -3
  8. package/dist/commands/chore.js.map +1 -1
  9. package/dist/commands/dashboard.d.ts +42 -0
  10. package/dist/commands/dashboard.d.ts.map +1 -0
  11. package/dist/commands/dashboard.js +170 -0
  12. package/dist/commands/dashboard.js.map +1 -0
  13. package/dist/commands/flow.d.ts +8 -0
  14. package/dist/commands/flow.d.ts.map +1 -1
  15. package/dist/commands/flow.js +2 -1
  16. package/dist/commands/flow.js.map +1 -1
  17. package/dist/commands/knowledge.d.ts.map +1 -1
  18. package/dist/commands/knowledge.js.map +1 -1
  19. package/dist/commands/shared/json-output.d.ts.map +1 -1
  20. package/dist/commands/shared/json-output.js +1 -0
  21. package/dist/commands/shared/json-output.js.map +1 -1
  22. package/dist/core/artifact-graph/graph.d.ts.map +1 -1
  23. package/dist/core/dashboard/aggregate.d.ts +146 -0
  24. package/dist/core/dashboard/aggregate.d.ts.map +1 -0
  25. package/dist/core/dashboard/aggregate.js +231 -0
  26. package/dist/core/dashboard/aggregate.js.map +1 -0
  27. package/dist/core/dashboard/render.d.ts +12 -0
  28. package/dist/core/dashboard/render.d.ts.map +1 -0
  29. package/dist/core/dashboard/render.js +506 -0
  30. package/dist/core/dashboard/render.js.map +1 -0
  31. package/dist/core/flow-manager.d.ts.map +1 -1
  32. package/dist/core/flow-manager.js +1 -12
  33. package/dist/core/flow-manager.js.map +1 -1
  34. package/dist/core/i18n-data/en.d.ts +6 -0
  35. package/dist/core/i18n-data/en.d.ts.map +1 -1
  36. package/dist/core/i18n-data/en.js +63 -1
  37. package/dist/core/i18n-data/en.js.map +1 -1
  38. package/dist/core/i18n-data/zh-CN.d.ts +6 -0
  39. package/dist/core/i18n-data/zh-CN.d.ts.map +1 -1
  40. package/dist/core/i18n-data/zh-CN.js +63 -1
  41. package/dist/core/i18n-data/zh-CN.js.map +1 -1
  42. package/dist/core/i18n.js +4 -4
  43. package/dist/core/i18n.js.map +1 -1
  44. package/dist/core/migrate.d.ts +1 -1
  45. package/dist/core/migrate.d.ts.map +1 -1
  46. package/dist/core/migrate.js +4 -4
  47. package/dist/core/migrate.js.map +1 -1
  48. package/dist/core/public-logger.js +1 -1
  49. package/dist/core/public-logger.js.map +1 -1
  50. package/dist/core/template-loader.d.ts.map +1 -1
  51. package/dist/core/template-loader.js +46 -14
  52. package/dist/core/template-loader.js.map +1 -1
  53. package/dist/core/update.d.ts.map +1 -1
  54. package/dist/core/update.js +42 -10
  55. package/dist/core/update.js.map +1 -1
  56. package/dist/core/workspace/identity.js.map +1 -1
  57. package/dsh/ADAPTER.md +72 -13
  58. package/dsh/README.md +20 -11
  59. package/dsh/cordis.patch.yml +23 -3
  60. package/dsh/lib/adapters/guard-state.js +326 -0
  61. package/dsh/lib/adapters/session-cwd-registry.js +82 -0
  62. package/dsh/lib/adapters/sessions.js +7 -3
  63. package/dsh/lib/adapters/workflow-gate.js +52 -2
  64. package/dsh/lib/adapters/workspace.js +4 -2
  65. package/dsh/lib/guard.js +2732 -206
  66. package/dsh/lib/index.js +48 -4
  67. package/dsh/lib/tools/index.js +54 -1
  68. package/dsh/package.json +2 -2
  69. package/package.json +1 -1
@@ -0,0 +1,326 @@
1
+ /**
2
+ * 门禁自检状态(v1.5.11 D-126-04 / P0-4)
3
+ *
4
+ * 目的:让「门禁到底有没有生效、用哪个 cwd 判的、是不是旧进程/旧适配器」
5
+ * 成为**可查证据**,而不是靠「工具没报错」猜。两处落点:
6
+ *
7
+ * 1. 工作区:`<project>/.openfeel/dev/guard-state.json`
8
+ * 2. 全局:`os.tmpdir()/openfeel-guard/guard-state.json`
9
+ *
10
+ * **cwd 解析失败时必须落全局路径**(`root = null`):绝不用 `process.cwd()`
11
+ * (那会把宿主启动目录当成会话工作区,正是原 bug 的形态),也绝不静默丢弃——
12
+ * 那恰好是最需要证据的时刻。
13
+ *
14
+ * 失败策略:写盘失败要**出声**(stderr),但不阻塞判定(best-effort)。
15
+ * 读取顺序:工作区 → 全局(全局命中时 `source='global-fallback'`、`degraded=true`)。
16
+ *
17
+ * @module openfeel-dsh/guard-state
18
+ */
19
+ import { createHmac, randomBytes, timingSafeEqual } from 'node:crypto';
20
+ import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
21
+ import { tmpdir } from 'node:os';
22
+ import { dirname, join } from 'node:path';
23
+
24
+ /** 文件 schema 版本(后续字段演进用)。 */
25
+ export const GUARD_STATE_SCHEMA_VERSION = 1;
26
+
27
+ /**
28
+ * 进程内 HMAC 密钥(op-005 / P1-6):**只在内存里**,绝不落盘、绝不写日志。
29
+ *
30
+ * 为什么需要:`guard-state.json` 是普通 JSON,agent 可以用 P0-1 的写通道整体重写它
31
+ * (伪造 `pid` / `lastDecision` / `stale` 三键),让 `feel_status` 显示「门禁正常」。
32
+ * 加一个只有**当前进程**知道的密钥签名后:
33
+ * - 本进程写的状态 → 校验通过;
34
+ * - 别的进程(含重启前的自己/被伪造的文件)写的状态 → 校验失败 → `stale: true`
35
+ * (文案「门禁状态被篡改或来自旧进程」)。
36
+ *
37
+ * 代价:跨进程共享时必然 stale —— 这恰好是「状态是否由**正在服务的进程**写出」的
38
+ * 正确语义,不是缺陷。
39
+ */
40
+ const STATE_HMAC_KEY = randomBytes(32);
41
+
42
+ /** 递归稳定序列化(键排序),保证同一对象在写入/校验两侧得到同一字符串。 */
43
+ function stableStringify(value) {
44
+ if (Array.isArray(value)) return `[${value.map(stableStringify).join(',')}]`;
45
+ if (value !== null && typeof value === 'object') {
46
+ const keys = Object.keys(value).filter((key) => value[key] !== undefined).sort();
47
+ return `{${keys.map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`).join(',')}}`;
48
+ }
49
+ return JSON.stringify(value) ?? 'null';
50
+ }
51
+
52
+ /**
53
+ * 计算状态签名(排除 `hmac` 与读取时附加的 `source`)。
54
+ * @param {Record<string, any>} state
55
+ * @returns {string}
56
+ */
57
+ function signState(state) {
58
+ const { hmac: _hmac, source: _source, ...rest } = state;
59
+ return createHmac('sha256', STATE_HMAC_KEY).update(stableStringify(rest)).digest('hex');
60
+ }
61
+
62
+ /**
63
+ * 校验状态签名(op-005 / P1-6)。
64
+ * @param {Record<string, any> | null} state
65
+ * @returns {boolean}
66
+ */
67
+ export function verifyGuardStateSignature(state) {
68
+ if (state === null || typeof state !== 'object') return false;
69
+ const actual = state.hmac;
70
+ if (typeof actual !== 'string' || actual === '') return false;
71
+ const expected = signState(state);
72
+ if (actual.length !== expected.length) return false;
73
+ try {
74
+ return timingSafeEqual(Buffer.from(actual, 'utf8'), Buffer.from(expected, 'utf8'));
75
+ } catch {
76
+ return false;
77
+ }
78
+ }
79
+
80
+ /** 注册段字段(从全局文件播种到工作区文件时只带这些,避免把别的项目的 lastDecision 带进来)。 */
81
+ const REGISTRATION_KEYS = [
82
+ 'schemaVersion',
83
+ 'registered',
84
+ 'enforce',
85
+ 'shellGate',
86
+ 'hostStartedAt',
87
+ 'loaded_hash',
88
+ 'pid',
89
+ 'resolvedAt',
90
+ ];
91
+
92
+ /**
93
+ * 工作区 guard-state 路径。
94
+ * @param {string} root 项目根
95
+ * @returns {string}
96
+ */
97
+ export function guardStatePath(root) {
98
+ return join(root, '.openfeel', 'dev', 'guard-state.json');
99
+ }
100
+
101
+ /** 全局落点目录(cwd 不可解析时用)。 @returns {string} */
102
+ export function globalGuardDir() {
103
+ return join(tmpdir(), 'openfeel-guard');
104
+ }
105
+
106
+ /** 全局 guard-state 路径。 @returns {string} */
107
+ export function globalGuardStatePath() {
108
+ return join(globalGuardDir(), 'guard-state.json');
109
+ }
110
+
111
+ /**
112
+ * 全局审计落点(`recordGuardAudit(null)` 用)。
113
+ *
114
+ * 与 guard.js 的 `.openfeel/log/guard-audit.jsonl` 同名同格式:
115
+ * cwd 解析失败时证据不会丢,只是换了目录。
116
+ * @returns {string}
117
+ */
118
+ export function globalGuardAuditPath() {
119
+ return join(globalGuardDir(), 'guard-audit.jsonl');
120
+ }
121
+
122
+ /**
123
+ * 读一个 state 文件(缺失/损坏 → null)。
124
+ * @param {string} file
125
+ * @returns {Record<string, any> | null}
126
+ */
127
+ function readStateFile(file) {
128
+ try {
129
+ const raw = JSON.parse(readFileSync(file, 'utf-8'));
130
+ return typeof raw === 'object' && raw !== null ? raw : null;
131
+ } catch {
132
+ return null;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * 读取 guard-state(工作区优先,缺失/root=null 时回退全局)。
138
+ *
139
+ * @param {string | null} root 项目根(null = 只看全局)
140
+ * @returns {(Record<string, any> & { source: 'workspace'|'global-fallback' }) | null}
141
+ */
142
+ export function readGuardState(root) {
143
+ if (typeof root === 'string' && root !== '') {
144
+ const workspace = readStateFile(guardStatePath(root));
145
+ if (workspace !== null) return { ...workspace, source: 'workspace' };
146
+ }
147
+ const global = readStateFile(globalGuardStatePath());
148
+ if (global !== null) return { ...global, source: 'global-fallback' };
149
+ return null;
150
+ }
151
+
152
+ /**
153
+ * 写入 guard-state(best-effort;失败出声、不阻塞)。
154
+ *
155
+ * `root = null` → 固定写全局路径(**绝不**用 `process.cwd()`)。
156
+ * 工作区文件不存在时,用全局文件的注册段播种(registered/enforce/…),
157
+ * 这样第一次判定写出的工作区文件同样带自证三键。
158
+ *
159
+ * 失败时:stderr 打文件与原因;给了 `fallback`(at/tool/reason/cwdSource)时
160
+ * 再打两行结构化的判定现场 —— 「最需要证据的时候」绝不静默(D-126-04)。
161
+ *
162
+ * @param {string | null} root
163
+ * @param {Record<string, any>} patch
164
+ * @param {{ at?: string, tool?: string, reason?: string, decision?: string, cwdSource?: string } | null} [fallback]
165
+ * @returns {boolean} 是否写入成功
166
+ */
167
+ export function writeGuardState(root, patch, fallback = null) {
168
+ const isGlobal = typeof root !== 'string' || root === '';
169
+ const file = isGlobal ? globalGuardStatePath() : guardStatePath(root);
170
+ try {
171
+ const globalRaw = readStateFile(globalGuardStatePath());
172
+ const workspaceRaw = isGlobal ? null : readStateFile(file);
173
+ const seed = globalRaw === null
174
+ ? {}
175
+ : Object.fromEntries(REGISTRATION_KEYS.filter((key) => globalRaw[key] !== undefined).map((key) => [key, globalRaw[key]]));
176
+ const next = {
177
+ ...seed,
178
+ ...(workspaceRaw ?? {}),
179
+ ...patch,
180
+ schemaVersion: GUARD_STATE_SCHEMA_VERSION,
181
+ source: undefined,
182
+ updatedAt: new Date().toISOString(),
183
+ };
184
+ delete next.source;
185
+ delete next.hmac;
186
+ next.hmac = signState(next);
187
+ mkdirSync(dirname(file), { recursive: true });
188
+ const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
189
+ writeFileSync(tmp, `${JSON.stringify(next, null, 2)}\n`, 'utf-8');
190
+ renameSync(tmp, file);
191
+ return true;
192
+ } catch (error) {
193
+ const reason = error instanceof Error ? error.message : String(error);
194
+ process.stderr.write(`[guard] guard-state-unavailable:${file}(${reason})\n`);
195
+ if (fallback !== null && typeof fallback === 'object') {
196
+ process.stderr.write(
197
+ `[guard] at=${fallback.at ?? ''} tool=${fallback.tool ?? ''} reason=${fallback.reason ?? ''}\n`,
198
+ );
199
+ process.stderr.write(`[guard] cwdSource=${fallback.cwdSource ?? ''} decision=${fallback.decision ?? ''}\n`);
200
+ }
201
+ return false;
202
+ }
203
+ }
204
+
205
+ /**
206
+ * 删除 guard-state / 全局审计(测试用)。
207
+ * @param {string | null} root
208
+ */
209
+ export function clearGuardState(root) {
210
+ try {
211
+ if (typeof root === 'string' && root !== '') rmSync(guardStatePath(root), { force: true });
212
+ } catch {
213
+ /* ignore */
214
+ }
215
+ try {
216
+ rmSync(globalGuardStatePath(), { force: true });
217
+ rmSync(globalGuardAuditPath(), { force: true });
218
+ } catch {
219
+ /* ignore */
220
+ }
221
+ }
222
+
223
+ /**
224
+ * 可信度判定(op-005 / P1-6):返回不可信的原因,可信时返回 null。
225
+ *
226
+ * 顺序很重要 —— **先验签**:签名不过说明文件不是本进程写的(被 agent 重写、
227
+ * 或来自旧进程),后面所有自证字段都不可信,没必要再看。
228
+ *
229
+ * @param {Record<string, any> | null} state
230
+ * @param {Record<string, any> | null} hostMarker
231
+ * @param {number} pid
232
+ * @returns {{ reason: string, message?: string } | null}
233
+ */
234
+ export function guardStateDistrust(state, hostMarker = null, pid = process.pid) {
235
+ if (state === null || typeof state !== 'object') return null;
236
+ if (!verifyGuardStateSignature(state)) {
237
+ return {
238
+ reason: 'hmac',
239
+ message: '门禁状态被篡改或来自旧进程(guard-state 签名校验失败):请重启宿主后确认 feel_status.guard.stale === false。',
240
+ };
241
+ }
242
+ if (!Number.isFinite(state.pid)) {
243
+ return {
244
+ reason: 'pid-missing',
245
+ message: '门禁状态缺少 pid(无法证明由当前进程写入):请重启宿主后确认 feel_status.guard.stale === false。',
246
+ };
247
+ }
248
+ if (state.pid !== pid) return { reason: 'pid' };
249
+ if (hostMarker === null || typeof hostMarker !== 'object') return null;
250
+ if (
251
+ typeof state.loaded_hash === 'string' && state.loaded_hash !== ''
252
+ && typeof hostMarker.loaded_hash === 'string' && hostMarker.loaded_hash !== ''
253
+ && state.loaded_hash !== hostMarker.loaded_hash
254
+ ) return { reason: 'loaded-hash' };
255
+ const stateAt = Date.parse(state.hostStartedAt);
256
+ const markerAt = Date.parse(hostMarker.started_at);
257
+ if (Number.isFinite(stateAt) && Number.isFinite(markerAt) && stateAt < markerAt) return { reason: 'host-started-at' };
258
+ return null;
259
+ }
260
+
261
+ /**
262
+ * stale 判定(P0-4 / op-005 P1-6):状态不可信 → true。
263
+ *
264
+ * - **HMAC 校验失败** → 被篡改或来自旧进程(新增,防伪造);
265
+ * - **缺 `pid`** → 不可信(旧实现「无证据不冤枉」,但状态文件可被整体重写,
266
+ * 所以缺 pid 必须当 stale);
267
+ * - `pid` 与当前进程不符 → 状态来自旧进程;
268
+ * - `loaded_hash` 与标记不符 → 适配器文件已更新但宿主没重启;
269
+ * - `hostStartedAt` 早于标记的 `started_at` → 状态来自更早的宿主启动。
270
+ *
271
+ * @param {Record<string, any> | null} state
272
+ * @param {Record<string, any> | null} hostMarker
273
+ * @param {number} [pid]
274
+ * @returns {boolean}
275
+ */
276
+ export function computeGuardStale(state, hostMarker, pid = process.pid) {
277
+ return guardStateDistrust(state, hostMarker, pid) !== null;
278
+ }
279
+
280
+ /**
281
+ * 组装 `feel_status` / `feel_next` 的 `guard` 段。
282
+ *
283
+ * 两处都缺 → `{ registered:false, cwd:null, cwdSource:null, stale:false }`(计划口径:
284
+ * 不再假装生效)。`live` 是调用方用**同一条 cwd 解析链**现算的结果,仅用于
285
+ * 填补「还没有第一条判定」时的显示空缺(状态里已有 cwd 时以状态为准)。
286
+ *
287
+ * @param {{ root: string | null, hostMarker?: Record<string, any> | null, pid?: number, live?: { cwd: string | null, source: string, degraded: boolean } | null }} input
288
+ * @returns {Record<string, any>}
289
+ */
290
+ export function buildGuardSection({ root, hostMarker = null, pid = process.pid, live = null }) {
291
+ const state = readGuardState(root);
292
+ if (state === null) {
293
+ return { registered: false, cwd: null, cwdSource: null, stale: false };
294
+ }
295
+ const distrust = guardStateDistrust(state, hostMarker, pid);
296
+ const stale = distrust !== null;
297
+ const section = {
298
+ registered: state.registered === true,
299
+ enforce: typeof state.enforce === 'string' ? state.enforce : undefined,
300
+ shellGate: typeof state.shellGate === 'string' ? state.shellGate : undefined,
301
+ cwd: typeof state.cwd === 'string' && state.cwd !== '' ? state.cwd : null,
302
+ cwdSource: typeof state.cwdSource === 'string' && state.cwdSource !== '' ? state.cwdSource : null,
303
+ degraded: state.degraded === true || state.source === 'global-fallback',
304
+ resolvedAt: state.resolvedAt ?? null,
305
+ hostStartedAt: state.hostStartedAt ?? null,
306
+ loaded_hash: state.loaded_hash ?? null,
307
+ pid: Number.isFinite(state.pid) ? state.pid : null,
308
+ lastDecision: state.lastDecision ?? null,
309
+ stale,
310
+ // op-005 / P1-6:不可信的原因与文案(`hmac` = 被篡改或来自旧进程)。
311
+ ...(stale ? { staleReason: distrust.reason, staleMessage: distrust.message ?? null } : {}),
312
+ source: state.source,
313
+ };
314
+ // 只在「还没有任何判定」时用现场解析填补显示。**已有 lastDecision 时以状态为准**:
315
+ // fail-closed 那次判定写的是 `cwd:null / unresolved`,如果这里用同类链再解析一次
316
+ // (比如落到 process/config 环)就会把「已 fail-closed」粉饰成正常 —— 那正是
317
+ // D-126-04 要杜绝的「静默失效」。
318
+ const hasDecision = state.lastDecision !== null && typeof state.lastDecision === 'object';
319
+ if (!hasDecision && section.cwd === null && live !== null && typeof live.cwd === 'string' && live.cwd !== '') {
320
+ section.cwd = live.cwd;
321
+ section.cwdSource = live.source;
322
+ section.degraded = live.degraded === true;
323
+ section.source = 'live-resolution';
324
+ }
325
+ return section;
326
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * 会话 → 工作区 cwd 登记表(v1.5.11 D-126-01 / P1-4)
3
+ *
4
+ * 目的:给写入门禁的 cwd 解析链补上第 3 环。**只按 `sessionId` 存**,
5
+ * 不存任何「进程级最近一次 cwd」(`registry-last`):后者在多窗口/多项目时
6
+ * 会跨项目串味——窗口 A 在 P1(`exec_running`)登记过,窗口 B 在 P2
7
+ * (`review_pending`)拿不到 cwd 时就会用 P1 的 cwd 去判 B 的写入,
8
+ * 等于「按别的项目放行」,比静默放行更危险(plan §D-126-01 P1-4)。
9
+ *
10
+ * 纪律:
11
+ * - 纯内存、零依赖、全 try/catch —— 登记失败绝不影响工具/提示主流程;
12
+ * - 上限 64,LRU 淘汰(`get` 也算一次使用);
13
+ * - 只有 `sessionId` 与 `cwd` 都非空才写入(写入点不产生误记)。
14
+ *
15
+ * @module openfeel-dsh/session-cwd-registry
16
+ */
17
+
18
+ /** 登记上限(LRU)。 */
19
+ export const CWD_REGISTRY_LIMIT = 64;
20
+
21
+ /** @type {Map<string, { cwd: string, at: number }>} */
22
+ const registry = new Map();
23
+
24
+ /**
25
+ * 登记 / 刷新某会话的工作区。
26
+ * @param {unknown} sessionId
27
+ * @param {unknown} cwd
28
+ * @returns {void}
29
+ */
30
+ export function touchSession(sessionId, cwd) {
31
+ try {
32
+ if (typeof sessionId !== 'string' || sessionId.trim() === '') return;
33
+ if (typeof cwd !== 'string' || cwd.trim() === '') return;
34
+ const key = sessionId.trim();
35
+ // 重新插入以刷新 LRU 顺序
36
+ registry.delete(key);
37
+ registry.set(key, { cwd: cwd.trim(), at: Date.now() });
38
+ while (registry.size > CWD_REGISTRY_LIMIT) {
39
+ const oldest = registry.keys().next().value;
40
+ if (oldest === undefined) break;
41
+ registry.delete(oldest);
42
+ }
43
+ } catch {
44
+ /* 登记是观测手段,失败不影响主流程 */
45
+ }
46
+ }
47
+
48
+ /**
49
+ * 读取某会话登记的工作区(命中即刷新 LRU 顺序)。
50
+ * @param {unknown} sessionId
51
+ * @returns {string | null}
52
+ */
53
+ export function getSessionCwd(sessionId) {
54
+ try {
55
+ if (typeof sessionId !== 'string' || sessionId.trim() === '') return null;
56
+ const key = sessionId.trim();
57
+ const entry = registry.get(key);
58
+ if (entry === undefined) return null;
59
+ registry.delete(key);
60
+ registry.set(key, entry);
61
+ return entry.cwd;
62
+ } catch {
63
+ return null;
64
+ }
65
+ }
66
+
67
+ /**
68
+ * 当前登记的会话数(测试/诊断用)。
69
+ * @returns {number}
70
+ */
71
+ export function cwdRegistrySize() {
72
+ return registry.size;
73
+ }
74
+
75
+ /** 清空登记表(测试用)。 */
76
+ export function resetCwdRegistry() {
77
+ registry.clear();
78
+ }
79
+
80
+ /* 与 plan §3.3 的命名对齐(两种名字同义,避免各调用点各写一套)。 */
81
+ export const recordSessionCwd = touchSession;
82
+ export const lookupSessionCwd = getSessionCwd;
@@ -137,12 +137,16 @@ function writeSessions(projectRoot, sessions) {
137
137
  * @param {string|null} [stage] 当前活跃阶段(无 → '')
138
138
  * @param {number} [now] 当前时间(毫秒;注入以便单测)
139
139
  */
140
- export function touchSession(projectRoot, stage = '', now = Date.now()) {
140
+ export function touchSession(projectRoot, stage = undefined, now = Date.now()) {
141
141
  try {
142
142
  const id = resolveSessionId(projectRoot);
143
- const nextStage = typeof stage === 'string' && stage !== '' ? stage : null;
144
143
  const current = readSessions(projectRoot);
145
144
  const previous = current.find((entry) => entry.id === id);
145
+ // 语义(v1.5.9 修复):undefined = 调用方无阶段信息 → 保留旧值;'' = 明确无开启阶段 → 清空;
146
+ // 此前 '' 也保留旧值,导致已完成/已归档阶段长期滞留在 sessions[].stage。
147
+ const nextStage = stage === undefined
148
+ ? (previous?.stage ?? null)
149
+ : (typeof stage === 'string' && stage !== '' ? stage : null);
146
150
  const kept = current.filter(
147
151
  (entry) => entry.id !== id && now - Date.parse(entry.lastSeen) <= PRUNE_OLDER_THAN_MS,
148
152
  );
@@ -150,7 +154,7 @@ export function touchSession(projectRoot, stage = '', now = Date.now()) {
150
154
  id,
151
155
  cwd: projectRoot,
152
156
  lastSeen: new Date(now).toISOString(),
153
- stage: nextStage ?? previous?.stage ?? null,
157
+ stage: nextStage,
154
158
  });
155
159
  writeSessions(projectRoot, kept);
156
160
  } catch {
@@ -1,10 +1,13 @@
1
1
  /**
2
- * 流程入口闸(v1.5.2;v1.5.7 改为需求导向语义)
2
+ * 流程入口闸(v1.5.2;v1.5.7 需求导向;v1.5.11 增加门禁自检)
3
3
  *
4
4
  * 目的:把「每个需求都要建/推进阶段」纠正为「阶段是版本容器、长期保持开启」。
5
- * 无开启阶段时提示按当前小版本开一个(保持 exec_running),而不是把它当成一次性任务;
6
5
  * 已开启阶段时不做任何催促(需求在阶段内完成,阶段关闭由用户在发布节点决定)。
7
6
  *
7
+ * v1.5.11(D-126-04):若 `snapshot.guard` 显示门禁**自身**不可信
8
+ * (未注册 / cwd 解析失败 / 来自旧进程或旧适配器),优先提示它 —— 「门禁看着在跑
9
+ * 其实没生效」比「没开阶段」严重得多。**无 `guard` 字段时行为完全不变**(零回归)。
10
+ *
8
11
  * 纯函数、无 IO;由工具层在读取快照后调用。
9
12
  */
10
13
 
@@ -13,6 +16,53 @@
13
16
  * @returns {{ required: boolean, reason: string|null, message: string, actions: string[] }}
14
17
  */
15
18
  export function computeWorkflowGate(snapshot) {
19
+ const guard = snapshot?.guard;
20
+ if (guard !== null && guard !== undefined && typeof guard === 'object') {
21
+ if (guard.registered === false) {
22
+ return {
23
+ required: true,
24
+ reason: 'guard-inactive',
25
+ message:
26
+ '写入门禁未生效(guard-state 缺失或 guard 未注册,例如 profile 的 enforce=off 或宿主未加载适配器):'
27
+ + '当前对源码/文档的写入没有门禁保护。请重启宿主让适配器重新注册,或确认 enforce 不是 off。',
28
+ actions: [
29
+ '确认 profile(cordis.patch.yml)的 openfeel-feel.enforce 为 project 或 always',
30
+ '重启 DSH 宿主(guard 只在宿主启动时注册)',
31
+ 'feel_status 的 guard 段应显示 registered:true;否则看 .openfeel/dev/guard-state.json 与 ~/.openfeel/adapter-host-dsh.json',
32
+ ],
33
+ };
34
+ }
35
+ if (guard.cwd === null || guard.cwdSource === 'unresolved') {
36
+ return {
37
+ required: true,
38
+ reason: 'guard-cwd-unresolved',
39
+ message:
40
+ '写入门禁无法确定会话工作区(cwd 解析失败),已按 fail-closed 拒绝写入。'
41
+ + '修复(任一):① profile 的 cordis.patch.yml 给 openfeel-feel 配 cwd: "<项目绝对路径>";'
42
+ + '② 设环境变量 OPENFEEL_GUARD_CWD 后重启;③ 重启会话让提示装配登记 cwd。',
43
+ actions: [
44
+ 'cordis.patch.yml 配 cwd: "<项目绝对路径>" 后重启',
45
+ '或设 OPENFEEL_GUARD_CWD=<项目绝对路径> 后重启',
46
+ '重启会话让提示装配写入会话 cwd 登记表',
47
+ ],
48
+ };
49
+ }
50
+ if (guard.stale === true) {
51
+ return {
52
+ required: true,
53
+ reason: 'guard-stale',
54
+ message:
55
+ '门禁来自旧进程/旧适配器,判定不可信(适配器文件已更新但宿主仍加载旧模块,或状态来自更早的进程)。'
56
+ + '请完全重启 DSH 宿主后再写入。',
57
+ actions: [
58
+ '完全重启 DSH 宿主(不是重载会话)',
59
+ '重启后确认 feel_status.guard.stale === false',
60
+ '确认 ~/.openfeel/adapter-host-dsh.json 的 loaded_hash 与 .openfeel/dev/guard-state.json 一致',
61
+ ],
62
+ };
63
+ }
64
+ }
65
+
16
66
  const phase = snapshot?.focus?.phase;
17
67
  // v1.5.8:『开启阶段』= phase 非空且非 done。仅剩 done 阶段时同样要求开新阶段
18
68
  // (此前 done 被当成活跃阶段,导致需求导向下永远不提示开阶段)。
@@ -34,10 +34,12 @@ export const OPENFEEL_DIR = '.openfeel';
34
34
  *
35
35
  * - `flow.json`:`openfeel init` 必然创建,是核心数据存储。
36
36
  * - `config.yaml`:同为 `init` 产物(`flow.json` 被手工删除时的兜底判据)。
37
+ * - `stages` / `metrics.json`:op-005 / P2 —— `flow.json` 被删/搬走时,
38
+ * 仍有其它 init 产物可自证「这是 OpenFeel 项目」,避免误落 fail-closed 全拒。
37
39
  *
38
- * 全局家目录 `~/.openfeel/` 两者皆无,因此不会被误判。
40
+ * 全局家目录 `~/.openfeel/` 这些都没有,因此不会被误判。
39
41
  */
40
- export const PROJECT_MARKERS = /** @type {const} */ (['flow.json', 'config.yaml']);
42
+ export const PROJECT_MARKERS = /** @type {const} */ (['flow.json', 'config.yaml', 'stages', 'metrics.json']);
41
43
 
42
44
  /**
43
45
  * 从工具执行上下文解析会话工作区目录。