@akjamie/agentic-sdlc 1.1.0 → 1.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/LICENSE +201 -21
- package/README.md +264 -154
- package/assets/schema/risk-assessment.schema.json +225 -0
- package/assets/schema/templates/design.md +94 -31
- package/assets/skills/api-design/SKILL.md +204 -0
- package/assets/skills/asdlc-apply/SKILL.md +43 -18
- package/assets/skills/asdlc-impl-review/SKILL.md +153 -47
- package/assets/skills/asdlc-new/SKILL.md +42 -24
- package/assets/skills/asdlc-verify/SKILL.md +16 -6
- package/assets/skills/batch-grill-me/SKILL.md +11 -7
- package/assets/skills/frontend-design/SKILL.md +84 -0
- package/assets/skills/grilling/SKILL.md +43 -17
- package/package.json +2 -2
- package/src/assess/ai_judge.js +163 -0
- package/src/assess/decide.js +164 -0
- package/src/assess/governance.js +53 -0
- package/src/assess/index.js +183 -0
- package/src/assess/render.js +71 -0
- package/src/assess/rules/builtin.js +195 -0
- package/src/assess/schema.js +148 -0
- package/src/assess/store.js +138 -0
- package/src/assess/vectors/v1_intent.js +80 -0
- package/src/assess/vectors/v2_security.js +131 -0
- package/src/assess/vectors/v3_topology.js +107 -0
- package/src/cli.js +91 -89
- package/src/commands/apply.js +103 -36
- package/src/commands/assess.js +57 -0
- package/src/commands/doctor.js +1 -14
- package/src/commands/init.js +0 -9
- package/src/commands/new.js +97 -19
- package/src/commands/pr.js +27 -3
- package/src/commands/upgrade.js +1 -1
- package/src/commands/verify.js +109 -15
- package/src/lib/agents.js +5 -4
- package/src/lib/fs.js +26 -3
- package/src/lib/level.js +118 -0
- package/src/lib/paths.js +2 -1
- package/src/lib/prtype.js +17 -4
- package/src/lib/risk.js +50 -0
- package/src/lib/schema.js +4 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { changeDir } from '../lib/paths.js';
|
|
4
|
+
import { HARNESS_RULES_VERSION, ENGINE_VERSION } from './rules/builtin.js';
|
|
5
|
+
import { evaluateV1Intent } from './vectors/v1_intent.js';
|
|
6
|
+
import { evaluateV2Security } from './vectors/v2_security.js';
|
|
7
|
+
import { evaluateV3Topology } from './vectors/v3_topology.js';
|
|
8
|
+
import { decideAssessment } from './decide.js';
|
|
9
|
+
import { deriveGovernance } from './governance.js';
|
|
10
|
+
import { appendAssessmentRecord, loadLatestAssessment, loadInitialAssessment } from './store.js';
|
|
11
|
+
import { renderCheckSummary, renderPRComment, renderLabels } from './render.js';
|
|
12
|
+
|
|
13
|
+
export { loadRiskHistory, loadLatestAssessment, loadInitialAssessment, loadMaxHistoryLevel } from './store.js';
|
|
14
|
+
export { renderCheckSummary, renderPRComment, renderLabels } from './render.js';
|
|
15
|
+
export { HARNESS_RULES_VERSION, ENGINE_VERSION, DEFAULT_GOVERNANCE } from './rules/builtin.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Unified Risk Assessment Engine (Specification §7.2)
|
|
19
|
+
* The single implementation point for all stages: new, spec, impl.
|
|
20
|
+
*/
|
|
21
|
+
export function assess({
|
|
22
|
+
stage = 'new',
|
|
23
|
+
changeId,
|
|
24
|
+
baseSha = 'HEAD',
|
|
25
|
+
headSha = null,
|
|
26
|
+
requirement = '',
|
|
27
|
+
changedFiles = [],
|
|
28
|
+
changeType = null,
|
|
29
|
+
previous = null,
|
|
30
|
+
baseline = null,
|
|
31
|
+
actor = { type: 'system', id: 'asdlc' },
|
|
32
|
+
repoRoot = null,
|
|
33
|
+
persist = true
|
|
34
|
+
} = {}) {
|
|
35
|
+
try {
|
|
36
|
+
const prev = previous || (repoRoot && changeId ? loadLatestAssessment(repoRoot, changeId) : null);
|
|
37
|
+
const baseRec = baseline || (repoRoot && changeId ? loadInitialAssessment(repoRoot, changeId) : null);
|
|
38
|
+
|
|
39
|
+
// If requirement is empty or for spec stage, load proposal.md and design.md from change dir
|
|
40
|
+
let effectiveReq = requirement || '';
|
|
41
|
+
if (repoRoot && changeId) {
|
|
42
|
+
try {
|
|
43
|
+
const cdir = changeDir(repoRoot, changeId);
|
|
44
|
+
const propPath = path.join(cdir, 'proposal.md');
|
|
45
|
+
if (fs.existsSync(propPath)) {
|
|
46
|
+
const propText = fs.readFileSync(propPath, 'utf8');
|
|
47
|
+
effectiveReq = `${effectiveReq}\n${propText}`.trim();
|
|
48
|
+
}
|
|
49
|
+
} catch { /* ignore read failure */ }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 1. Evaluate Three Risk Vectors
|
|
53
|
+
const v1 = evaluateV1Intent({ requirement: effectiveReq, changedFiles, changeType });
|
|
54
|
+
const v2 = evaluateV2Security({ requirement: effectiveReq, changedFiles, repoRoot });
|
|
55
|
+
const v3 = evaluateV3Topology({ requirement: effectiveReq, changedFiles, repoRoot });
|
|
56
|
+
|
|
57
|
+
// Inferred or provided Change Type
|
|
58
|
+
const effectiveType = changeType || v1.inferredType || 'change';
|
|
59
|
+
|
|
60
|
+
// 2. Decide Level, Verdict, Reason Codes
|
|
61
|
+
const decision = decideAssessment({
|
|
62
|
+
stage,
|
|
63
|
+
v1,
|
|
64
|
+
v2,
|
|
65
|
+
v3,
|
|
66
|
+
changedFiles,
|
|
67
|
+
previous: prev,
|
|
68
|
+
baseline: baseRec
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// 3. Derive Governance Output Matrix
|
|
72
|
+
const governance = decision.governance || deriveGovernance({
|
|
73
|
+
level: decision.level,
|
|
74
|
+
confidence: decision.confidence,
|
|
75
|
+
mandatory_human_review: decision.mandatory_human_review
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const recordData = {
|
|
79
|
+
timestamp: new Date().toISOString(),
|
|
80
|
+
actor,
|
|
81
|
+
stage,
|
|
82
|
+
role: 'primary',
|
|
83
|
+
compared_to: null,
|
|
84
|
+
status: decision.status,
|
|
85
|
+
verdict: decision.verdict,
|
|
86
|
+
defaulted_cause: null,
|
|
87
|
+
change_type: effectiveType,
|
|
88
|
+
source: {
|
|
89
|
+
base_sha: String(baseSha || 'HEAD'),
|
|
90
|
+
head_sha: headSha ? String(headSha) : null,
|
|
91
|
+
harness_rules_version: HARNESS_RULES_VERSION,
|
|
92
|
+
engine_version: ENGINE_VERSION,
|
|
93
|
+
task_ref: null,
|
|
94
|
+
models: []
|
|
95
|
+
},
|
|
96
|
+
risk: {
|
|
97
|
+
level: decision.level,
|
|
98
|
+
confidence: decision.confidence
|
|
99
|
+
},
|
|
100
|
+
vectors: {
|
|
101
|
+
v1_intent: {
|
|
102
|
+
level: v1.level,
|
|
103
|
+
confidence: v1.confidence,
|
|
104
|
+
evidence: v1.evidence,
|
|
105
|
+
unresolved: v1.unresolved,
|
|
106
|
+
mandatory_human_review: v1.mandatory_human_review,
|
|
107
|
+
no_signal: v1.no_signal
|
|
108
|
+
},
|
|
109
|
+
v2_security: {
|
|
110
|
+
level: v2.level,
|
|
111
|
+
confidence: v2.confidence,
|
|
112
|
+
evidence: v2.evidence,
|
|
113
|
+
unresolved: v2.unresolved,
|
|
114
|
+
mandatory_human_review: v2.mandatory_human_review,
|
|
115
|
+
no_signal: v2.no_signal
|
|
116
|
+
},
|
|
117
|
+
v3_topology: {
|
|
118
|
+
level: v3.level,
|
|
119
|
+
confidence: v3.confidence,
|
|
120
|
+
evidence: v3.evidence,
|
|
121
|
+
unresolved: v3.unresolved,
|
|
122
|
+
mandatory_human_review: v3.mandatory_human_review,
|
|
123
|
+
no_signal: v3.no_signal
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
governance,
|
|
127
|
+
reason_codes: decision.reason_codes,
|
|
128
|
+
observations: [],
|
|
129
|
+
transition: decision.transition
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// 4. Persist if repoRoot and changeId provided
|
|
133
|
+
if (persist && repoRoot && changeId) {
|
|
134
|
+
return appendAssessmentRecord(repoRoot, changeId, recordData);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return recordData;
|
|
138
|
+
} catch (err) {
|
|
139
|
+
// Fallback to Global Default on unexpected exception (§5.5)
|
|
140
|
+
const fallbackDecision = decideAssessment({ stage, error: err, previous });
|
|
141
|
+
const fallbackRecord = {
|
|
142
|
+
timestamp: new Date().toISOString(),
|
|
143
|
+
actor,
|
|
144
|
+
stage,
|
|
145
|
+
role: 'primary',
|
|
146
|
+
compared_to: null,
|
|
147
|
+
status: 'defaulted',
|
|
148
|
+
verdict: 'defaulted',
|
|
149
|
+
defaulted_cause: err.message,
|
|
150
|
+
change_type: changeType || 'change',
|
|
151
|
+
source: {
|
|
152
|
+
base_sha: String(baseSha || 'HEAD'),
|
|
153
|
+
head_sha: headSha ? String(headSha) : null,
|
|
154
|
+
harness_rules_version: HARNESS_RULES_VERSION,
|
|
155
|
+
engine_version: ENGINE_VERSION,
|
|
156
|
+
task_ref: null,
|
|
157
|
+
models: []
|
|
158
|
+
},
|
|
159
|
+
risk: {
|
|
160
|
+
level: fallbackDecision.level,
|
|
161
|
+
confidence: 'low'
|
|
162
|
+
},
|
|
163
|
+
vectors: {
|
|
164
|
+
v1_intent: { level: 'L2', confidence: 'low', evidence: [], unresolved: [err.message], mandatory_human_review: true, no_signal: false },
|
|
165
|
+
v2_security: { level: 'L2', confidence: 'low', evidence: [], unresolved: [err.message], mandatory_human_review: true, no_signal: false },
|
|
166
|
+
v3_topology: { level: 'L2', confidence: 'low', evidence: [], unresolved: [err.message], mandatory_human_review: true, no_signal: false }
|
|
167
|
+
},
|
|
168
|
+
governance: fallbackDecision.governance,
|
|
169
|
+
reason_codes: ['DEFAULTED'],
|
|
170
|
+
observations: [],
|
|
171
|
+
transition: fallbackDecision.transition
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
if (persist && repoRoot && changeId) {
|
|
175
|
+
try {
|
|
176
|
+
return appendAssessmentRecord(repoRoot, changeId, fallbackRecord);
|
|
177
|
+
} catch {
|
|
178
|
+
// Return memory record if disk append fails
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return fallbackRecord;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats assessment records into PR status summaries, comments, and labels (Specification §9.7)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export function renderCheckSummary(record) {
|
|
6
|
+
const lvl = record.risk.level;
|
|
7
|
+
const human = record.governance.implementation_review === 'ai+human' ? 'human-review-required' : 'auto-merge-eligible';
|
|
8
|
+
const codes = (record.reason_codes || []).join(',') || 'NONE';
|
|
9
|
+
return `[${record.verdict.toUpperCase()}] Level: ${lvl} | Mode: ${record.governance.workflow_mode} | Review: ${human} | Codes: ${codes}`;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function renderPRComment(record) {
|
|
13
|
+
const isEscalated = record.transition.direction === 'up';
|
|
14
|
+
const lvl = record.risk.level;
|
|
15
|
+
const isHumanReview = record.governance.implementation_review === 'ai+human';
|
|
16
|
+
|
|
17
|
+
// Prominent Governance Level & Action Highlight Banner
|
|
18
|
+
let actionHighlight = '';
|
|
19
|
+
if (isEscalated) {
|
|
20
|
+
actionHighlight = `> ⚠️ **【治理动作:实施阶段升级 · 必须人工终审】**\n> 🎯 **最终治理等级**:\`${lvl}\`(由 \`${record.transition.from_level || 'L1'}\` 原地升级)\n> 🚨 **流转执行**:保持单 PR 流水线,**自动合并已关闭 (Auto-Merge DENIED)**,必须由人类 Lead 审核批准后方可合入。`;
|
|
21
|
+
} else if (isHumanReview) {
|
|
22
|
+
actionHighlight = `> 🛑 **【治理动作:高风险改动 · 必须人工终审】**\n> 🎯 **最终治理等级**:\`${lvl}\`\n> 🚨 **流转执行**:**自动合并已关闭 (Auto-Merge DENIED)**,必须由人类 Lead 审核批准后方可合入主干。`;
|
|
23
|
+
} else {
|
|
24
|
+
actionHighlight = `> ⚡ **【治理动作:低风险改动 · AI 自动合入 (Auto-Merge Eligible)】**\n> 🎯 **最终治理等级**:\`${lvl}\`\n> 🚀 **流转执行**:CI 门禁与自动化测试全绿后,**系统将自动 Approve 并执行合并**,无需人工审批阻断。`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const title = isEscalated
|
|
28
|
+
? `⚠️ 需要人工评审 · 风险等级 ${record.transition.from_level || 'L1'} → ${lvl}(升级)`
|
|
29
|
+
: `🛡️ ASdlc 治理评估报告 · 风险等级 ${lvl}`;
|
|
30
|
+
|
|
31
|
+
const reasonList = (record.reason_codes || []).map((c) => `- [${c}]`).join('\n') || '- 局部封闭改动,未触发硬规则';
|
|
32
|
+
|
|
33
|
+
const evidenceItems = [];
|
|
34
|
+
for (const vName of ['v1_intent', 'v2_security', 'v3_topology']) {
|
|
35
|
+
const v = record.vectors[vName];
|
|
36
|
+
if (v && v.evidence) {
|
|
37
|
+
for (const e of v.evidence) {
|
|
38
|
+
evidenceItems.push(`- [${e.signal.toUpperCase()}] ${e.summary}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const reviewQuestions = isHumanReview
|
|
44
|
+
? `**评审人请确认:**\n- 行为与代码变化是否与已批准意图一致\n- 确认未解项与潜在外部影响已被充分覆盖\n`
|
|
45
|
+
: `**自动化判定:**\n- 满足自动合并条件,无需额外人工 Lead 审批。\n`;
|
|
46
|
+
|
|
47
|
+
return `### ${title}
|
|
48
|
+
|
|
49
|
+
${actionHighlight}
|
|
50
|
+
|
|
51
|
+
| 最终治理等级 (Level) | 工作流模式 (Workflow) | 实施评审要求 (Review) | 记录指纹 (Fingerprint) |
|
|
52
|
+
| :---: | :---: | :---: | :---: |
|
|
53
|
+
| **\`${lvl}\`** | \`${record.governance.workflow_mode}\` | \`${record.governance.implementation_review}\` | \`${(record.hash || '').slice(0, 12) || 'unpersisted'}\` |
|
|
54
|
+
|
|
55
|
+
**📋 评估理由与依据:**
|
|
56
|
+
${evidenceItems.join('\n') || reasonList}
|
|
57
|
+
|
|
58
|
+
${reviewQuestions}
|
|
59
|
+
`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function renderLabels(record) {
|
|
63
|
+
const labels = [`asdlc:${record.risk.level}`];
|
|
64
|
+
if (record.governance.implementation_review === 'ai+human') {
|
|
65
|
+
labels.push('asdlc:human-review-required');
|
|
66
|
+
}
|
|
67
|
+
if (record.transition.direction === 'up') {
|
|
68
|
+
labels.push('asdlc:escalated');
|
|
69
|
+
}
|
|
70
|
+
return labels;
|
|
71
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
export const HARNESS_RULES_VERSION = '2026.10.0';
|
|
2
|
+
export const ENGINE_VERSION = '1.3.0';
|
|
3
|
+
|
|
4
|
+
export const RISK_LEVELS = {
|
|
5
|
+
L0: 'L0',
|
|
6
|
+
L1: 'L1',
|
|
7
|
+
L2: 'L2',
|
|
8
|
+
L3: 'L3',
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const LEVEL_RANK = {
|
|
12
|
+
L0: 0,
|
|
13
|
+
L1: 1,
|
|
14
|
+
L2: 2,
|
|
15
|
+
L3: 3,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Global default fallback governance (Specification §5.5)
|
|
20
|
+
* Applied when assessment crashes, times out, or baseline is corrupted.
|
|
21
|
+
* Fixed at L2 (not L3) to avoid total tool paralysis while closing all automation.
|
|
22
|
+
*/
|
|
23
|
+
export const DEFAULT_GOVERNANCE = {
|
|
24
|
+
level: 'L2',
|
|
25
|
+
workflow_mode: 'spec-first',
|
|
26
|
+
spec_approval: 'human',
|
|
27
|
+
implementation_review: 'ai+human',
|
|
28
|
+
auto_approval: 'disabled'
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const CANONICAL_DESTRUCTIVE_PATTERN = /\b(drop\s+(table|column|database|index|schema)|truncate\s+(table|database)?|purge\s+(data|table|database)|hard\s+delete|clean\s+database|destroy\s+database)\b/i;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Hard Signal Rules (§5.2):
|
|
35
|
+
* Deterministic path patterns, migration types, CI/CD, agent governance files.
|
|
36
|
+
* Can trigger single-veto up to L3.
|
|
37
|
+
*/
|
|
38
|
+
export const HARD_RULES = [
|
|
39
|
+
// 1. Critical Governance Plane (L3)
|
|
40
|
+
{
|
|
41
|
+
id: 'HARD_GOV_PLANE',
|
|
42
|
+
level: 'L3',
|
|
43
|
+
signal: 'hard',
|
|
44
|
+
mandatory_human_review: true,
|
|
45
|
+
patterns: [
|
|
46
|
+
/(^|\/)AGENTS\.md$/i,
|
|
47
|
+
/(^|\/)openspec\/config\.yaml$/i,
|
|
48
|
+
/(^|\/)(assets|\.agents)\/skills\/.*SKILL\.md$/i
|
|
49
|
+
],
|
|
50
|
+
summary: 'Modifies core Agent governance constitution (AGENTS.md, openspec/config.yaml, or agent skills)'
|
|
51
|
+
},
|
|
52
|
+
// 2. High-Risk Security, Credentials & Cryptography (L3)
|
|
53
|
+
{
|
|
54
|
+
id: 'HARD_SECURITY_CREDENTIALS',
|
|
55
|
+
level: 'L3',
|
|
56
|
+
signal: 'hard',
|
|
57
|
+
mandatory_human_review: true,
|
|
58
|
+
patterns: [
|
|
59
|
+
/(^|\/)(auth|security|session|mfa|kms)\//i,
|
|
60
|
+
/\.(pem|key|cert|p12|jks)$/i,
|
|
61
|
+
/(^|\/)secrets?\.[a-z0-9]+$/i
|
|
62
|
+
],
|
|
63
|
+
summary: 'Touches security, cryptographic keys, authentication, or secrets'
|
|
64
|
+
},
|
|
65
|
+
// 3. Financial, Billing & Ledgers (L3)
|
|
66
|
+
{
|
|
67
|
+
id: 'HARD_FINANCIAL_ASSETS',
|
|
68
|
+
level: 'L3',
|
|
69
|
+
signal: 'hard',
|
|
70
|
+
mandatory_human_review: true,
|
|
71
|
+
patterns: [
|
|
72
|
+
/(^|\/)(payment|billing|ledger|clearing|settlement|wallet)\//i
|
|
73
|
+
],
|
|
74
|
+
summary: 'Touches core financial accounting, settlement, or payment ledger paths'
|
|
75
|
+
},
|
|
76
|
+
// 4. Destructive Database Operations (L3)
|
|
77
|
+
{
|
|
78
|
+
id: 'HARD_DESTRUCTIVE_DDL',
|
|
79
|
+
level: 'L3',
|
|
80
|
+
signal: 'hard',
|
|
81
|
+
mandatory_human_review: true,
|
|
82
|
+
patterns: [
|
|
83
|
+
/(^|\/)(db\/)?migrations?\/.*(drop|truncate|purge|destructive)/i,
|
|
84
|
+
/(^|\/)db\/migration\/.*(drop|truncate|purge|destructive)/i
|
|
85
|
+
],
|
|
86
|
+
summary: 'Executes destructive, irreversible schema or data deletion operations'
|
|
87
|
+
},
|
|
88
|
+
// 5. Public API Contracts & Framework Handlers (L2 - Spec-First)
|
|
89
|
+
{
|
|
90
|
+
id: 'HARD_API_CONTRACT',
|
|
91
|
+
level: 'L2',
|
|
92
|
+
signal: 'hard',
|
|
93
|
+
mandatory_human_review: false,
|
|
94
|
+
patterns: [
|
|
95
|
+
// Schema & IDL definitions (excluding docs and tooling schemas)
|
|
96
|
+
/(^|\/)(protos?)\//i,
|
|
97
|
+
/(^|\/)(openapi|swagger)\.[a-z0-9]+$/i,
|
|
98
|
+
/\.(proto|graphql|gql|thrift|prisma)$/i,
|
|
99
|
+
// Spring Boot (Java / Kotlin)
|
|
100
|
+
/(^|\/).*(Controller|Resource|Endpoint|Api)\.(java|kt)$/i,
|
|
101
|
+
/(^|\/)(controllers?|endpoints?|resources?)\/.*\.(java|kt)$/i,
|
|
102
|
+
// FastAPI / Python web frameworks
|
|
103
|
+
/(^|\/)(routers?|endpoints?)\/.*\.py$/i,
|
|
104
|
+
/(^|\/).*(router|controller|endpoint).*\.py$/i,
|
|
105
|
+
/(^|\/)(app|api)\/(main|app)\.py$/i,
|
|
106
|
+
// Node.js / NestJS / Express / Next.js
|
|
107
|
+
/(^|\/).*\.controller\.(ts|js)$/i,
|
|
108
|
+
/(^|\/)(pages|app|server|src)\/api\/.*\.(ts|js|tsx|jsx)$/i,
|
|
109
|
+
/(^|\/)(routes?\/|.*routes?\.)(ts|js)$/i,
|
|
110
|
+
// Go web handlers (excluding utilities and non-API handlers)
|
|
111
|
+
/(^|\/)(handlers?|routers?|controllers?|api)\/(?!.*error_handler).*\.go$/i,
|
|
112
|
+
/^(?!.*[/\\](utils?|helpers?)[/\\]).*(?<!error)_(handler|router|controller)\.go$/i
|
|
113
|
+
],
|
|
114
|
+
summary: 'Modifies public API contracts, controllers, routers, or schemas (Spring Boot, FastAPI, NestJS, Go, Proto, OpenAPI)'
|
|
115
|
+
},
|
|
116
|
+
// 6. CI/CD Workflows & Git Hooks (L1 Tooling / Routine Dev)
|
|
117
|
+
{
|
|
118
|
+
id: 'HARD_CI_TOOLING',
|
|
119
|
+
level: 'L1',
|
|
120
|
+
signal: 'hard',
|
|
121
|
+
mandatory_human_review: false,
|
|
122
|
+
patterns: [
|
|
123
|
+
/^\.github\/workflows\//i,
|
|
124
|
+
/(^|\/)Jenkinsfile$/i,
|
|
125
|
+
/(^|\/)\.githooks\//i,
|
|
126
|
+
/(^|\/)\.husky\//i,
|
|
127
|
+
/(^|\/)\.gitlab-ci\.ya?ml$/i
|
|
128
|
+
],
|
|
129
|
+
summary: 'Modifies CI/CD pipeline workflows, Git hooks, or developer tooling'
|
|
130
|
+
},
|
|
131
|
+
// 7. Database Migrations & Data Models (L2)
|
|
132
|
+
{
|
|
133
|
+
id: 'HARD_DB_MIGRATION',
|
|
134
|
+
level: 'L2',
|
|
135
|
+
signal: 'hard',
|
|
136
|
+
mandatory_human_review: false,
|
|
137
|
+
patterns: [
|
|
138
|
+
/(^|\/)(db\/)?migrations?\//i,
|
|
139
|
+
/(^|\/)db\/(schema|models?)\//i,
|
|
140
|
+
/(^|\/)prisma\/schema\.prisma$/i
|
|
141
|
+
],
|
|
142
|
+
summary: 'Modifies database migrations, tables, or persistence schemas'
|
|
143
|
+
},
|
|
144
|
+
// 7. Supply Chain & Build Manifests (L2)
|
|
145
|
+
{
|
|
146
|
+
id: 'HARD_SUPPLY_CHAIN',
|
|
147
|
+
level: 'L2',
|
|
148
|
+
signal: 'hard',
|
|
149
|
+
mandatory_human_review: false,
|
|
150
|
+
patterns: [
|
|
151
|
+
/(^|\/)(package\.json|pnpm-lock\.yaml|package-lock\.json|yarn\.lock)$/i,
|
|
152
|
+
/(^|\/)(go\.mod|go\.sum|Cargo\.toml|Cargo\.lock|pom\.xml|build\.gradle)$/i,
|
|
153
|
+
/(^|\/)(Dockerfile.*|Containerfile.*|docker-compose.*)$/i,
|
|
154
|
+
/(^|\/)(helm|k8s|terraform)\//i
|
|
155
|
+
],
|
|
156
|
+
summary: 'Touches dependency manifests, lockfiles, containers, or infra code'
|
|
157
|
+
}
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Deterministic Proof of L0 Trivial Change (§2.2):
|
|
162
|
+
* All files must match documentation or non-executable patterns.
|
|
163
|
+
* Never touches code, CI, configs, or agent files.
|
|
164
|
+
*/
|
|
165
|
+
export const L0_DETERMINISTIC_PATTERNS = [
|
|
166
|
+
/^docs\//i,
|
|
167
|
+
/\.md$/i,
|
|
168
|
+
/\.txt$/i,
|
|
169
|
+
/^LICENSE$/i,
|
|
170
|
+
/^NOTICE$/i
|
|
171
|
+
];
|
|
172
|
+
|
|
173
|
+
export const TEST_AND_DOC_PATTERNS = [
|
|
174
|
+
/(^|\/)(tests?|__tests__|testing)\//i,
|
|
175
|
+
/\.(test|spec)\.[a-z0-9]+$/i,
|
|
176
|
+
/_test\.go$/i,
|
|
177
|
+
/test_[^/]+\.py$/i,
|
|
178
|
+
/[^/]+_test\.py$/i,
|
|
179
|
+
/(^|\/)docs?\//i,
|
|
180
|
+
/\.(md|txt)$/i,
|
|
181
|
+
/(^|\/)assets\/(?:images|media|icons|fonts)\//i
|
|
182
|
+
];
|
|
183
|
+
|
|
184
|
+
export function isTestOrDocFile(filePath = '') {
|
|
185
|
+
const norm = String(filePath).replace(/\\/g, '/');
|
|
186
|
+
if (
|
|
187
|
+
/(^|\/)AGENTS\.md$/i.test(norm) ||
|
|
188
|
+
/(^|\/)openspec\/config\.yaml$/i.test(norm) ||
|
|
189
|
+
/(^|\/)(assets|\.agents)\/skills\//i.test(norm)
|
|
190
|
+
) {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
return TEST_AND_DOC_PATTERNS.some((p) => p.test(norm));
|
|
194
|
+
}
|
|
195
|
+
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { exists, readText } from '../lib/fs.js';
|
|
5
|
+
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const __dirname = path.dirname(__filename);
|
|
8
|
+
const SCHEMA_PATH = path.resolve(__dirname, '../../assets/schema/risk-assessment.schema.json');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Canonicalize JSON according to RFC 8785 (JCS principles):
|
|
12
|
+
* Deterministic key sorting, compact representation, no extra whitespace.
|
|
13
|
+
*/
|
|
14
|
+
export function canonicalJson(obj) {
|
|
15
|
+
if (obj === null || typeof obj !== 'object') {
|
|
16
|
+
return JSON.stringify(obj);
|
|
17
|
+
}
|
|
18
|
+
if (Array.isArray(obj)) {
|
|
19
|
+
return '[' + obj.map(canonicalJson).join(',') + ']';
|
|
20
|
+
}
|
|
21
|
+
const keys = Object.keys(obj).sort();
|
|
22
|
+
const pairs = keys.map((k) => JSON.stringify(k) + ':' + canonicalJson(obj[k]));
|
|
23
|
+
return '{' + pairs.join(',') + '}';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Computes sha256 hash of a record excluding the 'hash' field itself.
|
|
28
|
+
*/
|
|
29
|
+
export function computeRecordHash(record) {
|
|
30
|
+
const clone = { ...record };
|
|
31
|
+
delete clone.hash;
|
|
32
|
+
const canonical = canonicalJson(clone);
|
|
33
|
+
return crypto.createHash('sha256').update(canonical, 'utf8').digest('hex');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Validates a single record against the JSON Schema principles.
|
|
38
|
+
* Returns { valid: boolean, errors: string[] }
|
|
39
|
+
*/
|
|
40
|
+
export function validateRecordSchema(record) {
|
|
41
|
+
const errors = [];
|
|
42
|
+
if (!record || typeof record !== 'object') {
|
|
43
|
+
return { valid: false, errors: ['Record must be an object'] };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const requiredFields = [
|
|
47
|
+
'schema_version', 'change_id', 'seq', 'prev_hash', 'hash',
|
|
48
|
+
'timestamp', 'actor', 'stage', 'role', 'status', 'verdict',
|
|
49
|
+
'change_type', 'source', 'risk', 'vectors', 'governance',
|
|
50
|
+
'reason_codes', 'transition'
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
for (const f of requiredFields) {
|
|
54
|
+
if (record[f] === undefined) {
|
|
55
|
+
errors.push(`Missing required field: ${f}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (record.schema_version !== 1) {
|
|
60
|
+
errors.push(`schema_version must be 1, got ${record.schema_version}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const validLevels = ['L0', 'L1', 'L2', 'L3'];
|
|
64
|
+
if (record.risk && !validLevels.includes(record.risk.level)) {
|
|
65
|
+
errors.push(`Invalid risk level: ${record.risk.level}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const validTypes = ['chore', 'fix', 'change', 'feature'];
|
|
69
|
+
if (!validTypes.includes(record.change_type)) {
|
|
70
|
+
errors.push(`Invalid change_type: ${record.change_type}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const validStages = ['new', 'spec', 'impl'];
|
|
74
|
+
if (!validStages.includes(record.stage)) {
|
|
75
|
+
errors.push(`Invalid stage: ${record.stage}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const validStatuses = ['provisional', 'needs_input', 'ready', 'defaulted'];
|
|
79
|
+
if (!validStatuses.includes(record.status)) {
|
|
80
|
+
errors.push(`Invalid status: ${record.status}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (record.status === 'defaulted' && !record.defaulted_cause) {
|
|
84
|
+
errors.push('status defaulted requires non-null defaulted_cause');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (record.hash && computeRecordHash(record) !== record.hash) {
|
|
88
|
+
errors.push(`Hash mismatch: computed ${computeRecordHash(record)} vs declared ${record.hash}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
valid: errors.length === 0,
|
|
93
|
+
errors
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Validates a sequence of historical records (Cross-Record Invariants §8.3):
|
|
99
|
+
* 1. seq is continuous starting from 1
|
|
100
|
+
* 2. prev_hash equals previous record's hash (first record is null)
|
|
101
|
+
* 3. Level only increases (monotonic) unless waiver or recovered_from_default
|
|
102
|
+
*/
|
|
103
|
+
export function validateChainInvariants(records = []) {
|
|
104
|
+
const errors = [];
|
|
105
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
106
|
+
return { valid: false, errors: ['History is empty'] };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const rank = { L0: 0, L1: 1, L2: 2, L3: 3 };
|
|
110
|
+
|
|
111
|
+
for (let i = 0; i < records.length; i++) {
|
|
112
|
+
const rec = records[i];
|
|
113
|
+
const expectedSeq = i + 1;
|
|
114
|
+
if (rec.seq !== expectedSeq) {
|
|
115
|
+
errors.push(`Record at index ${i} has seq ${rec.seq}, expected ${expectedSeq}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Cryptographic content hash integrity (P1-3)
|
|
119
|
+
const computed = computeRecordHash(rec);
|
|
120
|
+
if (rec.hash && computed !== rec.hash) {
|
|
121
|
+
errors.push(`Record ${rec.seq} hash mismatch: computed ${computed} vs declared ${rec.hash}`);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (i === 0) {
|
|
125
|
+
if (rec.prev_hash !== null) {
|
|
126
|
+
errors.push(`Initial record must have prev_hash: null, got ${rec.prev_hash}`);
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
const prev = records[i - 1];
|
|
130
|
+
if (rec.prev_hash !== prev.hash) {
|
|
131
|
+
errors.push(`Record ${rec.seq} prev_hash (${rec.prev_hash}) does not match previous hash (${prev.hash})`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const prevRank = rank[prev.risk.level] ?? 1;
|
|
135
|
+
const curRank = rank[rec.risk.level] ?? 1;
|
|
136
|
+
const allowedDemote = Boolean(rec.transition?.waiver || rec.transition?.recovered_from_default);
|
|
137
|
+
|
|
138
|
+
if (curRank < prevRank && !allowedDemote) {
|
|
139
|
+
errors.push(`Illegal demotion from ${prev.risk.level} to ${rec.risk.level} without waiver or default recovery`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
valid: errors.length === 0,
|
|
146
|
+
errors
|
|
147
|
+
};
|
|
148
|
+
}
|