flow-comet 1.6.3 → 1.6.5

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 (106) hide show
  1. package/.flow-comet/skills/flow-comet/INSTALLED_VERSION +1 -1
  2. package/.flow-comet/skills/flow-comet/SKILL.md +21 -3
  3. package/.flow-comet/skills/flow-comet/reference/artifact-template-overlay.md +154 -0
  4. package/.flow-comet/skills/flow-comet/reference/artifact-templates.json +387 -0
  5. package/.flow-comet/skills/flow-comet/reference/entry-detail.md +8 -2
  6. package/.flow-comet/skills/flow-comet/reference/workflow-protocol.json +22 -0
  7. package/.flow-comet/skills/flow-comet/scripts/comet-hook-guard.mjs +11 -13
  8. package/.flow-comet/skills/flow-comet/scripts/context-init.mjs +2 -2
  9. package/.flow-comet/skills/flow-comet/scripts/evolve.mjs +180 -6
  10. package/.flow-comet/skills/flow-comet/scripts/guard-self-test.mjs +69 -18878
  11. package/.flow-comet/skills/flow-comet/scripts/health.mjs +29 -5
  12. package/.flow-comet/skills/flow-comet/scripts/items/a.mjs +1053 -0
  13. package/.flow-comet/skills/flow-comet/scripts/items/b.mjs +830 -0
  14. package/.flow-comet/skills/flow-comet/scripts/items/c.mjs +781 -0
  15. package/.flow-comet/skills/flow-comet/scripts/items/d.mjs +99 -0
  16. package/.flow-comet/skills/flow-comet/scripts/items/e.mjs +185 -0
  17. package/.flow-comet/skills/flow-comet/scripts/items/f.mjs +90 -0
  18. package/.flow-comet/skills/flow-comet/scripts/items/g.mjs +59 -0
  19. package/.flow-comet/skills/flow-comet/scripts/items/h.mjs +817 -0
  20. package/.flow-comet/skills/flow-comet/scripts/items/i.mjs +77 -0
  21. package/.flow-comet/skills/flow-comet/scripts/items/j.mjs +75 -0
  22. package/.flow-comet/skills/flow-comet/scripts/items/k.mjs +1427 -0
  23. package/.flow-comet/skills/flow-comet/scripts/items/l.mjs +275 -0
  24. package/.flow-comet/skills/flow-comet/scripts/items/m.mjs +618 -0
  25. package/.flow-comet/skills/flow-comet/scripts/items/n.mjs +154 -0
  26. package/.flow-comet/skills/flow-comet/scripts/lib/count-derivation.mjs +97 -0
  27. package/.flow-comet/skills/flow-comet/scripts/lib/fix-loop-node.mjs +196 -0
  28. package/.flow-comet/skills/flow-comet/scripts/lib/guard-artifact-skeleton.mjs +406 -0
  29. package/.flow-comet/skills/flow-comet/scripts/lib/guard-evidence-gates.mjs +756 -0
  30. package/.flow-comet/skills/flow-comet/scripts/lib/guard-gates.mjs +1235 -0
  31. package/.flow-comet/skills/flow-comet/scripts/lib/guard-routing.mjs +384 -0
  32. package/.flow-comet/skills/flow-comet/scripts/lib/guard-state-io.mjs +404 -0
  33. package/.flow-comet/skills/flow-comet/scripts/lib/guard-template-fidelity.mjs +714 -0
  34. package/.flow-comet/skills/flow-comet/scripts/lib/guard-uidesign.mjs +204 -0
  35. package/.flow-comet/skills/flow-comet/scripts/lib/reentry-node.mjs +237 -0
  36. package/.flow-comet/skills/flow-comet/scripts/lib/replan-node.mjs +209 -0
  37. package/.flow-comet/skills/flow-comet/scripts/lib/state-branch.mjs +49 -0
  38. package/.flow-comet/skills/flow-comet/scripts/lib/state-bridgecheck.mjs +201 -0
  39. package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-admin.mjs +179 -0
  40. package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-flow.mjs +714 -0
  41. package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-recovery.mjs +578 -0
  42. package/.flow-comet/skills/flow-comet/scripts/lib/state-determine.mjs +52 -0
  43. package/.flow-comet/skills/flow-comet/scripts/lib/state-evolve-hint.mjs +55 -0
  44. package/.flow-comet/skills/flow-comet/scripts/lib/state-explain.mjs +123 -0
  45. package/.flow-comet/skills/flow-comet/scripts/lib/state-intelscan.mjs +58 -0
  46. package/.flow-comet/skills/flow-comet/scripts/lib/state-io.mjs +147 -0
  47. package/.flow-comet/skills/flow-comet/scripts/lib/state-skillmarkers.mjs +249 -0
  48. package/.flow-comet/skills/flow-comet/scripts/lib/system-fixtures.mjs +1505 -0
  49. package/.flow-comet/skills/flow-comet/scripts/lib/task-graph.mjs +471 -0
  50. package/.flow-comet/skills/flow-comet/scripts/lib/test-assertions.mjs +509 -0
  51. package/.flow-comet/skills/flow-comet/scripts/lib/test-fixtures.mjs +1248 -0
  52. package/.flow-comet/skills/flow-comet/scripts/lib/test-helpers.mjs +237 -0
  53. package/.flow-comet/skills/flow-comet/scripts/lib/text-anchor-checks.mjs +2493 -0
  54. package/.flow-comet/skills/flow-comet/scripts/protocol-utils.mjs +214 -2
  55. package/.flow-comet/skills/flow-comet/scripts/route-node.mjs +72 -1123
  56. package/.flow-comet/skills/flow-comet/scripts/scenarios/a.mjs +976 -0
  57. package/.flow-comet/skills/flow-comet/scripts/scenarios/b.mjs +1233 -0
  58. package/.flow-comet/skills/flow-comet/scripts/scenarios/c.mjs +1382 -0
  59. package/.flow-comet/skills/flow-comet/scripts/scenarios/d.mjs +483 -0
  60. package/.flow-comet/skills/flow-comet/scripts/scenarios/e.mjs +1398 -0
  61. package/.flow-comet/skills/flow-comet/scripts/scenarios/f.mjs +1466 -0
  62. package/.flow-comet/skills/flow-comet/scripts/scenarios/g.mjs +1367 -0
  63. package/.flow-comet/skills/flow-comet/scripts/scenarios/h.mjs +1287 -0
  64. package/.flow-comet/skills/flow-comet/scripts/scenarios/i.mjs +1436 -0
  65. package/.flow-comet/skills/flow-comet/scripts/scenarios/j.mjs +1384 -0
  66. package/.flow-comet/skills/flow-comet/scripts/scenarios/k.mjs +1338 -0
  67. package/.flow-comet/skills/flow-comet/scripts/scenarios/l.mjs +1492 -0
  68. package/.flow-comet/skills/flow-comet/scripts/scenarios/m.mjs +971 -0
  69. package/.flow-comet/skills/flow-comet/scripts/scenarios/n.mjs +2125 -0
  70. package/.flow-comet/skills/flow-comet/scripts/state-schema.mjs +1 -1
  71. package/.flow-comet/skills/flow-comet/scripts/system-test.mjs +59 -7559
  72. package/.flow-comet/skills/flow-comet/scripts/time-utils.mjs +4 -4
  73. package/.flow-comet/skills/flow-comet/scripts/workflow-guard.mjs +223 -2476
  74. package/.flow-comet/skills/flow-comet/scripts/workflow-handoff.mjs +5 -5
  75. package/.flow-comet/skills/flow-comet/scripts/workflow-state.mjs +27 -2190
  76. package/.flow-comet/skills/flow-comet-archive/SKILL.md +6 -2
  77. package/.flow-comet/skills/flow-comet-change/SKILL.md +6 -2
  78. package/.flow-comet/skills/flow-comet-design/GUIDANCE.md +13 -3
  79. package/.flow-comet/skills/flow-comet-design/SKILL.md +15 -8
  80. package/.flow-comet/skills/flow-comet-dev/SKILL.md +1 -1
  81. package/.flow-comet/skills/flow-comet-evolve/SKILL.md +1 -0
  82. package/.flow-comet/skills/flow-comet-execute/SKILL.md +13 -10
  83. package/.flow-comet/skills/flow-comet-health/SKILL.md +1 -1
  84. package/.flow-comet/skills/flow-comet-integration/SKILL.md +1 -1
  85. package/.flow-comet/skills/flow-comet-open/GUIDANCE.md +14 -4
  86. package/.flow-comet/skills/flow-comet-open/SKILL.md +11 -9
  87. package/.flow-comet/skills/flow-comet-plan/GUIDANCE.md +13 -3
  88. package/.flow-comet/skills/flow-comet-plan/SKILL.md +11 -7
  89. package/.flow-comet/skills/flow-comet-requirement/SKILL.md +1 -1
  90. package/.flow-comet/skills/flow-comet-review/SKILL.md +15 -9
  91. package/.flow-comet/skills/flow-comet-subagent-execute/GUIDANCE.md +13 -3
  92. package/.flow-comet/skills/flow-comet-subagent-execute/SKILL.md +11 -8
  93. package/.flow-comet/skills/flow-comet-task/SKILL.md +1 -1
  94. package/.flow-comet/skills/flow-comet-test/SKILL.md +6 -2
  95. package/.flow-comet/skills/flow-comet-ui-design/SKILL.md +2 -2
  96. package/.flow-comet/skills/flow-comet-verify/SKILL.md +12 -8
  97. package/README.md +52 -69
  98. package/package.json +3 -2
  99. package/scripts/dsh-bridge.mjs +2 -2
  100. package/scripts/lib/installer-cleanup.mjs +152 -0
  101. package/scripts/lib/installer-flowkit.mjs +159 -0
  102. package/scripts/lib/installer-hooks.mjs +363 -0
  103. package/scripts/lib/installer-loader.mjs +220 -0
  104. package/scripts/lib/installer-migrate.mjs +236 -0
  105. package/scripts/lib/installer-platforms.mjs +314 -0
  106. package/scripts/prepare-env.mjs +21 -1313
@@ -0,0 +1,204 @@
1
+ // ui-design 适用性门控(前端判据 · 结构级,不做语义)。
2
+ //
3
+ // 协议 design 节点的 ui-design 绑定为 guarded,但它「仅前端」适用 ⇒ 出口自动补 required-skill
4
+ // 声明对该绑定失效(须真实 skill-load 声明),判据不成立(非前端)时按 advisory 处理并在出口输出
5
+ // 可见跳过行(不留假绿:跳过是显式记录,非静默省略)。
6
+ //
7
+ // 键控(enforcement 是这道门的单一表达处):门只在「协议登记了该绑定 ∧ enforcement === 'guarded'」
8
+ // 时存在——协议副本删掉绑定或把等级调回 advisory ⇒ 门不适用(留可见「门未启用」行,不静默消失),
9
+ // 相应路径逐字回到未登记绑定的原语义。门禁侧不持有第二份键表:登记表 APPLICABILITY_GATED_BINDINGS
10
+ // 按 `<nodeId>.<skill>` 键控,工件门与出口自动补写共用 applicabilityGatedBinding 查询。
11
+ //
12
+ // 判据必须结构性可判:CHANGE.md 的「视觉调性」段在场且段内不含**结构形态**的「不适用」标记 ⇒
13
+ // 前端;段内以结构形态标注 ⇒ 非前端。段**缺席**只在旧 change 上沿用「判非前端」的兼容口径——
14
+ // 新 change 由 open 出口按模板条件段拦(标题必须在场),此处缺席即拦,不再默认判非前端
15
+ // (否则「漏写段」会被静默翻译成一个更弱的门)。
16
+ //
17
+ // UI-DESIGN.md 在场时,其模板结构(frontmatter 8 键 / H2 骨架 / 结构型 H3)由产出物模板结构门禁
18
+ // 统一校验(与七类产出物同一实现,不在这里另写一份段名校验)。
19
+
20
+ import { promises as fs } from 'fs';
21
+ import path from 'path';
22
+ import { runRoot, fileExists } from './guard-state-io.mjs';
23
+ import { isNewChange, templateSectionPatterns, TEMPLATE_FALLBACK_SECTION_PATTERNS } from './guard-routing.mjs';
24
+ import { assessArtifact, evaluateArtifact, reportArtifactStructure } from './guard-template-fidelity.mjs';
25
+
26
+ export const UI_DESIGN_SKILL = 'flow-comet-ui-design';
27
+ const APPLICABILITY_GATED_BINDINGS = new Set(['design.' + UI_DESIGN_SKILL]);
28
+ const VISUAL_TONE_NOT_APPLICABLE = '不适用';
29
+
30
+ // 负向标记的结构约束(三种形态,共用同一词形边界)——裸子串会让「不适用于暗色主题」这类自然句子
31
+ // 关掉整道门(fail-open),故标记必须落在结构锚位且成词形边界:
32
+ // 形态 A 独立行:整行(去块引用 / 列表符 / 加粗)就是标记,可带括注理由与尾标点;
33
+ // 形态 B 行首:行以标记开头(如「不适用 —— CLI 工具,无用户可见界面。」);
34
+ // 形态 C 字段值位:`<适用性标签>: 不适用`——标签限本约定的短标签表(适用性 / 前端 / 视觉调性 …),
35
+ // 不通配任意字段名:`- 暗色主题:不适用` 这类「某子维度不适用」的字段不得关掉整道门。
36
+ // 词形边界 = 标记后为行尾 / 空白 / 标点 / 括注引号——「不适用于…」的「于」不是边界。
37
+ const TONE_MARKER_TAIL = String.raw`(?=$|[\s,。;、,;.::!!??…—()()【】「」『』“”"'])`;
38
+ const TONE_MARKER_LEAD = String.raw`^[>\s]*(?:[-*+]\s+)?(?:\*\*)?`;
39
+ const TONE_MARKER_FIELD_LABELS = '适用性|适用范围|是否前端|视觉调性|前端|界面|适用';
40
+ const TONE_MARKER_ALONE_LINE = new RegExp(TONE_MARKER_LEAD + VISUAL_TONE_NOT_APPLICABLE
41
+ + String.raw`(?:\*\*)?(?:\s*[((][^))]*[))])?[。.;;,,]?\s*$`);
42
+ const TONE_MARKER_LINE_HEAD = new RegExp(TONE_MARKER_LEAD + VISUAL_TONE_NOT_APPLICABLE
43
+ + String.raw`(?:\*\*)?` + TONE_MARKER_TAIL);
44
+ const TONE_MARKER_FIELD_VALUE = new RegExp(String.raw`(?:^|[\s|])(?:\*\*)?(?:`
45
+ + TONE_MARKER_FIELD_LABELS + String.raw`)(?:\*\*)?\s*[::]\s*(?:\*\*)?`
46
+ + VISUAL_TONE_NOT_APPLICABLE + String.raw`(?:\*\*)?` + TONE_MARKER_TAIL);
47
+
48
+ // 「视觉调性」段读取四态(区分「CHANGE.md 不存在」「模板整份缺失」「段缺席」「段在场」——四者判据
49
+ // 不同:文件不存在时本门没有输入,交给其余门禁处理;模板整份缺失时段名无从派生(no-basis,按
50
+ // 「未校验」显式跳过);段缺席才按 newChange 分级,C10)。
51
+ async function visualToneSectionState(changeName) {
52
+ if (!changeName) return { state: 'file-missing', body: null };
53
+ let text;
54
+ try {
55
+ text = await fs.readFile(path.join(runRoot, '.specs', changeName, 'CHANGE.md'), 'utf8');
56
+ } catch {
57
+ return { state: 'file-missing', body: null };
58
+ }
59
+ const headingPattern = (await templateSectionPatterns())
60
+ .pick('change', ['视觉调性'], TEMPLATE_FALLBACK_SECTION_PATTERNS.changeVisualTone);
61
+ // 整份模板缺席时 pick 返回可判别的跳过判据(TEMPLATE_SECTION_SKIP:skip 标记 + `.test` 恒真)。
62
+ // 不能拿它扫描:`.test` 恒真会让 CHANGE.md 的**首个小节**被当成「视觉调性」段(错的输入 = 错的
63
+ // 判据)。此时段名无从派生,显式报 no-basis 由调用方按「未校验(模板缺失)」处理;模板在场时
64
+ // pick 返回正则或 fallback,行为不变。
65
+ if (headingPattern !== null && typeof headingPattern === 'object' && headingPattern.skip === true) {
66
+ return { state: 'no-basis', body: null };
67
+ }
68
+ let body = null;
69
+ for (const line of String(text).replace(/\uFEFF/g, '').split(/\r?\n/)) {
70
+ if (/^##\s+/.test(line)) {
71
+ if (body !== null) break; // 到达下一个 H2 → 本段结束
72
+ if (headingPattern.test(line)) body = '';
73
+ continue;
74
+ }
75
+ if (body !== null) body += line + '\n';
76
+ }
77
+ return body === null ? { state: 'section-missing', body: null } : { state: 'present', body };
78
+ }
79
+
80
+ // 段内负向标记命中(逐行判定——判据限段内):命中 → 返回命中行片段(去空白 + 超长截断),未命中
81
+ // → null。片段回显进出口的跳过行,跳过因此自证(不是「段内某处出现过什么字面」)。
82
+ function visualToneNotApplicableHit(body) {
83
+ for (const rawLine of String(body).split(/\r?\n/)) {
84
+ const line = rawLine.trim();
85
+ if (line === '') continue;
86
+ if (TONE_MARKER_ALONE_LINE.test(line)
87
+ || TONE_MARKER_LINE_HEAD.test(line)
88
+ || TONE_MARKER_FIELD_VALUE.test(line)) {
89
+ return line.length > 60 ? line.slice(0, 60) + '…' : line;
90
+ }
91
+ }
92
+ return null;
93
+ }
94
+
95
+ // 登记表查询(唯一键表,键形 `<nodeId>.<skill>`):该节点 + 技能是否受适用性门控
96
+ function isRegisteredApplicabilityBinding(node, skill) {
97
+ return APPLICABILITY_GATED_BINDINGS.has(node.id + '.' + skill);
98
+ }
99
+
100
+ // 适用性门控绑定的单一查询:在场 ∧ enforcement === 'guarded' ∧ 登记在表 → 协议绑定;否则 null。
101
+ // 工件门、出口自动补写与缺声明恢复指引共用本查询——enforcement 是该门的单一表达处。
102
+ export function applicabilityGatedBinding(node, skill) {
103
+ if (!isRegisteredApplicabilityBinding(node, skill)) return null;
104
+ return (node.requiredSkillCalls ?? []).find((call) => call.skill === skill && call.enforcement === 'guarded') ?? null;
105
+ }
106
+
107
+ // 前端判据:{ applicable, reason, toneState }——applicable=true 表示本 change 判为前端项目。
108
+ // toneState 让调用方区分「段缺席」「模板整份缺失(no-basis,判据无依据)」与「段内标注不适用」:
109
+ // 段缺席在新 change 上必须拦(不能静默降级成非前端),最后一种是执行者显式声明,直接放行;
110
+ // no-basis 按「未校验」显式跳过,由调用方决定如何发声。
111
+ export async function frontendCriterion(changeName) {
112
+ const tone = await visualToneSectionState(changeName);
113
+ if (tone.state === 'file-missing') {
114
+ return { applicable: false, reason: 'CHANGE.md 不存在(本门无输入)', toneState: tone.state };
115
+ }
116
+ if (tone.state === 'no-basis') {
117
+ // 模板整份缺失 ⇒ 段名无从派生,本判据没有输入依据。不猜(不用首个小节冒充「视觉调性」段),
118
+ // 也不判前端:交给调用方按「未校验(模板缺失)」显式跳过(未验证 ≠ 通过,C9)。
119
+ return {
120
+ applicable: false,
121
+ reason: '「视觉调性」段判据无可依据(模板整份缺失,段名无从派生)',
122
+ toneState: tone.state,
123
+ };
124
+ }
125
+ if (tone.state === 'section-missing') {
126
+ return { applicable: false, reason: '「视觉调性」段缺席', toneState: tone.state };
127
+ }
128
+ const hit = visualToneNotApplicableHit(tone.body);
129
+ if (hit !== null) {
130
+ return {
131
+ applicable: false,
132
+ reason: '「视觉调性」段标注「不适用」(判非前端;命中片段: ' + hit + ')',
133
+ toneState: tone.state,
134
+ };
135
+ }
136
+ return { applicable: true, reason: '「视觉调性」段在场且未标注「不适用」(判前端;无结构形态标记)', toneState: tone.state };
137
+ }
138
+
139
+ // design 出口 ui-design 工件门(按协议绑定键控:登记 ∧ 等级 = guarded ∧ 判据成立)——判据成立 ⇒
140
+ // 要求 UI-DESIGN.md 在场:新 change 缺件 BLOCKED + 恢复指引,旧 change WARN 渐进不阻断(不静默
141
+ // 放行);判据不成立(非前端)⇒ 可见跳过行(含命中片段)后照常放行;协议删掉该绑定或等级非
142
+ // guarded ⇒ 门未启用(同样可见输出),行为回到未登记绑定的原语义——闸门开关由协议表达。
143
+ export async function checkUiDesignArtifact(node, state) {
144
+ if (!state.activeChange || !isRegisteredApplicabilityBinding(node, UI_DESIGN_SKILL)) return;
145
+ if (!applicabilityGatedBinding(node, UI_DESIGN_SKILL)) {
146
+ const protocolState = (node.requiredSkillCalls ?? []).some((call) => call.skill === UI_DESIGN_SKILL)
147
+ ? '协议 ' + node.id + ' 绑定等级 = advisory,非 guarded'
148
+ : '协议 ' + node.id + ' 节点未登记 ' + UI_DESIGN_SKILL + ' 绑定';
149
+ console.error('UI-DESIGN: 门未启用(' + protocolState + ')');
150
+ return;
151
+ }
152
+ const frontend = await frontendCriterion(state.activeChange);
153
+ if (frontend.toneState === 'no-basis') {
154
+ // 模板整份缺失:适用性门控的输入判据不存在,按「未校验」显式跳过(不静默通过,也不误报非前端)。
155
+ console.error('UI-DESIGN 未校验(模板缺失)——flow-kit/templates/CHANGE.md 不在项目根,'
156
+ + '「视觉调性」段名无从派生,适用性门控跳过');
157
+ return;
158
+ }
159
+ if (frontend.toneState === 'section-missing') {
160
+ // 段缺席不再默认判非前端:新 change 由 open 出口的条件段门禁拦,此处兜底同口径拦下,
161
+ // 要求执行者显式写「不适用」(缺席与声明不适用是两件事,只有后者是决策)。
162
+ const toneRecovery = '在 .specs/' + state.activeChange + '/CHANGE.md 的「视觉调性」段填写选定调性,'
163
+ + '或以结构形态标注「不适用」(非前端项目),然后重试 exit design';
164
+ if (isNewChange(state)) {
165
+ console.error('BLOCKED: CHANGE.md 的「视觉调性」段缺席——新 change 须显式标注「不适用」或填写调性(段缺席不再默认判非前端)');
166
+ console.error('恢复: ' + toneRecovery);
167
+ process.exit(1);
168
+ }
169
+ console.error('UI-DESIGN: skipped(旧 change 兼容)——「视觉调性」段缺席,沿用旧口径判非前端');
170
+ return;
171
+ }
172
+ if (!frontend.applicable) {
173
+ console.error('UI-DESIGN: skipped(非前端)——' + frontend.reason + ',不要求 UI-DESIGN.md');
174
+ return;
175
+ }
176
+ const artifact = path.join(runRoot, '.specs', state.activeChange, 'UI-DESIGN.md');
177
+ if (await fileExists(artifact)) {
178
+ // 工件在场 → 按模板结构校验(frontmatter 8 键 / H2 骨架 / 结构型 H3);模板缺席只输出未校验行。
179
+ let text;
180
+ try {
181
+ text = await fs.readFile(artifact, 'utf8');
182
+ } catch {
183
+ return;
184
+ }
185
+ // 标题形态与 frontmatter 键都属于「模板结构」面:模板缺席时整体判「未校验(模板缺失)」,
186
+ // 不拿登记表形态去替模板做缺席判定(C9/AC-21 的统一口径)。
187
+ const { skeleton, derived, issues } = await evaluateArtifact('UI-DESIGN', text, { underivedPolicy: 'skip' });
188
+ await reportArtifactStructure({
189
+ changeDir: path.join(runRoot, '.specs', state.activeChange),
190
+ state,
191
+ assessments: [assessArtifact({ fileLabel: 'UI-DESIGN.md', skeleton, derived, issues })],
192
+ });
193
+ return;
194
+ }
195
+ const recovery = '补 .specs/' + state.activeChange + '/UI-DESIGN.md(加载 ' + UI_DESIGN_SKILL + ' 并按其协议产出 design token / 反 AI-slop 自检),'
196
+ + '或在 .specs/' + state.activeChange + '/CHANGE.md 的「视觉调性」段以结构形态标注「不适用」(非前端项目)后重试 exit design';
197
+ if (isNewChange(state)) {
198
+ console.error('BLOCKED: design 出口缺 UI-DESIGN.md——' + frontend.reason + ',前端项目须产出 UI-DESIGN.md');
199
+ console.error('恢复: ' + recovery);
200
+ process.exit(1);
201
+ }
202
+ console.error('UI-DESIGN WARN: 缺 .specs/' + state.activeChange + '/UI-DESIGN.md——' + frontend.reason + '(旧 change 渐进不阻断;新 change 将强制)');
203
+ console.error('恢复: ' + recovery);
204
+ }
@@ -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
+ }