dsh-swarm-console 0.2.2 → 0.3.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/README.md +109 -109
- package/docs/DETAIL.md +79 -68
- package/index.js +388 -379
- package/lib/adapter.js +2 -1
- package/lib/core/checks.js +45 -20
- package/lib/core/contract.js +59 -10
- package/lib/core/evidence.js +2 -1
- package/lib/core/gate.js +81 -77
- package/lib/core/policy.js +17 -14
- package/lib/core/verify.js +261 -214
- package/package.json +47 -47
- package/policies/code-change.json +2 -14
package/lib/adapter.js
CHANGED
|
@@ -253,7 +253,8 @@ export function parseTodos(output) {
|
|
|
253
253
|
* @returns {string|undefined}
|
|
254
254
|
*/
|
|
255
255
|
export function parseResult(output) {
|
|
256
|
-
|
|
256
|
+
// P2-4:只取 RESULT: 之后的第一行,避免把后续说明整段吞进结果
|
|
257
|
+
const m = output.match(/RESULT:[ \t]*([^\n]*)/)
|
|
257
258
|
return m ? m[1].trim() : undefined
|
|
258
259
|
}
|
|
259
260
|
|
package/lib/core/checks.js
CHANGED
|
@@ -1,53 +1,78 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* P0-2 · 检查请求执行器(0.
|
|
2
|
+
* P0-2 · 检查请求执行器(0.3.0:契约结构破坏性变更)。
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* 变化:
|
|
5
|
+
* - 一切路径以 contract.workspaceRoot 为根;file.exists 只接受相对路径
|
|
6
|
+
* - exec 的 cwd 固定为 workspaceRoot(结论不由宿主 cwd 决定)
|
|
7
|
+
* - exec 命令必须与 contract.testCmd 完全相等(白名单),否则该检查判 RED
|
|
8
|
+
* - 非法请求不再 throw:返回 {ok:false, detail:'检查请求非法:…'},由 gate 判 RED
|
|
9
|
+
*
|
|
10
|
+
* 模型传入的 ok 一律忽略,并记一条 warning。
|
|
6
11
|
*/
|
|
7
12
|
import { existsSync as fsExistsSync } from 'node:fs'
|
|
8
13
|
import { spawnSync as cpSpawnSync } from 'node:child_process'
|
|
14
|
+
import { isAbsolute, resolve } from 'node:path'
|
|
9
15
|
|
|
10
16
|
/**
|
|
11
|
-
* @param {Array} requests
|
|
12
|
-
* @param {{existsSync?:Function, exec?:Function}} deps
|
|
17
|
+
* @param {Array} requests 检查请求
|
|
18
|
+
* @param {{workspaceRoot:string, testCmd?:string, existsSync?:Function, exec?:Function}} deps
|
|
13
19
|
* @returns {{checks:Array, warnings:string[]}}
|
|
14
20
|
*/
|
|
15
21
|
export function runChecks(requests, deps = {}) {
|
|
16
|
-
|
|
22
|
+
const workspaceRoot = deps.workspaceRoot
|
|
17
23
|
const existsSync = deps.existsSync ?? fsExistsSync
|
|
18
|
-
const exec = deps.exec ?? ((cmd) => cpSpawnSync(cmd, {
|
|
24
|
+
const exec = deps.exec ?? ((cmd, opts) => cpSpawnSync(cmd, {
|
|
19
25
|
shell: true,
|
|
26
|
+
cwd: opts?.cwd,
|
|
20
27
|
timeout: 120000,
|
|
21
28
|
encoding: 'utf8',
|
|
22
29
|
maxBuffer: 8 * 1024 * 1024,
|
|
23
30
|
}))
|
|
24
31
|
|
|
25
32
|
const warnings = []
|
|
33
|
+
// 顶层非法(不是数组)也不抛:整体记为一条失败检查,交 gate 判 RED
|
|
34
|
+
if (!Array.isArray(requests)) {
|
|
35
|
+
return {
|
|
36
|
+
checks: [{ id: '__checks__', type: 'invalid', ok: false, detail: '检查请求非法:checks 必须是数组' }],
|
|
37
|
+
warnings,
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
26
41
|
const checks = requests.map((req, i) => {
|
|
27
|
-
|
|
42
|
+
const fallbackId = req && typeof req === 'object' && typeof req.id === 'string' ? req.id : `checks[${i}]`
|
|
43
|
+
const invalid = (reason) => ({ id: fallbackId, type: String(req?.type ?? 'invalid'), ok: false, detail: `检查请求非法:${reason}` })
|
|
44
|
+
|
|
45
|
+
if (!req || typeof req !== 'object') return invalid('必须是对象')
|
|
28
46
|
const { id, type } = req
|
|
29
|
-
if (typeof id !== 'string' || id === '')
|
|
30
|
-
if (typeof type !== 'string' || type === '')
|
|
47
|
+
if (typeof id !== 'string' || id === '') return invalid('id 必填')
|
|
48
|
+
if (typeof type !== 'string' || type === '') return invalid('type 必填')
|
|
31
49
|
|
|
32
|
-
// 模型自报 ok 一律忽略,记 warning
|
|
50
|
+
// 模型自报 ok 一律忽略,记 warning
|
|
33
51
|
if (req.ok !== undefined) {
|
|
34
52
|
warnings.push(`check ${id} 自报 ok=${req.ok} 已忽略,改由插件实际执行判定`)
|
|
35
53
|
}
|
|
36
54
|
|
|
37
55
|
if (type === 'file.exists') {
|
|
38
|
-
if (typeof req.path !== 'string' || req.path === '')
|
|
39
|
-
|
|
56
|
+
if (typeof req.path !== 'string' || req.path === '') return invalid('file.exists 必带 path')
|
|
57
|
+
// P0-2:路径必须是相对 workspaceRoot 的相对路径,绝对路径直接判 RED
|
|
58
|
+
if (isAbsolute(req.path)) {
|
|
59
|
+
return { id, type, ok: false, path: req.path, detail: '检查路径必须是相对 workspaceRoot 的相对路径' }
|
|
60
|
+
}
|
|
61
|
+
if (typeof workspaceRoot !== 'string' || workspaceRoot === '') {
|
|
62
|
+
return invalid('缺少 workspaceRoot,无法解析相对路径')
|
|
40
63
|
}
|
|
41
|
-
const
|
|
42
|
-
|
|
64
|
+
const absPath = resolve(workspaceRoot, req.path)
|
|
65
|
+
const ok = existsSync(absPath) === true
|
|
66
|
+
return { id, type, ok, path: req.path, absPath, detail: ok ? `文件存在:${req.path}` : `文件不存在:${req.path}` }
|
|
43
67
|
}
|
|
44
68
|
|
|
45
69
|
if (type === 'exec') {
|
|
46
|
-
if (typeof req.cmd !== 'string' || req.cmd === '')
|
|
47
|
-
|
|
70
|
+
if (typeof req.cmd !== 'string' || req.cmd === '') return invalid('exec 必带 cmd')
|
|
71
|
+
// P0-4:命令白名单——必须与契约 testCmd 完全相等
|
|
72
|
+
if (typeof deps.testCmd !== 'string' || deps.testCmd === '' || req.cmd !== deps.testCmd) {
|
|
73
|
+
return { id, type, ok: false, cmd: req.cmd, detail: 'exec 命令未在契约中声明为 testCmd,已拒绝' }
|
|
48
74
|
}
|
|
49
|
-
const r = exec(req.cmd)
|
|
50
|
-
// 关键:ok 只看真实退出码,不看模型自报,也不看 stdout 内容
|
|
75
|
+
const r = exec(req.cmd, { cwd: workspaceRoot })
|
|
51
76
|
const ok = r.error === undefined && r.status === 0
|
|
52
77
|
const out = { id, type, ok, cmd: req.cmd, exitCode: r.status }
|
|
53
78
|
if (!ok) {
|
|
@@ -57,7 +82,7 @@ export function runChecks(requests, deps = {}) {
|
|
|
57
82
|
return out
|
|
58
83
|
}
|
|
59
84
|
|
|
60
|
-
|
|
85
|
+
return invalid(`未知检查类型 ${type}`)
|
|
61
86
|
})
|
|
62
87
|
|
|
63
88
|
return { checks, warnings }
|
package/lib/core/contract.js
CHANGED
|
@@ -1,28 +1,71 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* D1 ·
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* D1 · 冻结任务契约(0.3.0 起破坏性变更)。
|
|
3
|
+
*
|
|
4
|
+
* 新增 workspaceRoot(必填):检查以它为根执行,避免「在哪个 cwd 跑」决定结论。
|
|
5
|
+
* 新增 testCmd(可选):exec 检查唯一允许的命令(白名单);未声明则拒绝一切 exec。
|
|
6
|
+
* policyId 不再默认填充:只有显式声明才可能启用策略(P0-1 杜绝「命名即自证」)。
|
|
7
|
+
*
|
|
8
|
+
* id = 规范化内容哈希前 12 位;改 workspaceRoot / testCmd 同样换 id。
|
|
6
9
|
*/
|
|
7
10
|
import { createHash } from 'node:crypto'
|
|
11
|
+
import { existsSync, statSync } from 'node:fs'
|
|
12
|
+
import { isAbsolute, resolve } from 'node:path'
|
|
8
13
|
|
|
9
14
|
/**
|
|
10
|
-
* @param {{objective:string, constraints?:string[], criteria?:Array
|
|
11
|
-
* @returns {{id:string, objective:string, constraints:string[], criteria:Array<{id:string,text:string,checkIds:string[]}>, policyId:string, frozenAt:string}}
|
|
15
|
+
* @param {{objective:string, constraints?:string[], criteria?:Array, workspaceRoot:string, testCmd?:string, policyId?:string, frozenAt?:string}} input
|
|
12
16
|
*/
|
|
13
17
|
export function freezeContract(input) {
|
|
14
18
|
if (!input || typeof input.objective !== 'string' || input.objective.trim() === '') {
|
|
15
19
|
throw new Error('objective 必须是非空字符串')
|
|
16
20
|
}
|
|
21
|
+
|
|
22
|
+
// P0-2/P0-5:workspaceRoot 必填,且必须是存在目录的绝对路径
|
|
23
|
+
if (typeof input.workspaceRoot !== 'string' || input.workspaceRoot.trim() === '') {
|
|
24
|
+
throw new Error('workspaceRoot 必填(绝对路径):检查以它为根执行')
|
|
25
|
+
}
|
|
26
|
+
if (!isAbsolute(input.workspaceRoot)) {
|
|
27
|
+
throw new Error(`workspaceRoot 必须是绝对路径,收到:${input.workspaceRoot}`)
|
|
28
|
+
}
|
|
29
|
+
const workspaceRoot = resolve(input.workspaceRoot)
|
|
30
|
+
if (!existsSync(workspaceRoot)) {
|
|
31
|
+
throw new Error(`workspaceRoot 不存在:${workspaceRoot}`)
|
|
32
|
+
}
|
|
33
|
+
let st
|
|
34
|
+
try { st = statSync(workspaceRoot) } catch { st = undefined }
|
|
35
|
+
if (!st || !st.isDirectory()) {
|
|
36
|
+
throw new Error(`workspaceRoot 不是目录:${workspaceRoot}`)
|
|
37
|
+
}
|
|
38
|
+
|
|
17
39
|
const constraints = normalizeStringArray(input.constraints, 'constraints')
|
|
18
40
|
const criteria = normalizeCriteria(input.criteria)
|
|
19
|
-
|
|
41
|
+
|
|
42
|
+
// P0-4:testCmd 可选;给了就必须是非空字符串
|
|
43
|
+
let testCmd
|
|
44
|
+
if (input.testCmd !== undefined && input.testCmd !== null) {
|
|
45
|
+
if (typeof input.testCmd !== 'string' || input.testCmd.trim() === '') {
|
|
46
|
+
throw new Error('testCmd 必须是非空字符串(exec 检查的唯一允许命令)')
|
|
47
|
+
}
|
|
48
|
+
testCmd = input.testCmd.trim()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// P0-1:policyId 仅显式声明才存在,不设默认值
|
|
52
|
+
let policyId
|
|
53
|
+
if (input.policyId !== undefined && input.policyId !== null) {
|
|
54
|
+
if (typeof input.policyId !== 'string' || input.policyId.trim() === '') {
|
|
55
|
+
throw new Error('policyId 必须是非空字符串')
|
|
56
|
+
}
|
|
57
|
+
policyId = input.policyId.trim()
|
|
58
|
+
}
|
|
59
|
+
|
|
20
60
|
const frozenAt = typeof input.frozenAt === 'string' && input.frozenAt !== '' ? input.frozenAt : new Date().toISOString()
|
|
21
61
|
|
|
22
|
-
|
|
23
|
-
const canonical = JSON.stringify({ objective: input.objective, constraints, criteria, policyId })
|
|
62
|
+
const canonical = JSON.stringify({ objective: input.objective, constraints, criteria, workspaceRoot, testCmd, policyId })
|
|
24
63
|
const id = createHash('sha256').update(canonical).digest('hex').slice(0, 12)
|
|
25
|
-
|
|
64
|
+
|
|
65
|
+
const contract = { id, objective: input.objective, constraints, criteria, workspaceRoot, frozenAt }
|
|
66
|
+
if (testCmd !== undefined) contract.testCmd = testCmd
|
|
67
|
+
if (policyId !== undefined) contract.policyId = policyId
|
|
68
|
+
return contract
|
|
26
69
|
}
|
|
27
70
|
|
|
28
71
|
/** 校验已冻结契约的形状(供 gate / verify_run 入口复用)。 */
|
|
@@ -32,6 +75,12 @@ export function validateContract(contract) {
|
|
|
32
75
|
if (typeof contract.id !== 'string' || !/^[0-9a-f]{12}$/.test(contract.id)) errs.push('id 必须是 12 位小写十六进制')
|
|
33
76
|
if (typeof contract.objective !== 'string' || contract.objective.trim() === '') errs.push('objective 必填')
|
|
34
77
|
if (!Array.isArray(contract.constraints)) errs.push('constraints 必须是数组')
|
|
78
|
+
if (typeof contract.workspaceRoot !== 'string' || !isAbsolute(contract.workspaceRoot)) {
|
|
79
|
+
errs.push('workspaceRoot 必须是绝对路径')
|
|
80
|
+
}
|
|
81
|
+
if (contract.testCmd !== undefined && (typeof contract.testCmd !== 'string' || contract.testCmd.trim() === '')) {
|
|
82
|
+
errs.push('testCmd 必须是非空字符串')
|
|
83
|
+
}
|
|
35
84
|
if (!Array.isArray(contract.criteria)) errs.push('criteria 必须是数组')
|
|
36
85
|
else for (const [i, c] of contract.criteria.entries()) {
|
|
37
86
|
if (!c || typeof c.id !== 'string' || c.id === '') errs.push(`criteria[${i}].id 必填`)
|
package/lib/core/evidence.js
CHANGED
|
@@ -92,7 +92,8 @@ function normalizeChecks(value) {
|
|
|
92
92
|
}
|
|
93
93
|
const out = { id: c.id, type: c.type, ok: c.ok }
|
|
94
94
|
// path 必须保留:file.exists 失败时 gate 要用它生成 `test -f <path>` 复现命令
|
|
95
|
-
|
|
95
|
+
// absPath 必须保留:file.exists 的复现命令要用绝对路径,否则换个 cwd 就指向别的文件
|
|
96
|
+
for (const k of ['detail', 'exitCode', 'cmd', 'path', 'absPath']) if (c[k] !== undefined) out[k] = c[k]
|
|
96
97
|
return out
|
|
97
98
|
})
|
|
98
99
|
}
|
package/lib/core/gate.js
CHANGED
|
@@ -1,77 +1,81 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* D3 · 三态门禁纯函数。
|
|
3
|
-
* decide(contract, evidence) → { decision, reasons, missing, reproCmd? }
|
|
4
|
-
*
|
|
5
|
-
* R1 任一 criterion 的 checkIds 为空,或没有对应 evidence.checks → 最高 YELLOW
|
|
6
|
-
* R2 任一 check.ok === false → RED,reasons 必须包含 reproCmd(可复制执行)
|
|
7
|
-
* R3 全部 criteria 有证据且通过 → GREEN
|
|
8
|
-
*
|
|
9
|
-
* 优先级:RED > YELLOW > GREEN。纯函数,零副作用、零依赖。
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @param {object} contract TaskContract
|
|
14
|
-
* @param {object} evidence Evidence
|
|
15
|
-
*/
|
|
16
|
-
export function decide(contract, evidence) {
|
|
17
|
-
const reasons = []
|
|
18
|
-
const missing = []
|
|
19
|
-
|
|
20
|
-
if (!contract || !Array.isArray(contract.criteria) || contract.criteria.length === 0) {
|
|
21
|
-
return { decision: 'RED', reasons: ['契约没有任何验收条件,不可判通过'], missing: [] }
|
|
22
|
-
}
|
|
23
|
-
const checks = Array.isArray(evidence?.checks) ? evidence.checks : []
|
|
24
|
-
const byId = new Map(checks.map((c) => [c.id, c]))
|
|
25
|
-
|
|
26
|
-
// R2 先行:任一 check 失败即 RED,并给出最小复现命令
|
|
27
|
-
const failed = checks.filter((c) => c.ok === false)
|
|
28
|
-
if (failed.length > 0) {
|
|
29
|
-
const reproCmd = failed.map((c) => reproFor(c)).filter(Boolean).join(' && ')
|
|
30
|
-
for (const c of failed) {
|
|
31
|
-
reasons.push(`check "${c.id}" 失败:${c.detail || c.type}${c.cmd ? `(${c.cmd})` : ''}`)
|
|
32
|
-
}
|
|
33
|
-
if (!reproCmd) reasons.push('未提供可复现命令(reproCmd 为空)')
|
|
34
|
-
return { decision: 'RED', reasons, missing, reproCmd: reproCmd || undefined }
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// R1:逐条 criterion 查证据覆盖
|
|
38
|
-
for (const criterion of contract.criteria) {
|
|
39
|
-
const ids = Array.isArray(criterion.checkIds) ? criterion.checkIds : []
|
|
40
|
-
if (ids.length === 0) {
|
|
41
|
-
missing.push(`${criterion.id}: 未绑定 checkIds`)
|
|
42
|
-
continue
|
|
43
|
-
}
|
|
44
|
-
const hit = ids.filter((id) => byId.has(id))
|
|
45
|
-
if (hit.length === 0) {
|
|
46
|
-
missing.push(`${criterion.id}: 没有对应 evidence.checks(需要 ${ids.join(', ')})`)
|
|
47
|
-
} else if (hit.length < ids.length) {
|
|
48
|
-
const absent = ids.filter((id) => !byId.has(id))
|
|
49
|
-
missing.push(`${criterion.id}: 缺少检查 ${absent.join(', ')}`)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (missing.length > 0) {
|
|
54
|
-
reasons.push(`${missing.length} 条验收条件缺少证据,不能判为通过`)
|
|
55
|
-
return { decision: 'YELLOW', reasons, missing }
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// R3:全部覆盖且全通过
|
|
59
|
-
reasons.push(`全部 ${contract.criteria.length} 条验收条件均有通过证据`)
|
|
60
|
-
return { decision: 'GREEN', reasons, missing }
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** 从失败 check 构造可复制执行的最小复现命令(按检查类型给对应命令)。 */
|
|
64
|
-
function reproFor(check) {
|
|
65
|
-
if (typeof check.cmd === 'string' && check.cmd.trim() !== '') return check.cmd
|
|
66
|
-
// file.exists
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
1
|
+
/**
|
|
2
|
+
* D3 · 三态门禁纯函数。
|
|
3
|
+
* decide(contract, evidence) → { decision, reasons, missing, reproCmd? }
|
|
4
|
+
*
|
|
5
|
+
* R1 任一 criterion 的 checkIds 为空,或没有对应 evidence.checks → 最高 YELLOW
|
|
6
|
+
* R2 任一 check.ok === false → RED,reasons 必须包含 reproCmd(可复制执行)
|
|
7
|
+
* R3 全部 criteria 有证据且通过 → GREEN
|
|
8
|
+
*
|
|
9
|
+
* 优先级:RED > YELLOW > GREEN。纯函数,零副作用、零依赖。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {object} contract TaskContract
|
|
14
|
+
* @param {object} evidence Evidence
|
|
15
|
+
*/
|
|
16
|
+
export function decide(contract, evidence) {
|
|
17
|
+
const reasons = []
|
|
18
|
+
const missing = []
|
|
19
|
+
|
|
20
|
+
if (!contract || !Array.isArray(contract.criteria) || contract.criteria.length === 0) {
|
|
21
|
+
return { decision: 'RED', reasons: ['契约没有任何验收条件,不可判通过'], missing: [] }
|
|
22
|
+
}
|
|
23
|
+
const checks = Array.isArray(evidence?.checks) ? evidence.checks : []
|
|
24
|
+
const byId = new Map(checks.map((c) => [c.id, c]))
|
|
25
|
+
|
|
26
|
+
// R2 先行:任一 check 失败即 RED,并给出最小复现命令
|
|
27
|
+
const failed = checks.filter((c) => c.ok === false)
|
|
28
|
+
if (failed.length > 0) {
|
|
29
|
+
const reproCmd = failed.map((c) => reproFor(c)).filter(Boolean).join(' && ')
|
|
30
|
+
for (const c of failed) {
|
|
31
|
+
reasons.push(`check "${c.id}" 失败:${c.detail || c.type}${c.cmd ? `(${c.cmd})` : ''}`)
|
|
32
|
+
}
|
|
33
|
+
if (!reproCmd) reasons.push('未提供可复现命令(reproCmd 为空)')
|
|
34
|
+
return { decision: 'RED', reasons, missing, reproCmd: reproCmd || undefined }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// R1:逐条 criterion 查证据覆盖
|
|
38
|
+
for (const criterion of contract.criteria) {
|
|
39
|
+
const ids = Array.isArray(criterion.checkIds) ? criterion.checkIds : []
|
|
40
|
+
if (ids.length === 0) {
|
|
41
|
+
missing.push(`${criterion.id}: 未绑定 checkIds`)
|
|
42
|
+
continue
|
|
43
|
+
}
|
|
44
|
+
const hit = ids.filter((id) => byId.has(id))
|
|
45
|
+
if (hit.length === 0) {
|
|
46
|
+
missing.push(`${criterion.id}: 没有对应 evidence.checks(需要 ${ids.join(', ')})`)
|
|
47
|
+
} else if (hit.length < ids.length) {
|
|
48
|
+
const absent = ids.filter((id) => !byId.has(id))
|
|
49
|
+
missing.push(`${criterion.id}: 缺少检查 ${absent.join(', ')}`)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (missing.length > 0) {
|
|
54
|
+
reasons.push(`${missing.length} 条验收条件缺少证据,不能判为通过`)
|
|
55
|
+
return { decision: 'YELLOW', reasons, missing }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// R3:全部覆盖且全通过
|
|
59
|
+
reasons.push(`全部 ${contract.criteria.length} 条验收条件均有通过证据`)
|
|
60
|
+
return { decision: 'GREEN', reasons, missing }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 从失败 check 构造可复制执行的最小复现命令(按检查类型给对应命令)。 */
|
|
64
|
+
function reproFor(check) {
|
|
65
|
+
if (typeof check.cmd === 'string' && check.cmd.trim() !== '') return check.cmd
|
|
66
|
+
// P1-3 · file.exists:用 node -e 判断,跨平台(不依赖 sh / test -f);
|
|
67
|
+
// 有 absPath 时写绝对路径,保证命令在任何 cwd 下都指向同一文件。
|
|
68
|
+
if (check.type === 'file.exists') {
|
|
69
|
+
const target = typeof check.absPath === 'string' && check.absPath !== '' ? check.absPath : check.path
|
|
70
|
+
if (typeof target === 'string' && target !== '') {
|
|
71
|
+
return `node -e "process.exit(require('node:fs').existsSync(process.argv[1])?0:1)" ${dq(target)}`
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// 其余情况兜底:给一条可直接执行且退出非 0 的真命令,不是装饰性注释
|
|
75
|
+
return `echo "check ${check.id} 无可执行复现命令(该检查类型未提供路径/命令)" && exit 1`
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 双引号包裹(cmd 与 sh 都可解析),内部双引号转义 */
|
|
79
|
+
function dq(value) {
|
|
80
|
+
return '"' + String(value).replace(/"/g, '\\"') + '"'
|
|
81
|
+
}
|
package/lib/core/policy.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* P0-3 · 策略加载与转换(0.
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
2
|
+
* P0-3 · 策略加载与转换(0.3.0)。
|
|
3
|
+
*
|
|
4
|
+
* P0-1:策略层不再猜命令——删掉 'npm test' 兜底。
|
|
5
|
+
* P0-4:exec 只能来自 contract.testCmd;契约没声明就生成不出任何检查。
|
|
6
|
+
* P2-3:策略 JSON 只声明实际能生成的类型(exec)。
|
|
7
7
|
*/
|
|
8
8
|
import { readFileSync } from 'node:fs'
|
|
9
9
|
import { fileURLToPath } from 'node:url'
|
|
@@ -19,18 +19,21 @@ export function loadPolicy(id = 'code-change', baseDir = POLICIES_DIR) {
|
|
|
19
19
|
return data
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* 把策略声明转成可执行的检查请求。
|
|
24
|
+
* 只有契约显式给了 testCmd 才生成 exec;绝不使用默认命令。
|
|
25
|
+
*/
|
|
23
26
|
export function policyToChecks(policy, contract = {}) {
|
|
24
27
|
const out = []
|
|
28
|
+
const testCmd = typeof contract.testCmd === 'string' ? contract.testCmd.trim() : ''
|
|
29
|
+
if (testCmd === '') return out
|
|
25
30
|
for (const c of policy.checks ?? []) {
|
|
26
|
-
if (c.type === 'exec') {
|
|
27
|
-
const testCmd = (Array.isArray(contract.constraints) ? contract.constraints : [])
|
|
28
|
-
.map((s) => String(s))
|
|
29
|
-
.find((s) => s.startsWith('testCmd:'))
|
|
30
|
-
const cmd = testCmd ? testCmd.slice('testCmd:'.length).trim() : 'npm test'
|
|
31
|
-
out.push({ id: c.id, type: 'exec', cmd })
|
|
32
|
-
}
|
|
33
|
-
// file.exists / workspace.diff 需要路径或宿主 workspaceChanges,不在此伪造
|
|
31
|
+
if (c.type === 'exec') out.push({ id: c.id, type: 'exec', cmd: testCmd })
|
|
34
32
|
}
|
|
35
33
|
return out
|
|
36
34
|
}
|
|
35
|
+
|
|
36
|
+
/** 策略声明的检查 id 列表(供 P0-1 判断「策略是否被契约覆盖」)。 */
|
|
37
|
+
export function policyCheckIds(policy) {
|
|
38
|
+
return (policy.checks ?? []).map((c) => c.id).filter((id) => typeof id === 'string')
|
|
39
|
+
}
|