dsh-extra-plan 0.0.0-stage → 1.7.0
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/LICENSE +21 -0
- package/README.md +179 -2
- package/assets/presets/extra-plan/agent.cordis.yml +580 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-executor.generated.yml +115 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-planner.generated.yml +113 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-prober.generated.yml +107 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-reviewer.generated.yml +115 -0
- package/assets/presets/extra-plan/preset-definition.generated.yml +335 -0
- package/assets/presets/extra-plan/preset.yml +2 -0
- package/cordis.patch.yml +7 -0
- package/index.js +1671 -0
- package/lib/agent-runtime.js +172 -0
- package/lib/agent-session.js +65 -0
- package/lib/assembly-presentation.js +267 -0
- package/lib/client-carrier.js +7 -0
- package/lib/client.js +314 -0
- package/lib/extra-plan-gate-tool.js +387 -0
- package/lib/gate-decisions.js +744 -0
- package/lib/gate-words.js +108 -0
- package/lib/live-config.js +293 -0
- package/lib/log-retention.js +132 -0
- package/lib/model-routing.js +648 -0
- package/lib/path-policy.js +464 -0
- package/lib/plan-draft-tool.js +294 -0
- package/lib/planner-budget.js +136 -0
- package/lib/preset-defaults.generated.js +17 -0
- package/lib/preset-settings.js +3 -0
- package/lib/preset-sync.js +809 -0
- package/lib/preset-yaml.js +438 -0
- package/lib/role-contract.js +11 -0
- package/lib/run-code-scanner.js +360 -0
- package/lib/run-code-static.js +621 -0
- package/lib/runtime-diagnostics.js +89 -0
- package/lib/runtime-lifecycle.js +162 -0
- package/lib/runtime-static.js +23 -0
- package/lib/save-contract.js +263 -0
- package/lib/save-persistence.js +223 -0
- package/lib/save-plan-markdown-parser.js +369 -0
- package/lib/save-plan-validation-diagnostics.js +49 -0
- package/lib/save-plan-validation.js +812 -0
- package/lib/save-probe-validation.js +173 -0
- package/lib/save-source-verification.js +195 -0
- package/lib/save-tool-factories.js +270 -0
- package/lib/sdk-text-cache.js +161 -0
- package/lib/settings-contract.js +191 -0
- package/lib/settings.js +369 -0
- package/lib/shell-mutation.js +484 -0
- package/lib/usage-ledger-background.js +214 -0
- package/lib/usage-ledger-worker.js +116 -0
- package/lib/usage-ledger.js +1393 -0
- package/locale/en.json +6 -0
- package/locale/settings/en.json +6 -0
- package/locale/settings/zh.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +56 -4
- package/scripts/generate-runtime-defaults.mjs +796 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync, renameSync, existsSync, unlinkSync, readFileSync, lstatSync, fstatSync, openSync, closeSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { resolveWorkspacePath, prepareWriteTarget, validateReadTarget } from './path-policy.js'
|
|
4
|
+
import { errorText } from './runtime-static.js'
|
|
5
|
+
|
|
6
|
+
// 单次调用的临时写入与顺序发布;不创建、不扫描、不恢复跨调用 journal。
|
|
7
|
+
// fsOps 仅供隔离测试注入,默认操作冻结以避免夹具修改共享依赖。
|
|
8
|
+
const DEFAULT_FS_OPS = Object.freeze({ mkdirSync, writeFileSync, renameSync, existsSync, unlinkSync, readFileSync, lstatSync, fstatSync, openSync, closeSync })
|
|
9
|
+
|
|
10
|
+
function fsOpsOf(overrides) {
|
|
11
|
+
if (overrides === null || overrides === undefined || typeof overrides !== 'object') return DEFAULT_FS_OPS
|
|
12
|
+
return { ...DEFAULT_FS_OPS, ...overrides }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function policyOf(options) {
|
|
16
|
+
return options !== null && typeof options === 'object' ? options : {}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function authorizeDirectory(dir, pathContext, purpose) {
|
|
20
|
+
if (pathContext === undefined || pathContext === null) return null
|
|
21
|
+
const target = resolveWorkspacePath(pathContext, dir, { purpose })
|
|
22
|
+
return prepareWriteTarget(pathContext, target, { expectedType: 'any', purpose })
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function authorizeFile(path, pathContext, purpose) {
|
|
26
|
+
if (pathContext === undefined || pathContext === null) return null
|
|
27
|
+
const target = resolveWorkspacePath(pathContext, path, { purpose })
|
|
28
|
+
return prepareWriteTarget(pathContext, target, { expectedType: 'file', purpose })
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function cleanupCreated(paths, ops, pathContext, purpose) {
|
|
32
|
+
const failures = []
|
|
33
|
+
for (const path of paths) {
|
|
34
|
+
try {
|
|
35
|
+
authorizeFile(path, pathContext, purpose)
|
|
36
|
+
if (ops.existsSync(path)) ops.unlinkSync(path)
|
|
37
|
+
} catch (error) {
|
|
38
|
+
failures.push(path + '(' + errorText(error) + ')')
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return failures
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function commitError(stage, original, cleanupFailures) {
|
|
45
|
+
const prefix = 'atomicCommit: 阶段=' + stage + ';原始失败=' + errorText(original)
|
|
46
|
+
const suffix = cleanupFailures.length === 0 ? '' : ';清理失败:' + cleanupFailures.join(';')
|
|
47
|
+
const wrapped = new Error(prefix + suffix)
|
|
48
|
+
wrapped.cause = original
|
|
49
|
+
return wrapped
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 目录创建 → 逐个临时写入 → 逐个检查并发布。
|
|
53
|
+
// 目标存在时立即拒绝,发布失败只清理本次已尝试的临时文件/已发布文件;
|
|
54
|
+
// 预存目标与目录外历史文件不进入清理集合。
|
|
55
|
+
export function atomicCommit(dir, base, files, fsOps, policyOptions) {
|
|
56
|
+
const ops = fsOpsOf(fsOps)
|
|
57
|
+
const policy = policyOf(policyOptions)
|
|
58
|
+
const pathContext = policy.pathContext
|
|
59
|
+
const purpose = typeof policy.purpose === 'string' && policy.purpose !== '' ? policy.purpose : 'save-artifacts'
|
|
60
|
+
const authorizedDir = authorizeDirectory(dir, pathContext, purpose)
|
|
61
|
+
const suffix = '.tmp-' + process.pid + '-' + Date.now()
|
|
62
|
+
const entries = files.map((file) => ({
|
|
63
|
+
tmp: join(dir, file.name + suffix),
|
|
64
|
+
file: join(dir, file.name),
|
|
65
|
+
content: file.content,
|
|
66
|
+
}))
|
|
67
|
+
const attemptedTemps = []
|
|
68
|
+
const published = []
|
|
69
|
+
let stage = '准备'
|
|
70
|
+
try {
|
|
71
|
+
stage = '目录授权'
|
|
72
|
+
if (pathContext === undefined || pathContext === null) throw new Error('atomicCommit: 缺少workspace授权上下文')
|
|
73
|
+
if (authorizedDir === null) throw new Error('atomicCommit: 缺少workspace授权上下文')
|
|
74
|
+
stage = '目录创建'
|
|
75
|
+
ops.mkdirSync(dir, { recursive: true })
|
|
76
|
+
if (pathContext !== undefined && pathContext !== null) validateReadTarget(pathContext, resolveWorkspacePath(pathContext, dir, { purpose }), { expectedType: 'directory', purpose })
|
|
77
|
+
stage = '准备目标'
|
|
78
|
+
for (const entry of entries) {
|
|
79
|
+
authorizeFile(entry.file, pathContext, purpose)
|
|
80
|
+
authorizeFile(entry.tmp, pathContext, purpose)
|
|
81
|
+
if (ops.existsSync(entry.file)) throw new Error('预存目标已存在:' + entry.file)
|
|
82
|
+
if (ops.existsSync(entry.tmp)) throw new Error('临时文件已存在:' + entry.tmp)
|
|
83
|
+
}
|
|
84
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
85
|
+
const entry = entries[index]
|
|
86
|
+
stage = '临时写入' + (index + 1)
|
|
87
|
+
authorizeFile(entry.tmp, pathContext, purpose)
|
|
88
|
+
attemptedTemps.push(entry.tmp)
|
|
89
|
+
ops.writeFileSync(entry.tmp, entry.content, 'utf8')
|
|
90
|
+
}
|
|
91
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
92
|
+
const entry = entries[index]
|
|
93
|
+
stage = '发布' + (index + 1)
|
|
94
|
+
authorizeFile(entry.file, pathContext, purpose)
|
|
95
|
+
if (ops.existsSync(entry.file)) throw new Error('发布前目标已存在:' + entry.file)
|
|
96
|
+
ops.renameSync(entry.tmp, entry.file)
|
|
97
|
+
published.push(entry.file)
|
|
98
|
+
authorizeFile(entry.file, pathContext, purpose)
|
|
99
|
+
if (!ops.existsSync(entry.file)) throw new Error('发布后目标未就位:' + entry.file)
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {
|
|
102
|
+
const cleanupFailures = cleanupCreated([...attemptedTemps, ...published], ops, pathContext, purpose)
|
|
103
|
+
throw commitError(stage, error, cleanupFailures)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function replaceError(stage, original, cleanupFailures, restoreFailures) {
|
|
108
|
+
const parts = ['atomicReplace: 阶段=' + stage + ';原始失败=' + errorText(original)]
|
|
109
|
+
if (restoreFailures.length !== 0) parts.push('恢复失败:' + restoreFailures.join(';'))
|
|
110
|
+
if (cleanupFailures.length !== 0) parts.push('清理失败:' + cleanupFailures.join(';'))
|
|
111
|
+
return new Error(parts.join(';'), { cause: original })
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function sameFile(left, right) {
|
|
115
|
+
return left !== undefined && right !== undefined && left.isFile() && right.isFile()
|
|
116
|
+
&& left.ino > 0n && left.ino === right.ino && left.dev === right.dev
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function stagePairFile(ops, path, content, pending) {
|
|
120
|
+
const fd = ops.openSync(path, 'wx')
|
|
121
|
+
pending.set(path, undefined)
|
|
122
|
+
let failed = false
|
|
123
|
+
try {
|
|
124
|
+
pending.set(path, ops.fstatSync(fd, { bigint: true }))
|
|
125
|
+
ops.writeFileSync(fd, content, 'utf8')
|
|
126
|
+
} catch (error) {
|
|
127
|
+
failed = true
|
|
128
|
+
throw error
|
|
129
|
+
} finally {
|
|
130
|
+
try { ops.closeSync(fd) } catch (error) { if (!failed) throw error }
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// 成对覆盖只在本次调用中保留旧字节;不承诺外部并发或跨进程崩溃原子性。
|
|
135
|
+
export function atomicReplace(dir, files, fsOps, policyOptions) {
|
|
136
|
+
const ops = fsOpsOf(fsOps)
|
|
137
|
+
const policy = policyOf(policyOptions)
|
|
138
|
+
const pathContext = policy.pathContext
|
|
139
|
+
const purpose = typeof policy.purpose === 'string' && policy.purpose !== '' ? policy.purpose : 'save-artifacts'
|
|
140
|
+
const authorizedDir = authorizeDirectory(dir, pathContext, purpose)
|
|
141
|
+
const suffix = '.tmp-' + process.pid + '-' + Date.now()
|
|
142
|
+
const entries = files.map((file) => ({
|
|
143
|
+
tmp: join(dir, file.name + suffix),
|
|
144
|
+
file: join(dir, file.name),
|
|
145
|
+
content: file.content,
|
|
146
|
+
}))
|
|
147
|
+
const paired = entries.length === 2
|
|
148
|
+
const attemptedTemps = []
|
|
149
|
+
const pending = new Map()
|
|
150
|
+
const published = []
|
|
151
|
+
let stage = '准备'
|
|
152
|
+
try {
|
|
153
|
+
stage = '目录授权'
|
|
154
|
+
if (pathContext === undefined || pathContext === null) throw new Error('atomicReplace: 缺少workspace授权上下文')
|
|
155
|
+
if (authorizedDir === null) throw new Error('atomicReplace: 缺少workspace授权上下文')
|
|
156
|
+
stage = '目录创建'
|
|
157
|
+
ops.mkdirSync(dir, { recursive: true })
|
|
158
|
+
if (pathContext !== undefined && pathContext !== null) validateReadTarget(pathContext, resolveWorkspacePath(pathContext, dir, { purpose }), { expectedType: 'directory', purpose })
|
|
159
|
+
stage = '准备目标'
|
|
160
|
+
for (const entry of entries) {
|
|
161
|
+
const target = authorizeFile(entry.file, pathContext, purpose)
|
|
162
|
+
authorizeFile(entry.tmp, pathContext, purpose)
|
|
163
|
+
if (paired) entry.previous = target.exists ? ops.readFileSync(target.realPath) : null
|
|
164
|
+
}
|
|
165
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
166
|
+
const entry = entries[index]
|
|
167
|
+
stage = '临时写入' + (index + 1)
|
|
168
|
+
authorizeFile(entry.tmp, pathContext, purpose)
|
|
169
|
+
if (paired) stagePairFile(ops, entry.tmp, entry.content, pending)
|
|
170
|
+
else {
|
|
171
|
+
attemptedTemps.push(entry.tmp)
|
|
172
|
+
ops.writeFileSync(entry.tmp, entry.content, 'utf8')
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
176
|
+
const entry = entries[index]
|
|
177
|
+
stage = '覆盖' + (index + 1)
|
|
178
|
+
authorizeFile(entry.file, pathContext, purpose)
|
|
179
|
+
if (paired) entry.identity = pending.get(entry.tmp)
|
|
180
|
+
ops.renameSync(entry.tmp, entry.file)
|
|
181
|
+
if (paired) {
|
|
182
|
+
published.push(entry)
|
|
183
|
+
pending.delete(entry.tmp)
|
|
184
|
+
}
|
|
185
|
+
authorizeFile(entry.file, pathContext, purpose)
|
|
186
|
+
if (!ops.existsSync(entry.file)) throw new Error('覆盖后目标未就位:' + entry.file)
|
|
187
|
+
}
|
|
188
|
+
} catch (error) {
|
|
189
|
+
const restoreFailures = []
|
|
190
|
+
for (const entry of published.reverse()) {
|
|
191
|
+
try {
|
|
192
|
+
const target = authorizeFile(entry.file, pathContext, purpose)
|
|
193
|
+
if (!target.exists && entry.previous === null) continue
|
|
194
|
+
if (!target.exists || !sameFile(entry.identity, ops.lstatSync(entry.file, { bigint: true }))) throw new Error('无法确认是本轮发布,保留现场')
|
|
195
|
+
if (entry.previous === null) {
|
|
196
|
+
ops.unlinkSync(entry.file)
|
|
197
|
+
if (authorizeFile(entry.file, pathContext, purpose).exists) throw new Error('本轮新增目标未恢复为缺席')
|
|
198
|
+
} else {
|
|
199
|
+
const restoreTmp = entry.tmp + '-restore'
|
|
200
|
+
authorizeFile(restoreTmp, pathContext, purpose)
|
|
201
|
+
stagePairFile(ops, restoreTmp, entry.previous, pending)
|
|
202
|
+
ops.renameSync(restoreTmp, entry.file)
|
|
203
|
+
pending.delete(restoreTmp)
|
|
204
|
+
const restored = authorizeFile(entry.file, pathContext, purpose)
|
|
205
|
+
if (!restored.exists || !ops.readFileSync(restored.realPath).equals(entry.previous)) throw new Error('原始字节未恢复')
|
|
206
|
+
}
|
|
207
|
+
} catch (restoreError) {
|
|
208
|
+
restoreFailures.push(entry.file + '(' + errorText(restoreError) + ')')
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const cleanupFailures = cleanupCreated(attemptedTemps, ops, pathContext, purpose)
|
|
212
|
+
for (const [temp, identity] of pending) {
|
|
213
|
+
try {
|
|
214
|
+
if (!authorizeFile(temp, pathContext, purpose).exists) continue
|
|
215
|
+
if (!sameFile(identity, ops.lstatSync(temp, { bigint: true }))) throw new Error('无法确认本轮临时文件,保留现场')
|
|
216
|
+
ops.unlinkSync(temp)
|
|
217
|
+
} catch (cleanupError) {
|
|
218
|
+
cleanupFailures.push(temp + '(' + errorText(cleanupError) + ')')
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
throw replaceError(stage, error, cleanupFailures, restoreFailures)
|
|
222
|
+
}
|
|
223
|
+
}
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import { makeDiagnostic } from './save-plan-validation-diagnostics.js'
|
|
2
|
+
|
|
3
|
+
const TASK_HEADING_RE = /^\[任务([1-9]\d*)\][ \t]+(.+?)\s*$/
|
|
4
|
+
const TASK_CANDIDATE_RE = /^\[任务([^\]]*)\]/
|
|
5
|
+
const CHECK_HEADING_RE = /^\[检查([1-9]\d*)\][ \t]+(.+?)\s*$/
|
|
6
|
+
const CHECK_CANDIDATE_RE = /^\[检查([^\]]*)\]/
|
|
7
|
+
const CLOSURE_HEADING_RE = /^\[收尾\][ \t]*$/
|
|
8
|
+
const GLOBAL_HEADING_RE = /^\[全局验收\][ \t]*$/
|
|
9
|
+
const CLOSURE_CANDIDATE_RE = /^\[收尾/
|
|
10
|
+
const GLOBAL_CANDIDATE_RE = /^\[全局验收/
|
|
11
|
+
|
|
12
|
+
const DISPATCH_PREFIX = '- 派遣说明:'
|
|
13
|
+
const REVIEW_PREFIX = '- 验收说明:'
|
|
14
|
+
|
|
15
|
+
const PLAN_FIELDS_BY_KIND = new Map([
|
|
16
|
+
['task', new Set(['mainFiles', 'relatedFiles', 'fileOperations', 'modificationPoints', 'modificationLocations', 'anchors', 'generationSources', 'generationCommands', 'generationOutputs', 'groups', 'dependencyFields', 'dependencyReasons', 'groupReasons', 'modifications', 'selfChecks', 'resources'])],
|
|
17
|
+
['closure', new Set(['closureFiles', 'fileOperations', 'modificationPoints', 'modificationLocations', 'anchors', 'generationSources', 'generationCommands', 'generationOutputs', 'closureDependencies', 'closureActions', 'selfChecks', 'resources'])],
|
|
18
|
+
])
|
|
19
|
+
const CHECKLIST_FIELDS_BY_KIND = new Map([
|
|
20
|
+
['task', new Set(['mainFiles', 'relatedFiles', 'fileOperations', 'acceptancePointRefs', 'acceptanceLocations', 'expectations', 'prohibitions', 'formalAcceptance'])],
|
|
21
|
+
['closure', new Set(['closureFiles', 'fileOperations', 'acceptancePointRefs', 'acceptanceLocations', 'expectations', 'prohibitions', 'formalAcceptance'])],
|
|
22
|
+
['check', new Set(['reviewGroups', 'evidenceModes', 'workdirs', 'checks', 'evidence', 'resources'])],
|
|
23
|
+
['global', new Set(['expectations', 'prohibitions', 'formalAcceptance'])],
|
|
24
|
+
])
|
|
25
|
+
const EMPTY_STRUCTURAL_FIELDS = new Set()
|
|
26
|
+
|
|
27
|
+
const FIELD_DEFINITIONS = Object.freeze([
|
|
28
|
+
['mainFiles', '主文件', '- 主文件:'],
|
|
29
|
+
['relatedFiles', '关联文件', '- 关联文件:'],
|
|
30
|
+
['fileOperations', '文件操作', '- 文件操作:'],
|
|
31
|
+
['modificationPoints', '修改点', '- 修改点:'],
|
|
32
|
+
['modificationLocations', '修改位置', '- 修改位置:'],
|
|
33
|
+
['anchors', '定位锚点', '- 定位锚点:'],
|
|
34
|
+
['generationSources', '生成来源', '- 生成来源:'],
|
|
35
|
+
['generationCommands', '生成命令', '- 生成命令:'],
|
|
36
|
+
['generationOutputs', '生成产物', '- 生成产物:'],
|
|
37
|
+
['acceptancePointRefs', '对应修改点', '- 对应修改点:'],
|
|
38
|
+
['acceptanceLocations', '验收定位', '- 验收定位:'],
|
|
39
|
+
['groups', '执行组', '- 执行组:'],
|
|
40
|
+
['dependencyFields', '依赖', '- 依赖:'],
|
|
41
|
+
['dependencyReasons', '依赖理由', '- 依赖理由:'],
|
|
42
|
+
['groupReasons', '分组理由', '- 分组理由:'],
|
|
43
|
+
['modifications', '修改', '- 修改:'],
|
|
44
|
+
['selfChecks', '自检', '- 自检:'],
|
|
45
|
+
['resources', '独占资源', '- 独占资源:'],
|
|
46
|
+
['expectations', '期望', '- 期望:'],
|
|
47
|
+
['prohibitions', '禁止', '- 禁止:'],
|
|
48
|
+
['formalAcceptance', '正式验收', '- 正式验收:'],
|
|
49
|
+
['closureFiles', '收尾文件', '- 收尾文件:'],
|
|
50
|
+
['closureDependencies', '收尾依赖', '- 收尾依赖:'],
|
|
51
|
+
['closureActions', '收尾动作', '- 收尾动作:'],
|
|
52
|
+
['reviewGroups', '验收组', '- 验收组:'],
|
|
53
|
+
['evidenceModes', '取证方式', '- 取证方式:'],
|
|
54
|
+
['workdirs', '工作目录', '- 工作目录:'],
|
|
55
|
+
['checks', '检查', '- 检查:'],
|
|
56
|
+
['evidence', '验收证据', '- 验收证据:']
|
|
57
|
+
])
|
|
58
|
+
|
|
59
|
+
function newFileRecord(path, role) {
|
|
60
|
+
return { path, role, operations: [], points: [], currentPoint: null }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function newTask(id, title, line) {
|
|
64
|
+
return {
|
|
65
|
+
kind: 'task', id, title, line,
|
|
66
|
+
mainFiles: [], relatedFiles: [], files: [], currentFile: null,
|
|
67
|
+
groups: [], dependencyFields: [], dependencyReasons: [],
|
|
68
|
+
groupReasons: [], modifications: [], selfChecks: [], resources: [], expectations: [],
|
|
69
|
+
prohibitions: [], formalAcceptance: [], checkRefs: [], dependencies: [], resourceIds: [],
|
|
70
|
+
fileOperations: [], modificationPoints: [], modificationLocations: [], anchors: [],
|
|
71
|
+
generationSources: [], generationCommands: [], generationOutputs: [],
|
|
72
|
+
acceptancePointRefs: [], acceptanceLocations: [],
|
|
73
|
+
fileSet: new Set(), fileKeys: new Set()
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function newClosure(line) {
|
|
78
|
+
return {
|
|
79
|
+
kind: 'closure', line, closureFiles: [], files: [], currentFile: null,
|
|
80
|
+
closureDependencies: [], closureActions: [],
|
|
81
|
+
selfChecks: [], resources: [], expectations: [], prohibitions: [], formalAcceptance: [],
|
|
82
|
+
checkRefs: [], dependencies: [], resourceIds: [],
|
|
83
|
+
fileOperations: [], modificationPoints: [], modificationLocations: [], anchors: [],
|
|
84
|
+
generationSources: [], generationCommands: [], generationOutputs: [],
|
|
85
|
+
acceptancePointRefs: [], acceptanceLocations: [],
|
|
86
|
+
fileSet: new Set(), fileKeys: new Set()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
function newCheck(id, title, line) {
|
|
92
|
+
return {
|
|
93
|
+
kind: 'check', id, title, line, reviewGroups: [], evidenceModes: [], workdirs: [],
|
|
94
|
+
checks: [], evidence: [], resources: [], resourceIds: [], checkRefs: [],
|
|
95
|
+
coverageTasks: [], coverageClosure: false, coverageGlobal: false
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function newGlobal(line) {
|
|
100
|
+
return { kind: 'global', line, expectations: [], prohibitions: [], formalAcceptance: [], checkRefs: [] }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function normalizeText(text) {
|
|
104
|
+
return typeof text === 'string' ? text.replace(/\r\n?/g, '\n') : ''
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function fieldOf(line) {
|
|
108
|
+
for (const [key, label, prefix] of FIELD_DEFINITIONS) {
|
|
109
|
+
if (line.startsWith(prefix)) return { key, label, value: line.slice(prefix.length).trim() }
|
|
110
|
+
}
|
|
111
|
+
return null
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const FILE_PATH_FIELD_KEYS = new Set(['mainFiles', 'relatedFiles', 'closureFiles'])
|
|
115
|
+
const POINT_FIELD_KEYS = new Set(['modificationLocations', 'anchors', 'generationSources', 'generationCommands', 'generationOutputs', 'acceptanceLocations'])
|
|
116
|
+
|
|
117
|
+
function headingCandidate(line) {
|
|
118
|
+
const task = line.match(TASK_HEADING_RE)
|
|
119
|
+
if (task !== null) return { kind: 'task', id: Number(task[1]), title: task[2].trim() }
|
|
120
|
+
const check = line.match(CHECK_HEADING_RE)
|
|
121
|
+
if (check !== null) return { kind: 'check', id: Number(check[1]), title: check[2].trim() }
|
|
122
|
+
if (CLOSURE_HEADING_RE.test(line)) return { kind: 'closure' }
|
|
123
|
+
if (GLOBAL_HEADING_RE.test(line)) return { kind: 'global' }
|
|
124
|
+
if (TASK_CANDIDATE_RE.test(line)) return { kind: 'bad-task', value: line }
|
|
125
|
+
if (CHECK_CANDIDATE_RE.test(line)) return { kind: 'bad-check', value: line }
|
|
126
|
+
if (CLOSURE_CANDIDATE_RE.test(line)) return { kind: 'bad-closure', value: line }
|
|
127
|
+
if (GLOBAL_CANDIDATE_RE.test(line)) return { kind: 'bad-global', value: line }
|
|
128
|
+
return null
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function pushEmptyFieldDiagnostic(document, current, field) {
|
|
132
|
+
if (field.value !== '') return
|
|
133
|
+
const taskId = current.kind === 'task' ? current.id : null
|
|
134
|
+
return makeDiagnostic(document, taskId, field.label, field.value, '结构字段不能为空')
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function parseTaskDocument(text, document) {
|
|
138
|
+
const isPlan = document === 'plan'
|
|
139
|
+
const lines = normalizeText(text).split('\n')
|
|
140
|
+
const tasks = []
|
|
141
|
+
const checks = []
|
|
142
|
+
const diagnostics = []
|
|
143
|
+
const instructionEntries = []
|
|
144
|
+
let closure = null
|
|
145
|
+
let global = null
|
|
146
|
+
let current = null
|
|
147
|
+
let stage = 0
|
|
148
|
+
let fence = ''
|
|
149
|
+
|
|
150
|
+
const flush = () => {
|
|
151
|
+
if (current === null) return
|
|
152
|
+
if (current.kind === 'task') tasks.push(current)
|
|
153
|
+
else if (current.kind === 'check') checks.push(current)
|
|
154
|
+
else if (current.kind === 'closure') closure = current
|
|
155
|
+
else if (current.kind === 'global') global = current
|
|
156
|
+
current = null
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const wrongHeading = (kind, line, message) => {
|
|
160
|
+
diagnostics.push(makeDiagnostic(document, null, kind, line, message))
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const pushField = (owner, field) => {
|
|
164
|
+
owner[field.key].push(field.value)
|
|
165
|
+
const emptyDiagnostic = pushEmptyFieldDiagnostic(document, owner, field)
|
|
166
|
+
if (emptyDiagnostic !== undefined) diagnostics.push(emptyDiagnostic)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const requireFile = (field) => {
|
|
170
|
+
if (current.currentFile !== null) return current.currentFile
|
|
171
|
+
diagnostics.push(makeDiagnostic(document, current.kind === 'task' ? current.id : null, field.label, field.value, '文件级字段必须紧跟在主文件、关联文件或收尾文件之后'))
|
|
172
|
+
return null
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const requirePoint = (field) => {
|
|
176
|
+
const file = requireFile(field)
|
|
177
|
+
if (file === null) return null
|
|
178
|
+
if (file.currentPoint !== null) return file.currentPoint
|
|
179
|
+
diagnostics.push(makeDiagnostic(document, current.kind === 'task' ? current.id : null, field.label, field.value, '定位字段必须先声明对应的修改点或验收对应修改点'))
|
|
180
|
+
return null
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
184
|
+
const line = lines[index]
|
|
185
|
+
if (fence !== '') {
|
|
186
|
+
if (line.startsWith(fence + fence + fence)) fence = ''
|
|
187
|
+
continue
|
|
188
|
+
}
|
|
189
|
+
if (line.startsWith(String.fromCharCode(96).repeat(3))) { fence = String.fromCharCode(96); continue }
|
|
190
|
+
if (line.startsWith('~~~')) { fence = '~'; continue }
|
|
191
|
+
if (line !== line.trimStart()) continue
|
|
192
|
+
|
|
193
|
+
const instructionPrefix = isPlan ? DISPATCH_PREFIX : REVIEW_PREFIX
|
|
194
|
+
if (line.startsWith(instructionPrefix)) {
|
|
195
|
+
const value = line.slice(instructionPrefix.length).trim()
|
|
196
|
+
const label = isPlan ? '派遣说明' : '验收说明'
|
|
197
|
+
if (stage !== 0) diagnostics.push(makeDiagnostic(document, null, label, value, label + '必须位于首个任务前'))
|
|
198
|
+
instructionEntries.push({ value, line: index + 1 })
|
|
199
|
+
if (value === '') diagnostics.push(makeDiagnostic(document, null, label, value, label + '不能为空'))
|
|
200
|
+
continue
|
|
201
|
+
}
|
|
202
|
+
const wrongInstructionPrefix = isPlan ? REVIEW_PREFIX : DISPATCH_PREFIX
|
|
203
|
+
if (line.startsWith(wrongInstructionPrefix)) {
|
|
204
|
+
const value = line.slice(wrongInstructionPrefix.length).trim()
|
|
205
|
+
const label = isPlan ? '验收说明' : '派遣说明'
|
|
206
|
+
const message = isPlan ? '方案只能声明派遣说明,验收说明属于验收清单' : '验收清单只能声明验收说明,派遣说明属于方案'
|
|
207
|
+
diagnostics.push(makeDiagnostic(document, null, label, value, message))
|
|
208
|
+
continue
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const heading = headingCandidate(line)
|
|
212
|
+
if (heading !== null) {
|
|
213
|
+
flush()
|
|
214
|
+
if (heading.kind === 'task') {
|
|
215
|
+
if (stage > 1) {
|
|
216
|
+
wrongHeading('任务块', line, '数字任务必须位于收尾、检查和全局验收之前')
|
|
217
|
+
continue
|
|
218
|
+
}
|
|
219
|
+
stage = 1
|
|
220
|
+
current = newTask(heading.id, heading.title, index + 1)
|
|
221
|
+
continue
|
|
222
|
+
}
|
|
223
|
+
if (heading.kind === 'check') {
|
|
224
|
+
if (isPlan) {
|
|
225
|
+
wrongHeading('检查块', line, '方案不得包含检查定义')
|
|
226
|
+
continue
|
|
227
|
+
}
|
|
228
|
+
if (stage < 1 || stage > 3) {
|
|
229
|
+
wrongHeading('检查块', line, '检查定义必须位于数字任务和收尾之后、全局验收之前')
|
|
230
|
+
continue
|
|
231
|
+
}
|
|
232
|
+
stage = 3
|
|
233
|
+
current = newCheck(heading.id, heading.title, index + 1)
|
|
234
|
+
continue
|
|
235
|
+
}
|
|
236
|
+
if (heading.kind === 'closure') {
|
|
237
|
+
if (stage !== 1 || closure !== null) {
|
|
238
|
+
wrongHeading('收尾块', line, '收尾必须至多一个且位于全部数字任务之后')
|
|
239
|
+
continue
|
|
240
|
+
}
|
|
241
|
+
stage = 2
|
|
242
|
+
current = newClosure(index + 1)
|
|
243
|
+
continue
|
|
244
|
+
}
|
|
245
|
+
if (heading.kind === 'global') {
|
|
246
|
+
if (isPlan) {
|
|
247
|
+
wrongHeading('全局验收块', line, '方案不得包含全局验收')
|
|
248
|
+
continue
|
|
249
|
+
}
|
|
250
|
+
if (stage !== 3 || global !== null) {
|
|
251
|
+
wrongHeading('全局验收块', line, '全局验收必须至多一个且位于全部检查定义之后')
|
|
252
|
+
continue
|
|
253
|
+
}
|
|
254
|
+
stage = 4
|
|
255
|
+
current = newGlobal(index + 1)
|
|
256
|
+
continue
|
|
257
|
+
}
|
|
258
|
+
if (heading.kind === 'bad-task') {
|
|
259
|
+
wrongHeading('任务块', line, '任务块必须是列首“[任务正整数] 标题”,标题不能为空')
|
|
260
|
+
continue
|
|
261
|
+
}
|
|
262
|
+
if (heading.kind === 'bad-check') {
|
|
263
|
+
wrongHeading('检查块', line, '检查块必须是列首“[检查正整数] 标题”,标题不能为空')
|
|
264
|
+
continue
|
|
265
|
+
}
|
|
266
|
+
if (heading.kind === 'bad-closure') {
|
|
267
|
+
wrongHeading('收尾块', line, '收尾块必须严格写作“[收尾]”')
|
|
268
|
+
continue
|
|
269
|
+
}
|
|
270
|
+
if (heading.kind === 'bad-global') {
|
|
271
|
+
wrongHeading('全局验收块', line, '全局验收块必须严格写作“[全局验收]”')
|
|
272
|
+
continue
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const field = fieldOf(line)
|
|
277
|
+
if (field === null) continue
|
|
278
|
+
if (current === null) {
|
|
279
|
+
diagnostics.push(makeDiagnostic(document, null, field.label, field.value, '结构字段必须位于所属结构块内'))
|
|
280
|
+
continue
|
|
281
|
+
}
|
|
282
|
+
const allowed = (isPlan ? PLAN_FIELDS_BY_KIND : CHECKLIST_FIELDS_BY_KIND).get(current.kind) ?? EMPTY_STRUCTURAL_FIELDS
|
|
283
|
+
if (!allowed.has(field.key)) {
|
|
284
|
+
const taskId = current.kind === 'task' ? current.id : null
|
|
285
|
+
diagnostics.push(makeDiagnostic(document, taskId, field.label, field.value, '字段出现在错误结构块或职责位置'))
|
|
286
|
+
continue
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (FILE_PATH_FIELD_KEYS.has(field.key)) {
|
|
290
|
+
pushField(current, field)
|
|
291
|
+
const role = field.key === 'mainFiles' ? 'main' : field.key === 'relatedFiles' ? 'related' : 'closure'
|
|
292
|
+
const file = newFileRecord(field.value, role)
|
|
293
|
+
current.files.push(file)
|
|
294
|
+
current.currentFile = file
|
|
295
|
+
continue
|
|
296
|
+
}
|
|
297
|
+
if (field.key === 'fileOperations') {
|
|
298
|
+
pushField(current, field)
|
|
299
|
+
const file = requireFile(field)
|
|
300
|
+
if (file !== null) file.operations.push(field.value)
|
|
301
|
+
continue
|
|
302
|
+
}
|
|
303
|
+
if (isPlan && field.key === 'modificationPoints') {
|
|
304
|
+
pushField(current, field)
|
|
305
|
+
const file = requireFile(field)
|
|
306
|
+
if (file !== null) {
|
|
307
|
+
const point = { id: field.value, locations: [], anchors: [], actions: [], sources: [], commands: [], outputs: [] }
|
|
308
|
+
file.points.push(point)
|
|
309
|
+
file.currentPoint = point
|
|
310
|
+
}
|
|
311
|
+
continue
|
|
312
|
+
}
|
|
313
|
+
if (!isPlan && field.key === 'acceptancePointRefs') {
|
|
314
|
+
pushField(current, field)
|
|
315
|
+
const file = requireFile(field)
|
|
316
|
+
if (file !== null) {
|
|
317
|
+
const point = { id: field.value, locations: [], expectations: [] }
|
|
318
|
+
file.points.push(point)
|
|
319
|
+
file.currentPoint = point
|
|
320
|
+
}
|
|
321
|
+
continue
|
|
322
|
+
}
|
|
323
|
+
if (POINT_FIELD_KEYS.has(field.key)) {
|
|
324
|
+
pushField(current, field)
|
|
325
|
+
const point = requirePoint(field)
|
|
326
|
+
if (point !== null) {
|
|
327
|
+
if (field.key === 'modificationLocations' || field.key === 'acceptanceLocations') point.locations.push(field.value)
|
|
328
|
+
else if (field.key === 'anchors') point.anchors.push(field.value)
|
|
329
|
+
else if (field.key === 'generationSources') point.sources.push(field.value)
|
|
330
|
+
else if (field.key === 'generationCommands') point.commands.push(field.value)
|
|
331
|
+
else if (field.key === 'generationOutputs') point.outputs.push(field.value)
|
|
332
|
+
}
|
|
333
|
+
continue
|
|
334
|
+
}
|
|
335
|
+
if (field.key === 'modifications') {
|
|
336
|
+
pushField(current, field)
|
|
337
|
+
const point = requirePoint(field)
|
|
338
|
+
if (point !== null) point.actions.push(field.value)
|
|
339
|
+
continue
|
|
340
|
+
}
|
|
341
|
+
if (field.key === 'closureActions') {
|
|
342
|
+
pushField(current, field)
|
|
343
|
+
if (current.kind === 'closure' && current.currentFile !== null) {
|
|
344
|
+
const point = requirePoint(field)
|
|
345
|
+
if (point !== null) point.actions.push(field.value)
|
|
346
|
+
}
|
|
347
|
+
continue
|
|
348
|
+
}
|
|
349
|
+
if (field.key === 'expectations') {
|
|
350
|
+
pushField(current, field)
|
|
351
|
+
if (!isPlan && (current.kind === 'task' || current.kind === 'closure') && current.currentFile !== null) {
|
|
352
|
+
const point = requirePoint(field)
|
|
353
|
+
if (point !== null) point.expectations.push(field.value)
|
|
354
|
+
}
|
|
355
|
+
continue
|
|
356
|
+
}
|
|
357
|
+
pushField(current, field)
|
|
358
|
+
}
|
|
359
|
+
flush()
|
|
360
|
+
|
|
361
|
+
const instruction = instructionEntries.length === 1 ? instructionEntries[0].value : null
|
|
362
|
+
const instructionLabel = isPlan ? '派遣说明' : '验收说明'
|
|
363
|
+
if (instructionEntries.length === 0) diagnostics.push(makeDiagnostic(document, null, instructionLabel, '', '卷首必须声明且只声明一次说明'))
|
|
364
|
+
if (instructionEntries.length > 1) diagnostics.push(makeDiagnostic(document, null, instructionLabel, instructionEntries.map((item) => item.value).join('、'), instructionLabel + '不得重复'))
|
|
365
|
+
|
|
366
|
+
return { document, isPlan, instruction, tasks, closure, checks, global, diagnostics }
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export { parseTaskDocument }
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
function makeDiagnostic(document, taskId, field, value, message, template, line) {
|
|
2
|
+
return {
|
|
3
|
+
document,
|
|
4
|
+
taskId,
|
|
5
|
+
field,
|
|
6
|
+
value: value === undefined || value === null ? '' : String(value),
|
|
7
|
+
message,
|
|
8
|
+
template: template || defaultTemplate(field, taskId),
|
|
9
|
+
line: Number.isInteger(line) ? line : null
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function defaultTemplate(field, taskId) {
|
|
14
|
+
const id = Number.isInteger(taskId) ? taskId : 1
|
|
15
|
+
if (field === '派遣说明') return '- 派遣说明:先读方案和验收文件;完成后冻结再验收'
|
|
16
|
+
if (field === '验收说明') return '- 验收说明:按检查ID分组复核,漏项不得通过'
|
|
17
|
+
if (field === '任务块' || field === '任务ID') return '[任务' + id + '] 标题 / - 主文件:plugins/example/file.js / - 执行组:G1 / - 依赖:无'
|
|
18
|
+
if (field === '检查块' || field === '检查ID') return '[检查1] 标题 / - 验收组:R1 / - 取证方式:只读复核 / - 工作目录:.'
|
|
19
|
+
if (field === '收尾块') return '[收尾] / - 收尾依赖:[任务1] / - 收尾动作:完成收尾 / - 自检:已核对'
|
|
20
|
+
if (field === '全局验收块') return '[全局验收] / - 期望:全部覆盖 / - 禁止:漏项 / - 正式验收:[检查1]'
|
|
21
|
+
if (field === '主文件' || field === '收尾文件') return '- ' + field + ':plugins/example/file.js'
|
|
22
|
+
if (field === '关联文件') return '- 关联文件:plugins/example/helper.js'
|
|
23
|
+
if (field === '文件操作') return '- 文件操作:修改'
|
|
24
|
+
if (field === '修改点' || field === '对应修改点') return '- ' + field + ':M1'
|
|
25
|
+
if (field === '修改位置') return '- 修改位置:L12-34'
|
|
26
|
+
if (field === '定位锚点') return '- 定位锚点:function example()'
|
|
27
|
+
if (field === '生成来源') return '- 生成来源:plugins/example/source.js'
|
|
28
|
+
if (field === '生成命令') return '- 生成命令:node generator.mjs'
|
|
29
|
+
if (field === '生成产物') return '- 生成产物:说明目标文件结构'
|
|
30
|
+
if (field === '验收定位') return '- 验收定位:修改后的符号或文件存在条件'
|
|
31
|
+
if (field === '执行组') return '- 执行组:G1'
|
|
32
|
+
if (field === '依赖' || field === '收尾依赖') return '- ' + field + ':无'
|
|
33
|
+
if (field === '依赖理由') return '- 依赖理由:无'
|
|
34
|
+
if (field === '分组理由') return '- 分组理由:无'
|
|
35
|
+
if (field === '修改') return '- 修改:完成一个具体动作'
|
|
36
|
+
if (field === '自检') return '- 自检:运行有限纯函数检查'
|
|
37
|
+
if (field === '期望') return '- 期望:可验证的结果'
|
|
38
|
+
if (field === '禁止') return '- 禁止:明确的边界'
|
|
39
|
+
if (field === '正式验收') return '- 正式验收:[检查1]'
|
|
40
|
+
if (field === '验收组') return '- 验收组:R1'
|
|
41
|
+
if (field === '取证方式') return '- 取证方式:只读复核'
|
|
42
|
+
if (field === '工作目录') return '- 工作目录:.'
|
|
43
|
+
if (field === '检查') return '- 检查:明确的只读核对动作'
|
|
44
|
+
if (field === '验收证据') return '- 验收证据:命令退出0且原始结果可读'
|
|
45
|
+
if (field === '独占资源') return '- 独占资源:无'
|
|
46
|
+
return '- ' + field + ':填写非空内容'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { makeDiagnostic }
|