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.
- package/.flow-comet/skills/flow-comet/INSTALLED_VERSION +1 -1
- package/.flow-comet/skills/flow-comet/SKILL.md +21 -3
- package/.flow-comet/skills/flow-comet/reference/artifact-template-overlay.md +154 -0
- package/.flow-comet/skills/flow-comet/reference/artifact-templates.json +387 -0
- package/.flow-comet/skills/flow-comet/reference/entry-detail.md +8 -2
- package/.flow-comet/skills/flow-comet/reference/workflow-protocol.json +22 -0
- package/.flow-comet/skills/flow-comet/scripts/comet-hook-guard.mjs +11 -13
- package/.flow-comet/skills/flow-comet/scripts/context-init.mjs +2 -2
- package/.flow-comet/skills/flow-comet/scripts/evolve.mjs +180 -6
- package/.flow-comet/skills/flow-comet/scripts/guard-self-test.mjs +69 -18878
- package/.flow-comet/skills/flow-comet/scripts/health.mjs +29 -5
- package/.flow-comet/skills/flow-comet/scripts/items/a.mjs +1053 -0
- package/.flow-comet/skills/flow-comet/scripts/items/b.mjs +830 -0
- package/.flow-comet/skills/flow-comet/scripts/items/c.mjs +781 -0
- package/.flow-comet/skills/flow-comet/scripts/items/d.mjs +99 -0
- package/.flow-comet/skills/flow-comet/scripts/items/e.mjs +185 -0
- package/.flow-comet/skills/flow-comet/scripts/items/f.mjs +90 -0
- package/.flow-comet/skills/flow-comet/scripts/items/g.mjs +59 -0
- package/.flow-comet/skills/flow-comet/scripts/items/h.mjs +817 -0
- package/.flow-comet/skills/flow-comet/scripts/items/i.mjs +77 -0
- package/.flow-comet/skills/flow-comet/scripts/items/j.mjs +75 -0
- package/.flow-comet/skills/flow-comet/scripts/items/k.mjs +1427 -0
- package/.flow-comet/skills/flow-comet/scripts/items/l.mjs +275 -0
- package/.flow-comet/skills/flow-comet/scripts/items/m.mjs +618 -0
- package/.flow-comet/skills/flow-comet/scripts/items/n.mjs +154 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/count-derivation.mjs +97 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/fix-loop-node.mjs +196 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-artifact-skeleton.mjs +406 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-evidence-gates.mjs +756 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-gates.mjs +1235 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-routing.mjs +384 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-state-io.mjs +404 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-template-fidelity.mjs +714 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/guard-uidesign.mjs +204 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/reentry-node.mjs +237 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/replan-node.mjs +209 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-branch.mjs +49 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-bridgecheck.mjs +201 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-admin.mjs +179 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-flow.mjs +714 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-commands-recovery.mjs +578 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-determine.mjs +52 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-evolve-hint.mjs +55 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-explain.mjs +123 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-intelscan.mjs +58 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-io.mjs +147 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/state-skillmarkers.mjs +249 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/system-fixtures.mjs +1505 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/task-graph.mjs +471 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/test-assertions.mjs +509 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/test-fixtures.mjs +1248 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/test-helpers.mjs +237 -0
- package/.flow-comet/skills/flow-comet/scripts/lib/text-anchor-checks.mjs +2493 -0
- package/.flow-comet/skills/flow-comet/scripts/protocol-utils.mjs +214 -2
- package/.flow-comet/skills/flow-comet/scripts/route-node.mjs +72 -1123
- package/.flow-comet/skills/flow-comet/scripts/scenarios/a.mjs +976 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/b.mjs +1233 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/c.mjs +1382 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/d.mjs +483 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/e.mjs +1398 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/f.mjs +1466 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/g.mjs +1367 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/h.mjs +1287 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/i.mjs +1436 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/j.mjs +1384 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/k.mjs +1338 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/l.mjs +1492 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/m.mjs +971 -0
- package/.flow-comet/skills/flow-comet/scripts/scenarios/n.mjs +2125 -0
- package/.flow-comet/skills/flow-comet/scripts/state-schema.mjs +1 -1
- package/.flow-comet/skills/flow-comet/scripts/system-test.mjs +59 -7559
- package/.flow-comet/skills/flow-comet/scripts/time-utils.mjs +4 -4
- package/.flow-comet/skills/flow-comet/scripts/workflow-guard.mjs +223 -2476
- package/.flow-comet/skills/flow-comet/scripts/workflow-handoff.mjs +5 -5
- package/.flow-comet/skills/flow-comet/scripts/workflow-state.mjs +27 -2190
- package/.flow-comet/skills/flow-comet-archive/SKILL.md +6 -2
- package/.flow-comet/skills/flow-comet-change/SKILL.md +6 -2
- package/.flow-comet/skills/flow-comet-design/GUIDANCE.md +13 -3
- package/.flow-comet/skills/flow-comet-design/SKILL.md +15 -8
- package/.flow-comet/skills/flow-comet-dev/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-evolve/SKILL.md +1 -0
- package/.flow-comet/skills/flow-comet-execute/SKILL.md +13 -10
- package/.flow-comet/skills/flow-comet-health/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-integration/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-open/GUIDANCE.md +14 -4
- package/.flow-comet/skills/flow-comet-open/SKILL.md +11 -9
- package/.flow-comet/skills/flow-comet-plan/GUIDANCE.md +13 -3
- package/.flow-comet/skills/flow-comet-plan/SKILL.md +11 -7
- package/.flow-comet/skills/flow-comet-requirement/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-review/SKILL.md +15 -9
- package/.flow-comet/skills/flow-comet-subagent-execute/GUIDANCE.md +13 -3
- package/.flow-comet/skills/flow-comet-subagent-execute/SKILL.md +11 -8
- package/.flow-comet/skills/flow-comet-task/SKILL.md +1 -1
- package/.flow-comet/skills/flow-comet-test/SKILL.md +6 -2
- package/.flow-comet/skills/flow-comet-ui-design/SKILL.md +2 -2
- package/.flow-comet/skills/flow-comet-verify/SKILL.md +12 -8
- package/README.md +52 -69
- package/package.json +3 -2
- package/scripts/dsh-bridge.mjs +2 -2
- package/scripts/lib/installer-cleanup.mjs +152 -0
- package/scripts/lib/installer-flowkit.mjs +159 -0
- package/scripts/lib/installer-hooks.mjs +363 -0
- package/scripts/lib/installer-loader.mjs +220 -0
- package/scripts/lib/installer-migrate.mjs +236 -0
- package/scripts/lib/installer-platforms.mjs +314 -0
- package/scripts/prepare-env.mjs +21 -1313
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
// state 命令族 · 恢复面(单一归属):reenter(archive 源受控重入)/ replan(受控计划重校重签)。
|
|
2
|
+
// 全部判定集中在 route-node.mjs 纯函数族,本模块只做编排与落盘(BLOCK 与空操作路径一律在
|
|
3
|
+
// 写盘前返回,state 字节零改写);argv = 进程参数原样。
|
|
4
|
+
import { createHash } from 'crypto';
|
|
5
|
+
import { promises as fs } from 'fs';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
import {
|
|
8
|
+
resolveReentryDecision, resolveReplanDecision, analyzeDependencyGraph, findParallelWriteConflicts,
|
|
9
|
+
protocolTaskFilePath, REENTRY_ROUND_LIMIT, REENTRY_TARGET_NODE_IDS, REPLAN_ROUND_LIMIT,
|
|
10
|
+
} from '../route-node.mjs';
|
|
11
|
+
import { taskOpeningAttrs, taskBlocks } from '../task-parsing.mjs';
|
|
12
|
+
import { validateStateFields } from '../state-schema.mjs';
|
|
13
|
+
import { inspectWorkflowPathSegments } from '../protocol-utils.mjs';
|
|
14
|
+
import { fileExists, inspectReentryChangeDir, readJson, readState, runRootDir, specsDir, stateFile, writeState } from './state-io.mjs';
|
|
15
|
+
import { TASK7_REQUIRED } from './task-graph.mjs';
|
|
16
|
+
|
|
17
|
+
// reenter <target> --authorized-by <source> --reason <text> [--continue-round <n>]
|
|
18
|
+
// 受控重入(archive 源回边):archive 已 entry 且归档移动尚未发生时,用户显式授权把工作归属
|
|
19
|
+
// 退回执行家族 / review / verify 之一。编排顺序:参数防护 → 路径边界(change 名单段 +
|
|
20
|
+
// realpath 直接子目录)→ 单一权威判定(目标 / change 名 / 上限 / 幂等 / 源 / 授权)→ 备份
|
|
21
|
+
// (包含性复核)→ 转移 → 授权留痕 → 审计事件 → REENTRY 行。
|
|
22
|
+
export async function runReenter(protocol, argv) {
|
|
23
|
+
const target = argv[3];
|
|
24
|
+
const reentryTargetList = [...REENTRY_TARGET_NODE_IDS].join(', ');
|
|
25
|
+
const reenterUsage = '用法: workflow-state.mjs reenter <target> --authorized-by <source> --reason <text>'
|
|
26
|
+
+ ' [--continue-round <n>](target 白名单: ' + reentryTargetList + ')';
|
|
27
|
+
if (typeof target !== 'string' || target === '' || target.startsWith('--')) {
|
|
28
|
+
console.error('BLOCKED: reenter 缺少目标节点。' + reenterUsage);
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
let authorizedBy = null;
|
|
32
|
+
let reason = null;
|
|
33
|
+
let continuationRound = null;
|
|
34
|
+
const reenterArgs = argv.slice(4);
|
|
35
|
+
for (let index = 0; index < reenterArgs.length; index += 1) {
|
|
36
|
+
const arg = reenterArgs[index];
|
|
37
|
+
// 支持 --name value 与 --name=value 两种形态;--protocol 为全局参数(协议路径已由
|
|
38
|
+
// resolveProtocol 统一解析),命令面只做占位透传。
|
|
39
|
+
let key = arg;
|
|
40
|
+
let value = null;
|
|
41
|
+
if (typeof arg === 'string' && arg.startsWith('--')) {
|
|
42
|
+
const eq = arg.indexOf('=');
|
|
43
|
+
if (eq > 0) {
|
|
44
|
+
key = arg.slice(0, eq);
|
|
45
|
+
value = arg.slice(eq + 1);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (key === '--protocol') {
|
|
49
|
+
if (value === null) index += 1;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (key === '--continue-round') {
|
|
53
|
+
// 续轮授权只在达到轮次上限后生效:命令面收正整数,形态合法性与「是否已到上限」由
|
|
54
|
+
// 单一权威综合判定裁决(本处只做数值形态防护,fail-closed,先于任何读盘)。
|
|
55
|
+
let consumedNext = false;
|
|
56
|
+
if (value === null) {
|
|
57
|
+
value = reenterArgs[index + 1];
|
|
58
|
+
consumedNext = true;
|
|
59
|
+
}
|
|
60
|
+
const parsedRound = typeof value === 'string' && value.trim() !== '' && !value.startsWith('--')
|
|
61
|
+
? Number(value.trim())
|
|
62
|
+
: Number.NaN;
|
|
63
|
+
if (!Number.isInteger(parsedRound) || parsedRound <= 0) {
|
|
64
|
+
console.error('BLOCKED: reenter --continue-round 需要正整数取值,实际 ' + JSON.stringify(value) + '。' + reenterUsage);
|
|
65
|
+
process.exit(1);
|
|
66
|
+
}
|
|
67
|
+
continuationRound = parsedRound;
|
|
68
|
+
if (consumedNext) index += 1;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (key === '--authorized-by' || key === '--reason') {
|
|
72
|
+
let consumedNext = false;
|
|
73
|
+
if (value === null) {
|
|
74
|
+
value = reenterArgs[index + 1];
|
|
75
|
+
consumedNext = true;
|
|
76
|
+
}
|
|
77
|
+
// 缺值 / 空值 / 纯空白 / 取下个选项名当值 → 一律用法错误(fail-closed,先于任何读盘)
|
|
78
|
+
if (typeof value !== 'string' || value.trim() === '' || value.startsWith('--')) {
|
|
79
|
+
console.error('BLOCKED: reenter ' + key + ' 需要非空取值。' + reenterUsage);
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
if (key === '--authorized-by') authorizedBy = value.trim();
|
|
83
|
+
else reason = value.trim();
|
|
84
|
+
if (consumedNext) index += 1;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
console.error('BLOCKED: reenter 未知参数 ' + JSON.stringify(arg) + '。' + reenterUsage);
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
if (authorizedBy === null) {
|
|
91
|
+
console.error('BLOCKED: reenter 需要显式授权 --authorized-by <source>(授权不可自决)。' + reenterUsage);
|
|
92
|
+
process.exit(1);
|
|
93
|
+
}
|
|
94
|
+
if (reason === null) {
|
|
95
|
+
console.error('BLOCKED: reenter 需要 --reason <text> 说明重入原因。' + reenterUsage);
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
const state = await readState();
|
|
99
|
+
const badFields = validateStateFields(state);
|
|
100
|
+
if (badFields.length > 0) {
|
|
101
|
+
// history 存在但非数组等类型损坏在此 fail-closed:不得由本命令静默清空覆盖审计历史。
|
|
102
|
+
console.error('BLOCKED: state 字段类型非法: ' + badFields[0]);
|
|
103
|
+
process.exit(1);
|
|
104
|
+
}
|
|
105
|
+
// 原始 state(未经 readState 兼容默认值叠加):落盘时只写「原始键集 + 本次实际改动的字段」,
|
|
106
|
+
// 兼容默认值不得随重入回写(最小旧 state 的键集保持逐项不变)。
|
|
107
|
+
const rawState = (await fileExists(stateFile())) ? await readJson(stateFile()) : null;
|
|
108
|
+
// 路径边界:change 名必须是 .specs/ 下的单段真实目录(realpath 归一后是 .specs 直接子目录,
|
|
109
|
+
// 且路径上无 symlink/junction)。归档移动已发生(目录不在原位)、change 已 completed、
|
|
110
|
+
// change 名畸形(多段 / . / .. / 保留目录 archive)与链接逃逸一律 BLOCK。该边界先于综合
|
|
111
|
+
// 判定执行——completed 与已移动形态必须无条件 BLOCK(不被空操作分支短路)。
|
|
112
|
+
const changeName = typeof state.activeChange === 'string' ? state.activeChange : '';
|
|
113
|
+
const changeBoundary = await inspectReentryChangeDir(changeName);
|
|
114
|
+
const changeDirPresent = changeBoundary.ok
|
|
115
|
+
? true
|
|
116
|
+
: (changeBoundary.reason === 'change-dir-missing' ? false : null);
|
|
117
|
+
const movedOrCompletedMessage = 'BLOCKED: 归档移动已发生或 change 已 completed(.specs/'
|
|
118
|
+
+ (changeName || '<未知>') + '/ 在场=' + changeDirPresent + ',status=' + JSON.stringify(state.status)
|
|
119
|
+
+ ')——受控重入只覆盖归档移动前的运行形态;请走人工处置、新 change 或 hotfix 路径。';
|
|
120
|
+
if (state.status === 'completed' || changeDirPresent === false) {
|
|
121
|
+
console.error(movedOrCompletedMessage);
|
|
122
|
+
process.exit(1);
|
|
123
|
+
}
|
|
124
|
+
if (!changeBoundary.ok) {
|
|
125
|
+
const boundaryMessages = {
|
|
126
|
+
'no-active-change': '当前没有活跃 change(activeChange 缺失或为空)——先 select <change-id>',
|
|
127
|
+
'change-name-invalid': 'change 名非法 ' + JSON.stringify(changeName)
|
|
128
|
+
+ '——必须是 .specs/ 下的单段目录名(拒绝路径分隔符、. / .. 与保留目录 archive)',
|
|
129
|
+
'change-dir-escape': 'change 目录路径越界或跨越符号链接 / junction(' + changeBoundary.detail
|
|
130
|
+
+ ')——受控重入与备份只允许落在 .specs/<change-id>/ 真实目录内',
|
|
131
|
+
};
|
|
132
|
+
console.error('BLOCKED: ' + boundaryMessages[changeBoundary.reason]);
|
|
133
|
+
process.exit(1);
|
|
134
|
+
}
|
|
135
|
+
const changeDir = changeBoundary.changeDir;
|
|
136
|
+
// 授权形态的 round / at 由引擎派生(命令面只承载授权来源、原因与上限后的续轮轮次):普通轮次
|
|
137
|
+
// 以合法占位值过形态门;落盘轮次一律取综合判定返回的 round(由本 change 历史事件派生),
|
|
138
|
+
// 不在此内联第二份轮次推导。
|
|
139
|
+
const authorizedAt = new Date().toISOString();
|
|
140
|
+
const decision = resolveReentryDecision({
|
|
141
|
+
protocol,
|
|
142
|
+
state,
|
|
143
|
+
target,
|
|
144
|
+
authorization: { round: 1, at: authorizedAt, source: authorizedBy, target },
|
|
145
|
+
continuationRound,
|
|
146
|
+
});
|
|
147
|
+
if (!decision.ok) {
|
|
148
|
+
if (decision.reason === 'round-limit-reached') {
|
|
149
|
+
console.error('BLOCKED: 本 change 受控重入已达上限(' + decision.used + '/' + decision.limit
|
|
150
|
+
+ ' 轮)。需要人工裁决:继续(显式授权后在同一命令加 --continue-round <n>,n 为不小于第 '
|
|
151
|
+
+ (decision.used + 1) + ' 轮的正整数,须与 --authorized-by / --reason 同次传入)/ 停止(保持归档态,不再重入)。');
|
|
152
|
+
process.exit(1);
|
|
153
|
+
}
|
|
154
|
+
const guidance = {
|
|
155
|
+
'target-not-allowed': '目标节点 ' + JSON.stringify(target) + ' 不在受控重入白名单('
|
|
156
|
+
+ reentryTargetList + ')——请显式指定白名单内目标',
|
|
157
|
+
'target-not-enabled': '目标节点 ' + JSON.stringify(target)
|
|
158
|
+
+ ' 在当前协议中未启用(disabled)——请检查协议或改选已启用目标',
|
|
159
|
+
'no-active-change': '当前没有活跃 change(activeChange 缺失或为空)——先 select <change-id>',
|
|
160
|
+
'change-name-invalid': 'change 名非法 ' + JSON.stringify(changeName)
|
|
161
|
+
+ '——必须是 .specs/ 下的单段目录名(拒绝路径分隔符、. / .. 与保留目录 archive)',
|
|
162
|
+
'source-not-archive': '受控重入只覆盖 archive 源(当前节点 ' + JSON.stringify(state.currentNode)
|
|
163
|
+
+ ')——正常推进请走 next / record / exit 既有通道',
|
|
164
|
+
'continuation-not-required': '--continue-round 只在达到轮次上限后需要(当前 ' + decision.used + '/'
|
|
165
|
+
+ decision.limit + ' 轮)——上限前请直接以 --authorized-by / --reason 发起',
|
|
166
|
+
}[decision.reason] || ('授权形态不合法(' + decision.reason + ')——需要用户显式传入 --authorized-by <source>');
|
|
167
|
+
console.error('BLOCKED: ' + guidance);
|
|
168
|
+
process.exit(1);
|
|
169
|
+
}
|
|
170
|
+
if (decision.action === 'noop') {
|
|
171
|
+
console.log('REENTRY: 空操作——workflow 已处于目标形态(' + target
|
|
172
|
+
+ ',完成标记已是目标前驱交集);未备份、未计数、未改写 state。');
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (decision.action !== 'apply') {
|
|
176
|
+
console.error('BLOCKED: 受控重入判定返回未知动作 ' + JSON.stringify(decision.action) + '——按 fail-closed 拒绝。');
|
|
177
|
+
process.exit(1);
|
|
178
|
+
}
|
|
179
|
+
const round = decision.round;
|
|
180
|
+
const source = decision.authorization.source;
|
|
181
|
+
const at = decision.authorization.at;
|
|
182
|
+
const continuationAuthorized = decision.continuationAuthorized === true;
|
|
183
|
+
// 备份:重入前 state 全量快照(文件原始字节,UTF-8 BOM 按可解析形态落盘);文件名
|
|
184
|
+
// <UTC ISO 净化>-pre-<target>.json;审计指纹 = 备份文件字节的 sha256(可核验)。
|
|
185
|
+
const backupDir = path.join(changeDir, '.reentry-backups');
|
|
186
|
+
const backupFile = at.replace(/:/g, '-') + '-pre-' + target + '.json';
|
|
187
|
+
const backupPath = path.join(backupDir, backupFile);
|
|
188
|
+
const backupRecord = path.relative(runRootDir(), backupPath).split(path.sep).join('/');
|
|
189
|
+
const snapshotBytes = Buffer.from((await fs.readFile(stateFile(), 'utf8')).replace(/^\uFEFF/, ''), 'utf8');
|
|
190
|
+
const fingerprint = createHash('sha256').update(snapshotBytes).digest('hex');
|
|
191
|
+
const existingHistory = Array.isArray(rawState?.history) ? rawState.history : [];
|
|
192
|
+
let backupWritten = false;
|
|
193
|
+
try {
|
|
194
|
+
if (!rawState || typeof rawState !== 'object' || Array.isArray(rawState)) {
|
|
195
|
+
throw new Error('state 文件缺失或形态非法,无法重入');
|
|
196
|
+
}
|
|
197
|
+
// 备份写入路径的物理包含性复核(单一权威路径段扫描):备份目录与备份文件逐段
|
|
198
|
+
// lstat + realpath——任何 symlink/junction 逃出 .specs/ 一律拒绝,不写任何字节。
|
|
199
|
+
await fs.mkdir(backupDir, { recursive: true });
|
|
200
|
+
await inspectWorkflowPathSegments(specsDir(), backupDir, 'reenter backup dir', 'directory');
|
|
201
|
+
await fs.writeFile(backupPath, snapshotBytes);
|
|
202
|
+
backupWritten = true;
|
|
203
|
+
await inspectWorkflowPathSegments(specsDir(), backupPath, 'reenter backup file', 'file');
|
|
204
|
+
// 授权留痕:嵌套写入被重入目标节点的 evidence(授权记录 + 备份指针 + 指纹 + 原因 +
|
|
205
|
+
// 续轮标记)。只改 evidence 的目标节点键,其余键按原始键集逐项保留。
|
|
206
|
+
const existingEvidence = rawState.evidence && typeof rawState.evidence === 'object' && !Array.isArray(rawState.evidence)
|
|
207
|
+
? rawState.evidence
|
|
208
|
+
: {};
|
|
209
|
+
const targetEvidence = existingEvidence[decision.target]
|
|
210
|
+
&& typeof existingEvidence[decision.target] === 'object'
|
|
211
|
+
&& !Array.isArray(existingEvidence[decision.target])
|
|
212
|
+
? existingEvidence[decision.target]
|
|
213
|
+
: {};
|
|
214
|
+
const authorizationRecord = {
|
|
215
|
+
round,
|
|
216
|
+
at,
|
|
217
|
+
source,
|
|
218
|
+
target: decision.target,
|
|
219
|
+
backup: backupRecord,
|
|
220
|
+
fingerprint,
|
|
221
|
+
reason,
|
|
222
|
+
...(continuationAuthorized ? { continuationAuthorized: true } : {}),
|
|
223
|
+
};
|
|
224
|
+
// 落盘形态 = 原始 state 键集 + 本次实际改动的四类机器字段与审计字段——转移语义
|
|
225
|
+
// (currentNode := target / completedNodes := 前驱交集 / status := running)与审计
|
|
226
|
+
// (evidence 留痕 + history 追加事件)之外的字段逐项不变;readState 的兼容默认值不回写。
|
|
227
|
+
const persisted = { ...rawState };
|
|
228
|
+
persisted.currentNode = decision.target;
|
|
229
|
+
persisted.completedNodes = decision.completedNodes.slice();
|
|
230
|
+
persisted.status = 'running';
|
|
231
|
+
persisted.evidence = {
|
|
232
|
+
...existingEvidence,
|
|
233
|
+
[decision.target]: { ...targetEvidence, reentryAuthorization: authorizationRecord },
|
|
234
|
+
};
|
|
235
|
+
// 审计事件:append-only 追加 reentry-applied;change 字段是跨 change 轮次隔离的唯一依据。
|
|
236
|
+
persisted.history = existingHistory.concat([{
|
|
237
|
+
event: 'reentry-applied',
|
|
238
|
+
from: 'archive',
|
|
239
|
+
to: decision.target,
|
|
240
|
+
round,
|
|
241
|
+
authorization: { round, at, source, target: decision.target },
|
|
242
|
+
backup: backupRecord,
|
|
243
|
+
fingerprint,
|
|
244
|
+
at,
|
|
245
|
+
change: changeName,
|
|
246
|
+
reason,
|
|
247
|
+
...(continuationAuthorized ? { continuationAuthorized: true } : {}),
|
|
248
|
+
}]);
|
|
249
|
+
const persistedBad = validateStateFields(persisted);
|
|
250
|
+
if (persistedBad.length > 0) throw new Error('state 字段类型非法: ' + persistedBad[0]);
|
|
251
|
+
await writeState(persisted);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
// 写盘失败:回滚本次调用创建的孤儿备份(state 由原子写保证不被截断),原始错误照常上抛。
|
|
254
|
+
if (backupWritten) {
|
|
255
|
+
try { await fs.rm(backupPath, { force: true }); } catch { /* 清理失败不掩盖原始错误 */ }
|
|
256
|
+
}
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
const roundLabel = continuationAuthorized
|
|
260
|
+
? '第 ' + round + ' 轮(显式授权续轮,上限 ' + REENTRY_ROUND_LIMIT + ')'
|
|
261
|
+
: '第 ' + round + '/' + REENTRY_ROUND_LIMIT + ' 轮';
|
|
262
|
+
console.log('REENTRY: archive → ' + decision.target + '(授权源 ' + source + ';' + roundLabel
|
|
263
|
+
+ ';备份 ' + backupRecord + ')');
|
|
264
|
+
console.log('REASON: ' + reason);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// replan <reason> --authorized-by <source> [--continue-round <n>]
|
|
269
|
+
// 受控计划重校重签(ADR-013):计划在执行中被证明有缺陷时,唯一合规的重签入口——只在
|
|
270
|
+
// execute / subagent-execute 相位可用、停原位(不跳节点、不改 completedNodes),先把任务集
|
|
271
|
+
// 重新校验(依赖图 + 任务字段,绝不豁免)再重录 state.taskHash。编排顺序与 reenter 同构:
|
|
272
|
+
// 参数防护 → 读 state → 单一权威判定(相位/change → 授权 → 上限与续轮 → 幂等空操作)→
|
|
273
|
+
// 重校 → 备份(sha256)→ 重签 → 双写留痕 → REPLAN/REASON 行。
|
|
274
|
+
export async function runReplan(protocol, argv) {
|
|
275
|
+
const replanUsage = '用法: workflow-state.mjs replan <reason> --authorized-by <source> [--continue-round <n>]';
|
|
276
|
+
const replanReason = argv[3];
|
|
277
|
+
if (typeof replanReason !== 'string' || replanReason.trim() === '' || replanReason.startsWith('--')) {
|
|
278
|
+
console.error('BLOCKED: replan 缺少原因(位置参数 <reason> 必填且非空)。' + replanUsage);
|
|
279
|
+
process.exit(1);
|
|
280
|
+
}
|
|
281
|
+
const reason = replanReason.trim();
|
|
282
|
+
let authorizedBy = null;
|
|
283
|
+
let continuationRound = null;
|
|
284
|
+
const replanArgs = argv.slice(4);
|
|
285
|
+
for (let index = 0; index < replanArgs.length; index += 1) {
|
|
286
|
+
const arg = replanArgs[index];
|
|
287
|
+
// 支持 --name value 与 --name=value 两种形态;--protocol 为全局参数(协议路径已由
|
|
288
|
+
// resolveProtocol 统一解析),命令面只做占位透传。
|
|
289
|
+
let key = arg;
|
|
290
|
+
let value = null;
|
|
291
|
+
if (typeof arg === 'string' && arg.startsWith('--')) {
|
|
292
|
+
const eq = arg.indexOf('=');
|
|
293
|
+
if (eq > 0) {
|
|
294
|
+
key = arg.slice(0, eq);
|
|
295
|
+
value = arg.slice(eq + 1);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (key === '--protocol') {
|
|
299
|
+
if (value === null) index += 1;
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (key === '--continue-round') {
|
|
303
|
+
// 续轮授权只在达到轮次上限后生效:命令面收正整数,形态合法性与「是否已到上限」由
|
|
304
|
+
// 单一权威综合判定裁决(本处只做数值形态防护,fail-closed,先于任何读盘)。
|
|
305
|
+
let consumedNext = false;
|
|
306
|
+
if (value === null) {
|
|
307
|
+
value = replanArgs[index + 1];
|
|
308
|
+
consumedNext = true;
|
|
309
|
+
}
|
|
310
|
+
const parsedRound = typeof value === 'string' && value.trim() !== '' && !value.startsWith('--')
|
|
311
|
+
? Number(value.trim())
|
|
312
|
+
: Number.NaN;
|
|
313
|
+
if (!Number.isInteger(parsedRound) || parsedRound <= 0) {
|
|
314
|
+
console.error('BLOCKED: replan --continue-round 需要正整数取值,实际 ' + JSON.stringify(value) + '。' + replanUsage);
|
|
315
|
+
process.exit(1);
|
|
316
|
+
}
|
|
317
|
+
continuationRound = parsedRound;
|
|
318
|
+
if (consumedNext) index += 1;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (key === '--authorized-by') {
|
|
322
|
+
let consumedNext = false;
|
|
323
|
+
if (value === null) {
|
|
324
|
+
value = replanArgs[index + 1];
|
|
325
|
+
consumedNext = true;
|
|
326
|
+
}
|
|
327
|
+
// 缺值 / 空值 / 纯空白 / 取下个选项名当值 → 一律用法错误(fail-closed,先于任何读盘)
|
|
328
|
+
if (typeof value !== 'string' || value.trim() === '' || value.startsWith('--')) {
|
|
329
|
+
console.error('BLOCKED: replan --authorized-by 需要非空取值。' + replanUsage);
|
|
330
|
+
process.exit(1);
|
|
331
|
+
}
|
|
332
|
+
authorizedBy = value.trim();
|
|
333
|
+
if (consumedNext) index += 1;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
console.error('BLOCKED: replan 未知参数 ' + JSON.stringify(arg) + '。' + replanUsage);
|
|
337
|
+
process.exit(1);
|
|
338
|
+
}
|
|
339
|
+
if (authorizedBy === null) {
|
|
340
|
+
console.error('BLOCKED: replan 需要显式授权 --authorized-by <source>(授权不可自决)。' + replanUsage);
|
|
341
|
+
process.exit(1);
|
|
342
|
+
}
|
|
343
|
+
const state = await readState();
|
|
344
|
+
const badFields = validateStateFields(state);
|
|
345
|
+
if (badFields.length > 0) {
|
|
346
|
+
// history 存在但非数组等类型损坏在此 fail-closed:不得由本命令静默清空覆盖审计历史。
|
|
347
|
+
console.error('BLOCKED: state 字段类型非法: ' + badFields[0]);
|
|
348
|
+
process.exit(1);
|
|
349
|
+
}
|
|
350
|
+
// 原始 state(未经 readState 兼容默认值叠加):备份取文件原始字节,落盘只写「原始键集 +
|
|
351
|
+
// 本次实际改动的字段」——兼容默认值不得随重校回写(与受控重入同纪律)。
|
|
352
|
+
const rawState = (await fileExists(stateFile())) ? await readJson(stateFile()) : null;
|
|
353
|
+
const changeName = typeof state.activeChange === 'string' ? state.activeChange : '';
|
|
354
|
+
// 任务内容读取(fail-closed):不可读 / 为空 → 交由综合判定返回 task-content-missing
|
|
355
|
+
// block(缺 TASK.md 时不得以「空任务集签名」判幂等空操作放行)。
|
|
356
|
+
let taskContent = null;
|
|
357
|
+
try {
|
|
358
|
+
taskContent = await fs.readFile(protocolTaskFilePath(protocol, changeName, specsDir()), 'utf8');
|
|
359
|
+
} catch { taskContent = null; }
|
|
360
|
+
// 授权形态的 round / at 由引擎派生(命令面只承载授权来源、原因与上限后的续轮轮次):普通
|
|
361
|
+
// 轮次以合法占位值过形态门;落盘轮次一律取综合判定返回的 round(由本 change 历史事件派生),
|
|
362
|
+
// 不在此内联第二份轮次推导。
|
|
363
|
+
const authorizedAt = new Date().toISOString();
|
|
364
|
+
const decision = resolveReplanDecision({
|
|
365
|
+
protocol,
|
|
366
|
+
state,
|
|
367
|
+
authorization: { round: 1, at: authorizedAt, source: authorizedBy, reason, node: state.currentNode },
|
|
368
|
+
continuationRound,
|
|
369
|
+
taskContent,
|
|
370
|
+
});
|
|
371
|
+
if (!decision.ok) {
|
|
372
|
+
if (decision.reason === 'round-limit-reached') {
|
|
373
|
+
console.error('BLOCKED: 本 change 计划重校已达上限(' + decision.used + '/' + decision.limit
|
|
374
|
+
+ ' 轮)。需要人工裁决:继续(显式授权后在同一命令加 --continue-round <n>,n 为不小于第 '
|
|
375
|
+
+ (decision.used + 1) + ' 轮的正整数,须与 --authorized-by / <reason> 同次传入)/ 停止(保持现状,不再重校)。');
|
|
376
|
+
process.exit(1);
|
|
377
|
+
}
|
|
378
|
+
const replanGuidance = {
|
|
379
|
+
'no-active-change': '当前没有活跃 change(activeChange 缺失或为空)——先 select <change-id>',
|
|
380
|
+
'change-name-invalid': 'change 名非法 ' + JSON.stringify(changeName)
|
|
381
|
+
+ '——必须是 .specs/ 下的单段目录名(拒绝路径分隔符、. / .. 与保留目录 archive)',
|
|
382
|
+
'phase-not-allowed': 'replan 只在 execute / subagent-execute 相位可用(当前节点 '
|
|
383
|
+
+ JSON.stringify(state.currentNode) + ')——其余相位请走既有推进 / 受控重入通道',
|
|
384
|
+
'continuation-not-required': '--continue-round 只在达到轮次上限后需要(当前 ' + decision.used + '/'
|
|
385
|
+
+ decision.limit + ' 轮)——上限前请直接以 --authorized-by / <reason> 发起',
|
|
386
|
+
'task-content-missing': '任务内容缺失或不可读(.specs/<change>/TASK.md 不存在 / 为空)——按 fail-closed 拒绝重签',
|
|
387
|
+
}[decision.reason] || ('授权形态不合法(' + decision.reason + ')——需要用户显式传入 --authorized-by <source>');
|
|
388
|
+
console.error('BLOCKED: ' + replanGuidance);
|
|
389
|
+
process.exit(1);
|
|
390
|
+
}
|
|
391
|
+
if (decision.action === 'noop') {
|
|
392
|
+
console.log('REPLAN: 空操作——state.taskHash 已与当前 TASK.md 同签(' + decision.node
|
|
393
|
+
+ '),无需重校;未备份、未计数、未写事件、未改写 state。');
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (decision.action !== 'apply') {
|
|
397
|
+
console.error('BLOCKED: replan 判定返回未知动作 ' + JSON.stringify(decision.action) + '——按 fail-closed 拒绝。');
|
|
398
|
+
process.exit(1);
|
|
399
|
+
}
|
|
400
|
+
// 重校(ADR-013 决策 3:replan 绝不做校验豁免):任务块 / <verify> / 任务图(依赖环与
|
|
401
|
+
// 缺失依赖——analyzeDependencyGraph 单一实现,与 plan 出口同源)/ 7 字段完整性。任一失败
|
|
402
|
+
// → BLOCK,状态零改写(本段全部先于备份与写盘)。
|
|
403
|
+
const taskList = taskBlocks(taskContent);
|
|
404
|
+
if (taskList.length === 0) {
|
|
405
|
+
console.error('BLOCKED: TASK.md 无 <task> 块——replan 不做校验豁免,拒绝重签(状态零改写)。');
|
|
406
|
+
process.exit(1);
|
|
407
|
+
}
|
|
408
|
+
const missingVerify = taskList.filter((block) => !/<verify>/.test(block));
|
|
409
|
+
if (missingVerify.length > 0) {
|
|
410
|
+
console.error('BLOCKED: TASK.md 中 ' + missingVerify.length
|
|
411
|
+
+ ' 个 task 缺 <verify> 字段——replan 不做校验豁免,拒绝重签(状态零改写)。');
|
|
412
|
+
process.exit(1);
|
|
413
|
+
}
|
|
414
|
+
const depGraph = analyzeDependencyGraph(taskList);
|
|
415
|
+
if (depGraph.cyclic || depGraph.missing.length > 0) {
|
|
416
|
+
const detail = depGraph.cyclic
|
|
417
|
+
? '依赖环(拓扑排序无解,涉及任务: ' + depGraph.cycleIds.join(', ') + ')'
|
|
418
|
+
: '依赖不存在的任务: ' + depGraph.missing.map((m) => m.id + '→' + m.dep).join(', ');
|
|
419
|
+
console.error('BLOCKED: TASK.md ' + detail
|
|
420
|
+
+ '——依赖图不可满足(任务的依赖链必须存在且无环);replan 不做校验豁免,拒绝重签(状态零改写)'
|
|
421
|
+
+ ';恢复: 调整任务 depends_on——移除成环引用或改为真实存在的任务 id 后重试');
|
|
422
|
+
process.exit(1);
|
|
423
|
+
}
|
|
424
|
+
// 任务字段完整性(与 plan 出口同一判据):字段集从 `task-graph.mjs` 的单一权威常量取用——本模块
|
|
425
|
+
// 是执行期唯一允许修订任务集的通道,若在此另写一份字面集合,plan 出口会拒的任务集将被 replan
|
|
426
|
+
// 静默放行。新模板形态(含 <name>)且新 change → BLOCK;旧式一字段形态 / 旧 change 保持渐进
|
|
427
|
+
// WARN(不放大既有语义)。
|
|
428
|
+
const replanTask7NewFormat = taskList.some((block) => /<name\b/i.test(block));
|
|
429
|
+
const replanTask7Broken = [];
|
|
430
|
+
for (const block of taskList) {
|
|
431
|
+
const missing = TASK7_REQUIRED.filter((field) => !new RegExp('</?' + field + '\\b', 'i').test(block));
|
|
432
|
+
if (missing.length > 0) {
|
|
433
|
+
const idAttr = (taskOpeningAttrs(block) || {}).id || '?';
|
|
434
|
+
replanTask7Broken.push((idAttr !== '?' ? idAttr + ' ' : '') + '缺 ' + missing.join('/'));
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (replanTask7Broken.length > 0) {
|
|
438
|
+
if (replanTask7NewFormat && state.newChange === true) {
|
|
439
|
+
// 文案的集合字符串**从判据常量派生**(不得在此再写一遍字面集合——第二处同口径表达会在
|
|
440
|
+
// 判据演进时静默落后于实现;构造方式不改变渲染结果)。
|
|
441
|
+
console.error('BLOCKED: TASK.md 任务缺 7 字段(' + TASK7_REQUIRED.join('/') + '): '
|
|
442
|
+
+ replanTask7Broken.join('; ') + ';恢复: 对照 flow-kit/templates/TASK.md 补齐每个 <task> 的缺字段后重试;replan 不做校验豁免');
|
|
443
|
+
process.exit(1);
|
|
444
|
+
}
|
|
445
|
+
console.error('TASK-7FIELD WARN: TASK.md ' + replanTask7Broken.join('; ')
|
|
446
|
+
+ (replanTask7NewFormat ? '(旧 change 渐进,不阻断)' : '(旧式一字段模板形态,渐进不阻断)'));
|
|
447
|
+
}
|
|
448
|
+
// 路径边界:change 名必须是 .specs/ 下的单段真实目录(realpath 归一后仍在 .specs/ 内、
|
|
449
|
+
// 无 symlink/junction 逃逸;复用受控路径边界实现)——备份落点必须先通过该边界。
|
|
450
|
+
const changeBoundary = await inspectReentryChangeDir(changeName);
|
|
451
|
+
if (!changeBoundary.ok) {
|
|
452
|
+
const boundaryMessages = {
|
|
453
|
+
'no-active-change': '当前没有活跃 change(activeChange 缺失或为空)——先 select <change-id>',
|
|
454
|
+
'change-name-invalid': 'change 名非法 ' + JSON.stringify(changeName)
|
|
455
|
+
+ '——必须是 .specs/ 下的单段目录名(拒绝路径分隔符、. / .. 与保留目录 archive)',
|
|
456
|
+
'change-dir-missing': '.specs/' + (changeName || '<未知>') + '/ 目录不存在——重校备份必须具备真实 change 目录落点',
|
|
457
|
+
'change-dir-escape': 'change 目录路径越界或跨越符号链接 / junction(' + changeBoundary.detail
|
|
458
|
+
+ ')——受控重校与备份只允许落在 .specs/<change-id>/ 真实目录内',
|
|
459
|
+
};
|
|
460
|
+
console.error('BLOCKED: ' + (boundaryMessages[changeBoundary.reason] || ('change 目录边界异常(' + changeBoundary.reason + ')')));
|
|
461
|
+
process.exit(1);
|
|
462
|
+
}
|
|
463
|
+
const changeDir = changeBoundary.changeDir;
|
|
464
|
+
// 并行写冲突校验:replan 不做校验豁免——重校后的任务集若含
|
|
465
|
+
// 本趟可运行(依赖已满足)的并行任务且 write_files 重叠,与 plan 出口使用同一实现
|
|
466
|
+
// (route-node.findParallelWriteConflicts 单一来源)在此 BLOCK;必须早于备份与任何写盘,
|
|
467
|
+
// BLOCK 路径 state 字节零改写。read∩write 弱判与 plan 出口同风格——仅 WARN 不阻断。
|
|
468
|
+
// 同文件跨任务且无依赖路径(第三族)同样在此消费:与 plan 出口同判据、同分级(新 change
|
|
469
|
+
// BLOCKED / 旧 change WARN),消息同族(任务对 `a×b(files)` + 恢复指引「补显式 depends_on 或
|
|
470
|
+
// 合并为一个任务」)——replan 是执行期唯一合法的任务集修订通道,不得成为该形态的豁免口。
|
|
471
|
+
const { writeConflicts, crossTaskConflicts, readWarnings } = await findParallelWriteConflicts(path.join(specsDir(), changeName));
|
|
472
|
+
if (writeConflicts.length > 0) {
|
|
473
|
+
console.error('BLOCKED: replan 检测到并行写冲突('
|
|
474
|
+
+ writeConflicts.map((c) => c.a + '↔' + c.b + ': ' + c.files.join(',')).join('; ')
|
|
475
|
+
+ ')——replan 不做校验豁免,状态零改写;恢复: 调整 write_files 消除重叠后重试');
|
|
476
|
+
process.exit(1);
|
|
477
|
+
}
|
|
478
|
+
if (crossTaskConflicts.length > 0) {
|
|
479
|
+
const crossDetail = crossTaskConflicts.map((c) => c.a + '×' + c.b + '(' + c.files.join(',') + ')').join('; ');
|
|
480
|
+
const crossGuide = ';恢复: 补显式 depends_on 或合并为一个任务后重试';
|
|
481
|
+
if (state.newChange === true) {
|
|
482
|
+
console.error('BLOCKED: TASK.md 同文件跨任务且无依赖路径: ' + crossDetail
|
|
483
|
+
+ '——replan 不做校验豁免,状态零改写' + crossGuide);
|
|
484
|
+
process.exit(1);
|
|
485
|
+
}
|
|
486
|
+
console.error('WARN: TASK.md 同文件跨任务且无依赖路径(旧 change 渐进不阻断): ' + crossDetail + crossGuide);
|
|
487
|
+
}
|
|
488
|
+
if (readWarnings.length > 0) {
|
|
489
|
+
console.error('WARN: TASK.md 并行任务 read∩write 隐式依赖嫌疑(一方读取对方写路径,建议补显式 depends_on 声明): '
|
|
490
|
+
+ readWarnings.map((c) => c.a + '×' + c.b + '(' + c.files.join(',') + ')').join('; '));
|
|
491
|
+
}
|
|
492
|
+
const round = decision.round;
|
|
493
|
+
const source = decision.authorization.source;
|
|
494
|
+
const at = decision.authorization.at;
|
|
495
|
+
const continuationAuthorized = decision.continuationAuthorized === true;
|
|
496
|
+
const taskSetSignatureValue = decision.taskSetSignature;
|
|
497
|
+
if (typeof taskSetSignatureValue !== 'string' || taskSetSignatureValue === '') {
|
|
498
|
+
console.error('BLOCKED: replan 未能派生当前任务集签名——按 fail-closed 拒绝(状态零改写)。');
|
|
499
|
+
process.exit(1);
|
|
500
|
+
}
|
|
501
|
+
// 备份:重签前 state 全量快照(文件原始字节,UTF-8 BOM 按可解析形态落盘);文件名
|
|
502
|
+
// <UTC ISO 净化>-pre-replan.json;审计指纹 = 备份文件字节的 sha256(可核验)。
|
|
503
|
+
const backupDir = path.join(changeDir, 'replan-backups');
|
|
504
|
+
const backupFile = at.replace(/:/g, '-') + '-pre-replan.json';
|
|
505
|
+
const backupPath = path.join(backupDir, backupFile);
|
|
506
|
+
const backupRecord = path.relative(runRootDir(), backupPath).split(path.sep).join('/');
|
|
507
|
+
const snapshotBytes = Buffer.from((await fs.readFile(stateFile(), 'utf8')).replace(/^\uFEFF/, ''), 'utf8');
|
|
508
|
+
const fingerprint = createHash('sha256').update(snapshotBytes).digest('hex');
|
|
509
|
+
const existingHistory = Array.isArray(rawState?.history) ? rawState.history : [];
|
|
510
|
+
let backupWritten = false;
|
|
511
|
+
try {
|
|
512
|
+
if (!rawState || typeof rawState !== 'object' || Array.isArray(rawState)) {
|
|
513
|
+
throw new Error('state 文件缺失或形态非法,无法重校重签');
|
|
514
|
+
}
|
|
515
|
+
// 备份写入路径的物理包含性复核(单一权威路径段扫描):备份目录与备份文件逐段
|
|
516
|
+
// lstat + realpath——任何 symlink/junction 逃出 .specs/ 一律拒绝,不写任何字节。
|
|
517
|
+
await fs.mkdir(backupDir, { recursive: true });
|
|
518
|
+
await inspectWorkflowPathSegments(specsDir(), backupDir, 'replan backup dir', 'directory');
|
|
519
|
+
await fs.writeFile(backupPath, snapshotBytes);
|
|
520
|
+
backupWritten = true;
|
|
521
|
+
await inspectWorkflowPathSegments(specsDir(), backupPath, 'replan backup file', 'file');
|
|
522
|
+
// 双写留痕(ADR-013 决策 8):① evidence.<node>.replanAuthorization(节点级授权 + 备份
|
|
523
|
+
// 指纹);② history 追加 replan-applied(跨 change 审计检索)。落盘形态 = 原始 state
|
|
524
|
+
// 键集 + 本次实际改动的三处(taskHash / evidence / history),其余字段逐项不变。
|
|
525
|
+
const existingEvidence = rawState.evidence && typeof rawState.evidence === 'object' && !Array.isArray(rawState.evidence)
|
|
526
|
+
? rawState.evidence
|
|
527
|
+
: {};
|
|
528
|
+
const nodeEvidence = existingEvidence[decision.node]
|
|
529
|
+
&& typeof existingEvidence[decision.node] === 'object'
|
|
530
|
+
&& !Array.isArray(existingEvidence[decision.node])
|
|
531
|
+
? existingEvidence[decision.node]
|
|
532
|
+
: {};
|
|
533
|
+
const authorizationRecord = {
|
|
534
|
+
round,
|
|
535
|
+
at,
|
|
536
|
+
source,
|
|
537
|
+
reason,
|
|
538
|
+
backup: backupRecord,
|
|
539
|
+
fingerprint,
|
|
540
|
+
...(continuationAuthorized ? { continuationAuthorized: true } : {}),
|
|
541
|
+
};
|
|
542
|
+
const persisted = { ...rawState };
|
|
543
|
+
persisted.taskHash = taskSetSignatureValue;
|
|
544
|
+
persisted.evidence = {
|
|
545
|
+
...existingEvidence,
|
|
546
|
+
[decision.node]: { ...nodeEvidence, replanAuthorization: authorizationRecord },
|
|
547
|
+
};
|
|
548
|
+
persisted.history = existingHistory.concat([{
|
|
549
|
+
event: 'replan-applied',
|
|
550
|
+
change: changeName,
|
|
551
|
+
node: decision.node,
|
|
552
|
+
round,
|
|
553
|
+
reason,
|
|
554
|
+
authorizedBy: source,
|
|
555
|
+
taskSetSignature: taskSetSignatureValue,
|
|
556
|
+
backup: backupRecord,
|
|
557
|
+
fingerprint,
|
|
558
|
+
at,
|
|
559
|
+
...(continuationAuthorized ? { continuationAuthorized: true } : {}),
|
|
560
|
+
}]);
|
|
561
|
+
const persistedBad = validateStateFields(persisted);
|
|
562
|
+
if (persistedBad.length > 0) throw new Error('state 字段类型非法: ' + persistedBad[0]);
|
|
563
|
+
await writeState(persisted);
|
|
564
|
+
} catch (error) {
|
|
565
|
+
// 写盘失败:回滚本次调用创建的孤儿备份(state 由原子写保证不被截断),原始错误照常上抛。
|
|
566
|
+
if (backupWritten) {
|
|
567
|
+
try { await fs.rm(backupPath, { force: true }); } catch { /* 清理失败不掩盖原始错误 */ }
|
|
568
|
+
}
|
|
569
|
+
throw error;
|
|
570
|
+
}
|
|
571
|
+
const roundLabel = continuationAuthorized
|
|
572
|
+
? '第 ' + round + ' 轮(显式授权续轮,上限 ' + REPLAN_ROUND_LIMIT + ')'
|
|
573
|
+
: '第 ' + round + '/' + REPLAN_ROUND_LIMIT + ' 轮';
|
|
574
|
+
console.log('REPLAN: ' + decision.node + ' 重新校验通过(授权源 ' + source + ';' + roundLabel
|
|
575
|
+
+ ';备份 ' + backupRecord + ')');
|
|
576
|
+
console.log('REASON: ' + reason);
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// 节点完成判定族(单一归属):determineNode 的数据化委派与「正常推进豁免」判定。
|
|
2
|
+
import { route, resolveNextNode } from '../route-node.mjs';
|
|
3
|
+
import { runRootDir } from './state-io.mjs';
|
|
4
|
+
|
|
5
|
+
// 节点完成判定 · determineNode 数据化:完成标志从协议 outputSchemas 推导。
|
|
6
|
+
// 判定核心在共享模块 route-node.mjs——本函数保持既有签名与返回语义,仅委托 resolveNextNode
|
|
7
|
+
// (output 与抽取前一致)。route / buildNodeCompletionFlags / pathPatternExists / nodeFlagsComplete /
|
|
8
|
+
// protocolTaskFilePath / hasSubagentNode 同为该模块的单一实现(guard 与状态机共用,杜绝双实现漂移)。
|
|
9
|
+
export async function determineNode(changeName, protocol, completedNodes = []) {
|
|
10
|
+
return resolveNextNode({ runRoot: runRootDir(), changeName, protocol, completedNodes });
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// 正常推进豁免判定——exit --apply 会把 currentNode 推进到下一节点(如 open exit 后
|
|
14
|
+
// currentNode=design,该节点尚未开始故 evidence 无记录),随后按 SKILL 协议调 next(正常路径)
|
|
15
|
+
// 不应被误拦为"疑似未 exit"。判定三条件:① completedNodes 非空;② 最后一个已完成节点
|
|
16
|
+
// 存在 evidence(exit --apply 必须带证据通过——证据存在证明该 exit 真实发生,排除伪造/漂移状态,
|
|
17
|
+
// 如 review 无 evidence);③ currentNode 是下一个合法路由目标——**路由后继优先**(exit --apply
|
|
18
|
+
// 把 currentNode 推进到共享路由判定 resolveNextNode 的结果,平行转换 plan→subagent-execute、
|
|
19
|
+
// 趟间回流 subagent-execute→execute 等即由此产生;next 正常推进豁免识别路由后继,与 guard
|
|
20
|
+
// NEXT 同源),静态直接后继兜底(串行推进原语义,旧态/序列流不回归)。与回退豁免独立判断
|
|
21
|
+
// (回退 = TASK.md 有 pending 回退修复任务;本豁免 = 正常推进后继)。真乱序(currentNode
|
|
22
|
+
// 既非路由后继也非静态后继)仍维持严格 BLOCK。
|
|
23
|
+
export async function normalAdvanceExempt(state, protocol, completedNodes, currentNode, changeName) {
|
|
24
|
+
if (completedNodes.length === 0) return false;
|
|
25
|
+
const lastNodeId = completedNodes[completedNodes.length - 1];
|
|
26
|
+
const routeIds = route(protocol).map((node) => node.id);
|
|
27
|
+
const lastIdx = routeIds.indexOf(lastNodeId);
|
|
28
|
+
if (lastIdx < 0) return false;
|
|
29
|
+
const lastEvidence = state.evidence && typeof state.evidence === 'object'
|
|
30
|
+
? state.evidence[lastNodeId]
|
|
31
|
+
: null;
|
|
32
|
+
// evidence 必须是对象且含非空 summary(空对象 {} 可绕过豁免,真实流程 exit 强制 summary——
|
|
33
|
+
// 此处严格化防止手动修改 state 绕过)
|
|
34
|
+
const hasExitEvidence = !!(
|
|
35
|
+
lastEvidence &&
|
|
36
|
+
typeof lastEvidence === 'object' &&
|
|
37
|
+
!Array.isArray(lastEvidence) &&
|
|
38
|
+
typeof lastEvidence.summary === 'string' &&
|
|
39
|
+
lastEvidence.summary.trim() !== ''
|
|
40
|
+
);
|
|
41
|
+
if (!hasExitEvidence) return false;
|
|
42
|
+
// 路由后继判定(与 guard 出口同源):currentNode === resolveNextNode(completedNodes) 即
|
|
43
|
+
// 正常推进后的下一路由目标(含平行转换与趟间回流);文件推导失败(异常态)不豁免。
|
|
44
|
+
try {
|
|
45
|
+
const routingNext = await determineNode(changeName, protocol, completedNodes);
|
|
46
|
+
if (routingNext === currentNode) return true;
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
// 静态直接后继兜底(串行推进原语义,向后兼容旧态不回归)
|
|
51
|
+
return lastIdx + 1 < routeIds.length && routeIds[lastIdx + 1] === currentNode;
|
|
52
|
+
}
|