flow-comet 1.6.3 → 1.6.4

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 (78) hide show
  1. package/.flow-comet/skills/flow-comet/INSTALLED_VERSION +1 -1
  2. package/.flow-comet/skills/flow-comet/scripts/comet-hook-guard.mjs +11 -13
  3. package/.flow-comet/skills/flow-comet/scripts/context-init.mjs +2 -2
  4. package/.flow-comet/skills/flow-comet/scripts/evolve.mjs +6 -6
  5. package/.flow-comet/skills/flow-comet/scripts/guard-self-test.mjs +67 -18878
  6. package/.flow-comet/skills/flow-comet/scripts/health.mjs +1 -1
  7. package/.flow-comet/skills/flow-comet/scripts/items/a.mjs +1030 -0
  8. package/.flow-comet/skills/flow-comet/scripts/items/b.mjs +830 -0
  9. package/.flow-comet/skills/flow-comet/scripts/items/c.mjs +781 -0
  10. package/.flow-comet/skills/flow-comet/scripts/items/d.mjs +99 -0
  11. package/.flow-comet/skills/flow-comet/scripts/items/e.mjs +185 -0
  12. package/.flow-comet/skills/flow-comet/scripts/items/f.mjs +90 -0
  13. package/.flow-comet/skills/flow-comet/scripts/items/g.mjs +59 -0
  14. package/.flow-comet/skills/flow-comet/scripts/items/h.mjs +815 -0
  15. package/.flow-comet/skills/flow-comet/scripts/items/i.mjs +77 -0
  16. package/.flow-comet/skills/flow-comet/scripts/items/j.mjs +75 -0
  17. package/.flow-comet/skills/flow-comet/scripts/items/k.mjs +1427 -0
  18. package/.flow-comet/skills/flow-comet/scripts/items/l.mjs +275 -0
  19. package/.flow-comet/skills/flow-comet/scripts/items/m.mjs +614 -0
  20. package/.flow-comet/skills/flow-comet/scripts/lib/count-derivation.mjs +97 -0
  21. package/.flow-comet/skills/flow-comet/scripts/lib/fix-loop-node.mjs +196 -0
  22. package/.flow-comet/skills/flow-comet/scripts/lib/guard-evidence-gates.mjs +553 -0
  23. package/.flow-comet/skills/flow-comet/scripts/lib/guard-gates.mjs +837 -0
  24. package/.flow-comet/skills/flow-comet/scripts/lib/guard-routing.mjs +404 -0
  25. package/.flow-comet/skills/flow-comet/scripts/lib/guard-state-io.mjs +404 -0
  26. package/.flow-comet/skills/flow-comet/scripts/lib/guard-template-fidelity.mjs +302 -0
  27. package/.flow-comet/skills/flow-comet/scripts/lib/guard-uidesign.mjs +133 -0
  28. package/.flow-comet/skills/flow-comet/scripts/lib/reentry-node.mjs +237 -0
  29. package/.flow-comet/skills/flow-comet/scripts/lib/replan-node.mjs +209 -0
  30. package/.flow-comet/skills/flow-comet/scripts/lib/state-branch.mjs +49 -0
  31. package/.flow-comet/skills/flow-comet/scripts/lib/state-bridgecheck.mjs +201 -0
  32. package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-admin.mjs +141 -0
  33. package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-flow.mjs +700 -0
  34. package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-recovery.mjs +578 -0
  35. package/.flow-comet/skills/flow-comet/scripts/lib/state-determine.mjs +52 -0
  36. package/.flow-comet/skills/flow-comet/scripts/lib/state-evolve-hint.mjs +55 -0
  37. package/.flow-comet/skills/flow-comet/scripts/lib/state-explain.mjs +106 -0
  38. package/.flow-comet/skills/flow-comet/scripts/lib/state-intelscan.mjs +58 -0
  39. package/.flow-comet/skills/flow-comet/scripts/lib/state-io.mjs +147 -0
  40. package/.flow-comet/skills/flow-comet/scripts/lib/state-skillmarkers.mjs +189 -0
  41. package/.flow-comet/skills/flow-comet/scripts/lib/system-fixtures.mjs +1441 -0
  42. package/.flow-comet/skills/flow-comet/scripts/lib/task-graph.mjs +471 -0
  43. package/.flow-comet/skills/flow-comet/scripts/lib/test-assertions.mjs +509 -0
  44. package/.flow-comet/skills/flow-comet/scripts/lib/test-fixtures.mjs +1228 -0
  45. package/.flow-comet/skills/flow-comet/scripts/lib/test-helpers.mjs +237 -0
  46. package/.flow-comet/skills/flow-comet/scripts/lib/text-anchor-checks.mjs +2484 -0
  47. package/.flow-comet/skills/flow-comet/scripts/protocol-utils.mjs +2 -2
  48. package/.flow-comet/skills/flow-comet/scripts/route-node.mjs +72 -1123
  49. package/.flow-comet/skills/flow-comet/scripts/scenarios/a.mjs +966 -0
  50. package/.flow-comet/skills/flow-comet/scripts/scenarios/b.mjs +1233 -0
  51. package/.flow-comet/skills/flow-comet/scripts/scenarios/c.mjs +1382 -0
  52. package/.flow-comet/skills/flow-comet/scripts/scenarios/d.mjs +483 -0
  53. package/.flow-comet/skills/flow-comet/scripts/scenarios/e.mjs +1388 -0
  54. package/.flow-comet/skills/flow-comet/scripts/scenarios/f.mjs +1466 -0
  55. package/.flow-comet/skills/flow-comet/scripts/scenarios/g.mjs +1364 -0
  56. package/.flow-comet/skills/flow-comet/scripts/scenarios/h.mjs +1287 -0
  57. package/.flow-comet/skills/flow-comet/scripts/scenarios/i.mjs +1436 -0
  58. package/.flow-comet/skills/flow-comet/scripts/scenarios/j.mjs +1384 -0
  59. package/.flow-comet/skills/flow-comet/scripts/scenarios/k.mjs +1338 -0
  60. package/.flow-comet/skills/flow-comet/scripts/scenarios/l.mjs +1484 -0
  61. package/.flow-comet/skills/flow-comet/scripts/scenarios/m.mjs +971 -0
  62. package/.flow-comet/skills/flow-comet/scripts/state-schema.mjs +1 -1
  63. package/.flow-comet/skills/flow-comet/scripts/system-test.mjs +58 -7559
  64. package/.flow-comet/skills/flow-comet/scripts/time-utils.mjs +4 -4
  65. package/.flow-comet/skills/flow-comet/scripts/workflow-guard.mjs +151 -2476
  66. package/.flow-comet/skills/flow-comet/scripts/workflow-handoff.mjs +5 -5
  67. package/.flow-comet/skills/flow-comet/scripts/workflow-state.mjs +27 -2190
  68. package/.flow-comet/skills/flow-comet-plan/GUIDANCE.md +1 -1
  69. package/README.md +52 -69
  70. package/package.json +3 -2
  71. package/scripts/dsh-bridge.mjs +2 -2
  72. package/scripts/lib/installer-cleanup.mjs +152 -0
  73. package/scripts/lib/installer-flowkit.mjs +119 -0
  74. package/scripts/lib/installer-hooks.mjs +363 -0
  75. package/scripts/lib/installer-loader.mjs +220 -0
  76. package/scripts/lib/installer-migrate.mjs +236 -0
  77. package/scripts/lib/installer-platforms.mjs +314 -0
  78. package/scripts/prepare-env.mjs +21 -1313
@@ -0,0 +1,237 @@
1
+ // reentry-node.mjs — archive 源受控重入判定(单一权威 · 纯函数)
2
+ // archive 已 entry 且归档移动尚未发生时,用户显式授权把工作归属退到 execute 家族 / review /
3
+ // verify 的受控回边。目标白名单 / 源转移语义 / 授权形态 / 轮次上限 / 幂等空操作的全部判定集中在
4
+ // 本模块:reenter 子命令只做编排与落盘,禁止内联第二份。
5
+ // 纯函数:无 fs / console / process.exit / state 写入。信号统一用 structured result:非法 / 未授权
6
+ // 返回 { ok: false, reason: '<machine-code>' },不抛异常。
7
+ // 依赖方向:只依赖 task-graph.mjs(协议节点顺序 / 启用 / change 名形态 / 事件归属)。
8
+ import {
9
+ route,
10
+ protocolNodeEnabled,
11
+ isSingleSegmentChangeName,
12
+ historyEventBelongsToChange,
13
+ } from './task-graph.mjs';
14
+
15
+ // 受控重入目标白名单:显式目标必须 ∈ 本集合 ∩ 协议 enabled 节点。
16
+ export const REENTRY_TARGET_NODE_IDS = new Set(['execute', 'subagent-execute', 'review', 'verify']);
17
+
18
+ // 每 change 轮次上限:3 轮;调用方禁止内联阈值,必须引用本常量。
19
+ export const REENTRY_ROUND_LIMIT = 3;
20
+
21
+ // 源判定(①):currentNode === 'archive' 且 activeChange 是合法的单段 change 名。
22
+ // 源不是 archive / 没有活跃 change / change 名形态非法 → 未授权来源,返回 machine-code,不抛异常。
23
+ export function reentrySourceDecision({ currentNode, activeChange } = {}) {
24
+ if (typeof activeChange !== 'string' || activeChange.trim() === '') {
25
+ return { ok: false, reason: 'no-active-change' };
26
+ }
27
+ if (!isSingleSegmentChangeName(activeChange)) {
28
+ return { ok: false, reason: 'change-name-invalid' };
29
+ }
30
+ if (currentNode !== 'archive') {
31
+ return { ok: false, reason: 'source-not-archive' };
32
+ }
33
+ return { ok: true, reason: 'archive-source' };
34
+ }
35
+
36
+ // 目标判定(②):target ∈ 白名单 ∩ 协议 enabled 节点。enabled 判定复用 protocolNodeEnabled
37
+ // (协议 enabled 语义唯一来源),本函数不写第二份解析;协议缺失 / 非对象按 fail-closed 返回未启用
38
+ // (解析失败不得静默放行)。
39
+ export function resolveReentryTarget({ protocol, target } = {}) {
40
+ if (typeof target !== 'string' || !REENTRY_TARGET_NODE_IDS.has(target)) {
41
+ return { ok: false, reason: 'target-not-allowed' };
42
+ }
43
+ if (!protocol || typeof protocol !== 'object' || !protocolNodeEnabled(protocol, target)) {
44
+ return { ok: false, reason: 'target-not-enabled' };
45
+ }
46
+ return { ok: true, reason: 'target-allowed', target };
47
+ }
48
+
49
+ // 授权形态 fail-closed 解析(③):round 为正整数、at / source 均为非空(去空白)字符串、
50
+ // authorization.target 与本次调用 target 一致;任一项非法 → 未授权,不抛异常。
51
+ // 形态与既有 Fix 轮次授权同源,禁止第二份校验。
52
+ export function parseReentryAuthorization({ authorization, target } = {}) {
53
+ if (!authorization || typeof authorization !== 'object' || Array.isArray(authorization)) {
54
+ return { ok: false, reason: 'authorization-missing' };
55
+ }
56
+ if (typeof authorization.round !== 'number' || !Number.isInteger(authorization.round) || authorization.round <= 0) {
57
+ return { ok: false, reason: 'round-invalid' };
58
+ }
59
+ if (typeof authorization.at !== 'string' || authorization.at.trim() === '') {
60
+ return { ok: false, reason: 'at-invalid' };
61
+ }
62
+ if (typeof authorization.source !== 'string' || authorization.source.trim() === '') {
63
+ return { ok: false, reason: 'source-invalid' };
64
+ }
65
+ if (typeof target !== 'string' || target === '' || authorization.target !== target) {
66
+ return { ok: false, reason: 'target-mismatch' };
67
+ }
68
+ return {
69
+ ok: true,
70
+ reason: 'authorized',
71
+ authorization: {
72
+ round: authorization.round,
73
+ at: authorization.at.trim(),
74
+ source: authorization.source.trim(),
75
+ target,
76
+ },
77
+ };
78
+ }
79
+
80
+ // 上限后的续轮授权形态:达到上限后,用户可显式授权把轮次推进到下一轮。授权三元组形态复用
81
+ // parseReentryAuthorization(单一权威——不复制第二份字段校验);本函数只追加续轮特有约束:
82
+ // round 必须覆盖即将执行的轮次(已用轮次 + 1,与既有 Fix 轮次授权的「覆盖即将执行轮次」语义同型),
83
+ // 否则视为未授权 fail-closed。未达上限时调用方不得传续轮授权(续轮只在人工裁决点需要)。
84
+ // 本函数只做形态判定,不读 history。
85
+ export function parseReentryContinuationAuthorization({ authorization, target, used } = {}) {
86
+ const parsed = parseReentryAuthorization({ authorization, target });
87
+ if (!parsed.ok) return parsed;
88
+ if (typeof used !== 'number' || !Number.isInteger(used) || used < 0
89
+ || parsed.authorization.round < used + 1) {
90
+ return { ok: false, reason: 'round-below-required' };
91
+ }
92
+ return { ok: true, reason: 'continuation-authorized', authorization: parsed.authorization };
93
+ }
94
+
95
+ // 轮次派生(④):统计 state.history 中本 change 的 reentry-applied 事件数,change 归属过滤调用
96
+ // historyEventBelongsToChange(单一权威——不在此内联第二份)。跨 change 隔离依赖事件的 change
97
+ // 字段:写入方(reenter 子命令)必须带 change;无 change 的旧事件按 legacy 形态参与(reentry-applied
98
+ // 为既有事件类型,legacy 形态实际不会出现)。history 缺失 / 非数组 = 无事件(旧 state 形态,0 轮)。
99
+ // 轮次只按事件条数派生,不读事件里的 round 字段。
100
+ export function reentryRoundCount({ history, changeName } = {}) {
101
+ if (!Array.isArray(history)) return 0;
102
+ let count = 0;
103
+ for (const event of history) {
104
+ if (!event || event.event !== 'reentry-applied') continue;
105
+ if (!historyEventBelongsToChange(event, changeName)) continue;
106
+ count += 1;
107
+ }
108
+ return count;
109
+ }
110
+
111
+ // 上限判定(⑤):已用轮次 < REENTRY_ROUND_LIMIT 或持有合法续轮授权 → 放行并给出本轮序号
112
+ // (nextRound = 已用 + 1,供审计事件与 REENTRY 行使用);否则 blocked(调用方必须先于任何 state
113
+ // 写盘返回,保证字节零改写)。阈值只从 REENTRY_ROUND_LIMIT 取。
114
+ export function reentryRoundDecision({ history, changeName, continuationAuthorized = false } = {}) {
115
+ const used = reentryRoundCount({ history, changeName });
116
+ if (used < REENTRY_ROUND_LIMIT) {
117
+ return { ok: true, reason: 'round-available', used, nextRound: used + 1, limit: REENTRY_ROUND_LIMIT };
118
+ }
119
+ if (continuationAuthorized === true) {
120
+ return { ok: true, reason: 'round-available-continuation', used, nextRound: used + 1, limit: REENTRY_ROUND_LIMIT };
121
+ }
122
+ return { ok: false, reason: 'round-limit-reached', used, limit: REENTRY_ROUND_LIMIT };
123
+ }
124
+
125
+ // 协议前驱推导(⑦ 的输入):沿协议 success 边反向可达的全部祖先节点(传递闭包——对应前驱语义
126
+ // 「清掉目标及其后继」),按协议 route 顺序(disabled 过滤)排序;failure / pause 自环、target
127
+ // 自身与协议外节点不参与。顺序唯一来源是 route(protocol),不另写节点排序。
128
+ function reentryPredecessors(protocol, target) {
129
+ if (!protocol || typeof protocol !== 'object') return [];
130
+ const order = route(protocol).map((node) => node.id);
131
+ const ancestors = new Set();
132
+ const queue = [target];
133
+ while (queue.length > 0) {
134
+ const current = queue.shift();
135
+ for (const edge of protocol.edges ?? []) {
136
+ if (!edge || edge.condition !== 'success' || edge.to !== current) continue;
137
+ if (typeof edge.from !== 'string' || edge.from === '' || edge.from === target || ancestors.has(edge.from)) continue;
138
+ ancestors.add(edge.from);
139
+ queue.push(edge.from);
140
+ }
141
+ }
142
+ return order.filter((id) => ancestors.has(id));
143
+ }
144
+
145
+ // 前驱交集(⑦):completedNodes ∩ predecessors(target),按协议节点顺序保留原顺序。completedNodes
146
+ // 中的协议外 / 重复项自然被过滤与去重;target 未知 / 未启用 → 空交集(fail-closed)。
147
+ export function reentryPredecessorIntersection({ protocol, target, completedNodes = [] } = {}) {
148
+ const completed = Array.isArray(completedNodes) ? completedNodes : [];
149
+ return reentryPredecessors(protocol, target).filter((id) => completed.includes(id));
150
+ }
151
+
152
+ // 空操作判定(⑥ · 幂等锚):currentNode === target 且 completedNodes 已等于前驱交集结果
153
+ // (同序逐项相等)→ noop(调用方不备份 / 不计数 / 不写事件);否则返回待落地的交集,调用方按
154
+ // 前驱语义覆盖 completedNodes。判定本身不改写输入。
155
+ export function reentryNoOpDecision({ currentNode, target, protocol, completedNodes = [] } = {}) {
156
+ const completed = Array.isArray(completedNodes) ? completedNodes : [];
157
+ const intersection = reentryPredecessorIntersection({ protocol, target, completedNodes: completed });
158
+ const same = currentNode === target
159
+ && completed.length === intersection.length
160
+ && intersection.every((id, index) => completed[index] === id);
161
+ return same
162
+ ? { noop: true, reason: 'noop-target-satisfied', completedNodes: intersection }
163
+ : { noop: false, reason: 'target-state-not-satisfied', completedNodes: intersection };
164
+ }
165
+
166
+ // 受控重入综合判定(单一权威聚合判定 · 供 reenter 子命令单点调用),返回三态 action:
167
+ // 'block'(ok:false,调用方零写盘返回)、
168
+ // 'noop' (目标形态已成立,调用方零写盘提示)、
169
+ // 'apply'(ok:true,携带 changeName / target / 待落地 completedNodes / 本轮 round / 归一授权)。
170
+ // 判定顺序:目标合法性(越界即使处于重复态也 BLOCK)→ change 名形态 → 上限 → 幂等短路 → 源 → 授权。
171
+ // 幂等短路必须先于源判定:目标形态已成立时 currentNode === target ≠ 'archive',先判源会让空操作
172
+ // 判定永远不可达;但幂等短路不得越过上限判定——达到上限的 change 即使处于同目标形态也必须走人工
173
+ // 裁决路径(不得以空操作退出 0),也不得越过 change 名形态判定(畸形 change 名一律 BLOCK,不因形态
174
+ // 巧合而短路)。短路分支零写盘。调用方不得内联重排或复制其中任何一条判据。
175
+ export function resolveReentryDecision({ protocol, state, target, authorization, continuationRound } = {}) {
176
+ const targetDecision = resolveReentryTarget({ protocol, target });
177
+ if (!targetDecision.ok) return { ok: false, action: 'block', reason: targetDecision.reason };
178
+ const activeChange = state?.activeChange;
179
+ if (typeof activeChange !== 'string' || activeChange.trim() === '') {
180
+ return { ok: false, action: 'block', reason: 'no-active-change' };
181
+ }
182
+ if (!isSingleSegmentChangeName(activeChange)) {
183
+ return { ok: false, action: 'block', reason: 'change-name-invalid' };
184
+ }
185
+ const used = reentryRoundCount({ history: state?.history, changeName: activeChange });
186
+ const atLimit = used >= REENTRY_ROUND_LIMIT;
187
+ let continuationAuthorized = false;
188
+ if (atLimit) {
189
+ const continuation = parseReentryContinuationAuthorization({
190
+ authorization: continuationRound === undefined || continuationRound === null
191
+ ? null
192
+ : {
193
+ ...(authorization && typeof authorization === 'object' && !Array.isArray(authorization) ? authorization : {}),
194
+ round: continuationRound,
195
+ },
196
+ target,
197
+ used,
198
+ });
199
+ if (!continuation.ok) {
200
+ return { ok: false, action: 'block', reason: 'round-limit-reached', used, limit: REENTRY_ROUND_LIMIT };
201
+ }
202
+ continuationAuthorized = true;
203
+ } else if (continuationRound !== undefined && continuationRound !== null) {
204
+ return { ok: false, action: 'block', reason: 'continuation-not-required', used, limit: REENTRY_ROUND_LIMIT };
205
+ }
206
+ const noop = reentryNoOpDecision({
207
+ currentNode: state?.currentNode,
208
+ target,
209
+ protocol,
210
+ completedNodes: state?.completedNodes,
211
+ });
212
+ if (noop.noop && !atLimit) {
213
+ return { ok: true, action: 'noop', reason: noop.reason, target, completedNodes: noop.completedNodes };
214
+ }
215
+ const source = reentrySourceDecision({
216
+ currentNode: state?.currentNode,
217
+ activeChange,
218
+ });
219
+ if (!source.ok) return { ok: false, action: 'block', reason: source.reason };
220
+ const parsed = parseReentryAuthorization({ authorization, target });
221
+ if (!parsed.ok) return { ok: false, action: 'block', reason: parsed.reason };
222
+ const round = reentryRoundDecision({ history: state?.history, changeName: activeChange, continuationAuthorized });
223
+ if (!round.ok) {
224
+ return { ok: false, action: 'block', reason: round.reason, used: round.used, limit: round.limit };
225
+ }
226
+ return {
227
+ ok: true,
228
+ action: 'apply',
229
+ reason: 'reentry-authorized',
230
+ changeName: activeChange,
231
+ target,
232
+ completedNodes: noop.completedNodes,
233
+ round: round.nextRound,
234
+ continuationAuthorized,
235
+ authorization: parsed.authorization,
236
+ };
237
+ }
@@ -0,0 +1,209 @@
1
+ // replan-node.mjs — 受控计划重校重签判定(replan · 与 reentry 族同构 · 纯函数)
2
+ // 计划在执行中被证明有缺陷时的受控通道:仅 execute / subagent-execute 相位可用,停原位重校重签
3
+ // (不跳节点、不改 completedNodes)。相位与 change 名 / 授权形态 / 轮次上限 / 幂等空操作的全部判定
4
+ // 集中在本模块:replan 子命令只做编排与落盘,禁止内联第二份。
5
+ // 纯函数:无 fs / console / process.exit / state 写入。信号统一用 structured result:非法 / 未授权
6
+ // 返回 { ok:false, action:'block', reason:'<machine-code>' },不抛异常。
7
+ // 依赖方向:只依赖 task-graph.mjs(协议节点启用 / change 名形态 / 事件归属 / 任务集签名与版本语义)。
8
+ import {
9
+ protocolNodeEnabled,
10
+ isSingleSegmentChangeName,
11
+ historyEventBelongsToChange,
12
+ taskSetSignature,
13
+ sameTaskSetSignature,
14
+ } from './task-graph.mjs';
15
+
16
+ // 可调用相位白名单:execute 家族两个节点;调用方不得内联。
17
+ export const REPLAN_PHASE_NODE_IDS = new Set(['execute', 'subagent-execute']);
18
+
19
+ // 每 change 轮次上限:与 reentry 的轮次上限同值、语义独立;调用方禁止内联阈值,必须引用本常量。
20
+ export const REPLAN_ROUND_LIMIT = 3;
21
+
22
+ // 相位 / change 名判定(步骤 1):currentNode ∈ execute 家族 ∩ 协议 enabled;activeChange 必须是
23
+ // 合法单段 change 名(复用 isSingleSegmentChangeName)。协议缺失 / 非对象 / 节点未启用一律
24
+ // fail-closed(解析失败不得静默放行)。
25
+ export function replanSourceDecision({ protocol, currentNode, activeChange } = {}) {
26
+ if (typeof activeChange !== 'string' || activeChange.trim() === '') {
27
+ return { ok: false, reason: 'no-active-change' };
28
+ }
29
+ if (!isSingleSegmentChangeName(activeChange)) {
30
+ return { ok: false, reason: 'change-name-invalid' };
31
+ }
32
+ if (!REPLAN_PHASE_NODE_IDS.has(currentNode)
33
+ || !protocol || typeof protocol !== 'object'
34
+ || !protocolNodeEnabled(protocol, currentNode)) {
35
+ return { ok: false, reason: 'phase-not-allowed' };
36
+ }
37
+ return { ok: true, reason: 'replan-phase' };
38
+ }
39
+
40
+ // 授权形态 fail-closed 解析(步骤 2):round 为正整数,at / source / reason 均为非空(去空白)
41
+ // 字符串,authorization.node 与调用相位一致;任一项非法 → 未授权,不抛异常。
42
+ // 形态与 reentry 授权(parseReentryAuthorization)同族,禁止第二份字段校验。
43
+ export function parseReplanAuthorization({ authorization, node } = {}) {
44
+ if (!authorization || typeof authorization !== 'object' || Array.isArray(authorization)) {
45
+ return { ok: false, reason: 'authorization-missing' };
46
+ }
47
+ if (typeof authorization.round !== 'number' || !Number.isInteger(authorization.round) || authorization.round <= 0) {
48
+ return { ok: false, reason: 'round-invalid' };
49
+ }
50
+ if (typeof authorization.at !== 'string' || authorization.at.trim() === '') {
51
+ return { ok: false, reason: 'at-invalid' };
52
+ }
53
+ if (typeof authorization.source !== 'string' || authorization.source.trim() === '') {
54
+ return { ok: false, reason: 'source-invalid' };
55
+ }
56
+ if (typeof authorization.reason !== 'string' || authorization.reason.trim() === '') {
57
+ return { ok: false, reason: 'reason-invalid' };
58
+ }
59
+ if (typeof node !== 'string' || node === '' || authorization.node !== node) {
60
+ return { ok: false, reason: 'node-mismatch' };
61
+ }
62
+ return {
63
+ ok: true,
64
+ reason: 'authorized',
65
+ authorization: {
66
+ round: authorization.round,
67
+ at: authorization.at.trim(),
68
+ source: authorization.source.trim(),
69
+ reason: authorization.reason.trim(),
70
+ node,
71
+ },
72
+ };
73
+ }
74
+
75
+ // 上限后的续轮授权形态(步骤 3):授权形态复用 parseReplanAuthorization(单一权威——不复制第二份
76
+ // 字段校验);本函数只追加续轮特有约束:round 必须覆盖即将执行的轮次(已用轮次 + 1,与 reentry
77
+ // 续轮同语义),否则视为未授权 fail-closed。未达上限时调用方不得传续轮授权。本函数只做形态判定,
78
+ // 不读 history。
79
+ export function parseReplanContinuationAuthorization({ authorization, node, used } = {}) {
80
+ const parsed = parseReplanAuthorization({ authorization, node });
81
+ if (!parsed.ok) return parsed;
82
+ if (typeof used !== 'number' || !Number.isInteger(used) || used < 0
83
+ || parsed.authorization.round < used + 1) {
84
+ return { ok: false, reason: 'round-below-required' };
85
+ }
86
+ return { ok: true, reason: 'continuation-authorized', authorization: parsed.authorization };
87
+ }
88
+
89
+ // 轮次派生(步骤 3):统计 state.history 中本 change 的 replan-applied 事件数;change 归属过滤复用
90
+ // historyEventBelongsToChange(单一权威)。跨 change 隔离依赖事件 change 字段;无 change 字段的旧
91
+ // 事件按 legacy 形态参与(replan-applied 为既有事件类型,legacy 形态实际不会出现)。history 缺失 /
92
+ // 非数组 = 无事件(旧 state 形态,0 轮)。
93
+ export function replanRoundCount({ history, changeName } = {}) {
94
+ if (!Array.isArray(history)) return 0;
95
+ let count = 0;
96
+ for (const event of history) {
97
+ if (!event || event.event !== 'replan-applied') continue;
98
+ if (!historyEventBelongsToChange(event, changeName)) continue;
99
+ count += 1;
100
+ }
101
+ return count;
102
+ }
103
+
104
+ // 上限判定(步骤 3):已用轮次 < REPLAN_ROUND_LIMIT 或持有合法续轮授权 → 放行并给出本轮序号
105
+ // (nextRound = 已用 + 1,供审计事件与 REPLAN 行使用);否则 blocked(调用方必须先于任何 state
106
+ // 写盘返回,保证字节零改写)。阈值只从 REPLAN_ROUND_LIMIT 取。
107
+ export function replanRoundDecision({ history, changeName, continuationAuthorized = false } = {}) {
108
+ const used = replanRoundCount({ history, changeName });
109
+ if (used < REPLAN_ROUND_LIMIT) {
110
+ return { ok: true, reason: 'round-available', used, nextRound: used + 1, limit: REPLAN_ROUND_LIMIT };
111
+ }
112
+ if (continuationAuthorized === true) {
113
+ return { ok: true, reason: 'round-available-continuation', used, nextRound: used + 1, limit: REPLAN_ROUND_LIMIT };
114
+ }
115
+ return { ok: false, reason: 'round-limit-reached', used, limit: REPLAN_ROUND_LIMIT };
116
+ }
117
+
118
+ // 空操作判定(步骤 4):目标形态已成立 = state.taskHash 已与当前 TASK.md 签名同签(复用
119
+ // sameTaskSetSignature / taskSetSignature 既有版本语义——算法版本偏斜按不可比处理,不误拦)
120
+ // → noop(调用方零备份 / 零轮次 / 零事件 / 零改写)。判定本身不改写输入;返回 taskSetSignature
121
+ // 供调用方重签 / 写审计事件(避免第二份签名计算导致漂移)。
122
+ // 依赖缺失 fail-closed:taskContent 缺失 / 非字符串 / 空串 → 不允许判空操作(缺 TASK.md 时签名
123
+ // 退化为空任务集,放行会掩盖「文件不可读」),由调用方按重校失败 BLOCK。
124
+ export function replanNoOpDecision({ state, taskContent } = {}) {
125
+ if (typeof taskContent !== 'string' || taskContent === '') {
126
+ return { noop: false, reason: 'task-content-missing', taskSetSignature: null };
127
+ }
128
+ const signature = taskSetSignature(taskContent);
129
+ const same = sameTaskSetSignature(state?.taskHash, signature);
130
+ return same
131
+ ? { noop: true, reason: 'noop-target-satisfied', taskSetSignature: signature }
132
+ : { noop: false, reason: 'target-state-not-satisfied', taskSetSignature: signature };
133
+ }
134
+
135
+ // 受控重校综合判定(单一权威聚合判定 · 供 replan 子命令单点调用),返回三态 action:
136
+ // 'block'(ok:false,调用方零写盘返回)、
137
+ // 'noop' (目标形态已成立,调用方零写盘提示;仅当调用方传入 taskContent 时参与聚合)、
138
+ // 'apply'(ok:true,携带 changeName / node / 本轮 round / 归一授权;taskContent 在场时附带
139
+ // 当前任务集签名,供调用方直接重签 / 写事件)。
140
+ // 判定顺序:相位与 change 名(步骤 1)→ 授权形态(步骤 2)→ 上限与续轮(步骤 3)→ 幂等短路
141
+ // (步骤 4);任何 block 分支都先于 state 写盘,保证字节零改写。幂等短路不得越过上限判定——
142
+ // 达到上限的 change 未持显式续轮授权时一律走人工裁决路径(与 reentry 族判定顺序同构)。
143
+ // taskContent 为可选入参:提供时在授权 / 轮次通过后聚合空操作判定(缺失 / 非字符串 / 空串
144
+ // → block,fail-closed);未提供时返回 apply/block 两态,由调用方单独调用 replanNoOpDecision
145
+ // (两处判定同源,调用方不得复制判据)。
146
+ export function resolveReplanDecision({ protocol, state, authorization, continuationRound, taskContent } = {}) {
147
+ const currentChange = state?.activeChange;
148
+ const node = state?.currentNode;
149
+ const source = replanSourceDecision({ protocol, currentNode: node, activeChange: currentChange });
150
+ if (!source.ok) return { ok: false, action: 'block', reason: source.reason };
151
+ const parsed = parseReplanAuthorization({ authorization, node });
152
+ if (!parsed.ok) return { ok: false, action: 'block', reason: parsed.reason };
153
+ const used = replanRoundCount({ history: state?.history, changeName: currentChange });
154
+ const atLimit = used >= REPLAN_ROUND_LIMIT;
155
+ let continuationAuthorized = false;
156
+ if (atLimit) {
157
+ const continuation = parseReplanContinuationAuthorization({
158
+ authorization: continuationRound === undefined || continuationRound === null
159
+ ? null
160
+ : {
161
+ ...(authorization && typeof authorization === 'object' && !Array.isArray(authorization) ? authorization : {}),
162
+ round: continuationRound,
163
+ },
164
+ node,
165
+ used,
166
+ });
167
+ if (!continuation.ok) {
168
+ return { ok: false, action: 'block', reason: 'round-limit-reached', used, limit: REPLAN_ROUND_LIMIT };
169
+ }
170
+ continuationAuthorized = true;
171
+ } else if (continuationRound !== undefined && continuationRound !== null) {
172
+ return { ok: false, action: 'block', reason: 'continuation-not-required', used, limit: REPLAN_ROUND_LIMIT };
173
+ }
174
+ if (taskContent !== undefined) {
175
+ if (typeof taskContent !== 'string' || taskContent === '') {
176
+ return { ok: false, action: 'block', reason: 'task-content-missing' };
177
+ }
178
+ const noop = replanNoOpDecision({ state, taskContent });
179
+ if (noop.noop) {
180
+ return {
181
+ ok: true,
182
+ action: 'noop',
183
+ reason: noop.reason,
184
+ changeName: currentChange,
185
+ node,
186
+ taskSetSignature: noop.taskSetSignature,
187
+ };
188
+ }
189
+ }
190
+ const round = replanRoundDecision({
191
+ history: state?.history,
192
+ changeName: currentChange,
193
+ continuationAuthorized,
194
+ });
195
+ if (!round.ok) {
196
+ return { ok: false, action: 'block', reason: round.reason, used: round.used, limit: round.limit };
197
+ }
198
+ return {
199
+ ok: true,
200
+ action: 'apply',
201
+ reason: 'replan-authorized',
202
+ changeName: currentChange,
203
+ node,
204
+ round: round.nextRound,
205
+ continuationAuthorized,
206
+ authorization: parsed.authorization,
207
+ ...(taskContent !== undefined ? { taskSetSignature: taskSetSignature(taskContent) } : {}),
208
+ };
209
+ }
@@ -0,0 +1,49 @@
1
+ // E1 分支模式辅助族(单一归属):git 仓库检测、分支名与分支存在性、状态行的分支一致性提示。
2
+ import { execFileSync } from 'child_process';
3
+ import { runRootDir } from './state-io.mjs';
4
+
5
+ // git 仓库检测:`git rev-parse --is-inside-work-tree` 成功且输出 true
6
+ export function isInsideWorkTree() {
7
+ try {
8
+ const out = execFileSync('git', ['rev-parse', '--is-inside-work-tree'], { cwd: runRootDir(), stdio: 'pipe', encoding: 'utf8' });
9
+ return String(out).trim() === 'true';
10
+ } catch {
11
+ return false;
12
+ }
13
+ }
14
+
15
+ // 当前分支名;非 git 仓库 / detached HEAD 失败 → null
16
+ export function gitBranchName() {
17
+ try {
18
+ const out = execFileSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: runRootDir(), stdio: 'pipe', encoding: 'utf8' });
19
+ return String(out).trim() || null;
20
+ } catch {
21
+ return null;
22
+ }
23
+ }
24
+
25
+ // 分支是否存在(本地分支)
26
+ export function branchExists(name) {
27
+ try {
28
+ const out = execFileSync('git', ['branch', '--format', '%(refname:short)'], { cwd: runRootDir(), stdio: 'pipe', encoding: 'utf8' });
29
+ return String(out).split('\n').map(s => s.trim()).includes(name);
30
+ } catch {
31
+ return false;
32
+ }
33
+ }
34
+
35
+ // E1: status/next 追加分支信息——BRANCH: <当前分支> | 一致性: ok|mismatch
36
+ // mismatch(activeChange 存在但当前分支不是 <前缀><activeChange>)→ WARN 不 BLOCK;非 git 仓库 → BRANCH: none
37
+ export function printBranchLine(activeChange, branchPrefix = 'change/') {
38
+ const branch = gitBranchName();
39
+ if (branch === null) {
40
+ console.log('BRANCH: none');
41
+ return;
42
+ }
43
+ const expected = branchPrefix + activeChange;
44
+ const consistent = branch === expected;
45
+ console.log('BRANCH: ' + branch + ' | 一致性: ' + (consistent ? 'ok' : 'mismatch'));
46
+ if (!consistent) {
47
+ console.error('WARN: 分支与 activeChange 不一致——先 git checkout ' + expected + ' 再继续');
48
+ }
49
+ }