@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
package/src/commands/verify.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
-
import { exists, readText, walkFiles } from '../lib/fs.js';
|
|
2
|
+
import { exists, readText, writeText, walkFiles } from '../lib/fs.js';
|
|
3
3
|
import { readYaml } from '../lib/yaml.js';
|
|
4
4
|
import { openspecDir, changeDir } from '../lib/paths.js';
|
|
5
5
|
import { requireRepo, toChangeSlug, readLintConfig } from '../lib/cliutil.js';
|
|
@@ -10,6 +10,8 @@ import { lintSpecText, countBlockers } from '../lib/speclint.js';
|
|
|
10
10
|
import { checkSchema } from '../lib/schema.js';
|
|
11
11
|
import { loadPolicy, isAdvisory, DEFAULT_POLICY, matchesAny } from '../lib/policy.js';
|
|
12
12
|
import { classifyPR } from '../lib/prtype.js';
|
|
13
|
+
import { isSinglePrLevel, requiresHumanReview, readChangeLevel, readEffectiveLevel } from '../lib/level.js';
|
|
14
|
+
import { assess, renderCheckSummary, loadInitialAssessment } from '../assess/index.js';
|
|
13
15
|
import { checkTestPresence, checkTaskEvidence } from '../lib/tdd.js';
|
|
14
16
|
import { log, dim } from '../lib/log.js';
|
|
15
17
|
|
|
@@ -23,7 +25,12 @@ function meta(repoRoot, changeId) {
|
|
|
23
25
|
const text = readText(p);
|
|
24
26
|
const sha = text.match(/^\s*specSha:\s*['"]?([^\s'"]+)['"]?\s*$/m);
|
|
25
27
|
const status = text.match(/^\s*status:\s*['"]?([^\s'"]+)['"]?\s*$/m);
|
|
26
|
-
|
|
28
|
+
const lvl = text.match(/^\s*level:\s*['"]?([^\s'"]+)['"]?\s*$/m);
|
|
29
|
+
return {
|
|
30
|
+
specSha: sha ? sha[1] : null,
|
|
31
|
+
status: status ? status[1] : null,
|
|
32
|
+
level: lvl ? lvl[1].toUpperCase() : null,
|
|
33
|
+
};
|
|
27
34
|
}
|
|
28
35
|
|
|
29
36
|
function readYamlConfig(repoRoot) {
|
|
@@ -114,19 +121,30 @@ export function runVerify(changeId, opts = {}) {
|
|
|
114
121
|
const base = opts.base ?? branch;
|
|
115
122
|
const head = opts.head ?? 'HEAD';
|
|
116
123
|
|
|
117
|
-
const changedFiles = (git(['diff', '--name-only', `${base}...${head}`], repoRoot, { allowFail: true }) ?? '')
|
|
118
|
-
.split(/\r?\n/).filter(Boolean);
|
|
119
|
-
const changeOnMain = changeOnRef(repoRoot, branch, slug);
|
|
120
|
-
const pr = classifyPR({ changedFiles, changeOnMain, policy, emergency: opts.emergency });
|
|
121
|
-
add('pr-type', pr.type === 'invalid' ? 'fail' : 'pass', `type=${pr.type} (${pr.reason})`);
|
|
122
|
-
|
|
123
124
|
const cdir = changeDir(repoRoot, slug);
|
|
124
125
|
if (!exists(cdir)) {
|
|
125
126
|
add('change', 'fail', `change '${slug}' not found`);
|
|
126
127
|
return finish(results, policy);
|
|
127
128
|
}
|
|
128
129
|
add('change', 'pass', slug);
|
|
130
|
+
const effLevel = readEffectiveLevel(repoRoot, changeId);
|
|
131
|
+
if (effLevel?.tampered) {
|
|
132
|
+
add('level-integrity', 'fail', `tampered: declared ${effLevel.declaredLevel} < recorded history ${effLevel.maxHistoryLevel}`);
|
|
133
|
+
} else {
|
|
134
|
+
add('level-integrity', 'pass', `conforms to recorded history (${effLevel?.effectiveLevel || 'L1'})`);
|
|
135
|
+
}
|
|
129
136
|
const { specSha, status } = meta(repoRoot, changeId);
|
|
137
|
+
const changeLevel = effLevel?.effectiveLevel || meta(repoRoot, changeId).level || null;
|
|
138
|
+
|
|
139
|
+
const changedFiles = (git(['diff', '--name-only', `${base}...${head}`], repoRoot, { allowFail: true }) ?? '')
|
|
140
|
+
.split(/\r?\n/).filter(Boolean);
|
|
141
|
+
const changeOnMain = changeOnRef(repoRoot, branch, slug);
|
|
142
|
+
const initialAss = loadInitialAssessment(repoRoot, slug);
|
|
143
|
+
const wasInitialSinglePr = initialAss && isSinglePrLevel(initialAss.risk?.level);
|
|
144
|
+
const isSinglePr = Boolean(wasInitialSinglePr || isSinglePrLevel(changeLevel));
|
|
145
|
+
|
|
146
|
+
const pr = classifyPR({ changedFiles, changeOnMain, policy, emergency: opts.emergency, level: changeLevel, repoRoot, singlePr: isSinglePr });
|
|
147
|
+
add('pr-type', pr.type === 'invalid' ? 'fail' : 'pass', `type=${pr.type} (${pr.reason})`);
|
|
130
148
|
|
|
131
149
|
const required = ['proposal.md', 'tasks.md'];
|
|
132
150
|
const missing = required.filter((f) => !exists(path.join(cdir, f)));
|
|
@@ -137,20 +155,96 @@ export function runVerify(changeId, opts = {}) {
|
|
|
137
155
|
|
|
138
156
|
const lockedPaths = [`openspec/changes/${slug}/proposal.md`, `openspec/changes/${slug}/specs`];
|
|
139
157
|
const approvedSha = lastCommitTouching(repoRoot, branch, lockedPaths);
|
|
140
|
-
if (
|
|
141
|
-
|
|
142
|
-
else
|
|
158
|
+
if (effLevel?.tampered) {
|
|
159
|
+
add('approved-spec', 'fail', 'governance level tampered: declared level is lower than history');
|
|
160
|
+
} else if (isSinglePrLevel(changeLevel) || wasInitialSinglePr) {
|
|
161
|
+
add('approved-spec', 'pass', `${changeLevel || 'single-pr'} single-PR mode (spec approval co-located in implementation PR)`);
|
|
162
|
+
} else if (!approvedSha) {
|
|
163
|
+
add('approved-spec', 'fail', `no commit on locked paths is merged into ${branch} (not approved)`);
|
|
164
|
+
} else if (specSha && specSha !== approvedSha) {
|
|
165
|
+
add('approved-spec', 'fail', `declared specSha ${specSha.slice(0, 8)} != approved commit ${approvedSha.slice(0, 8)}`);
|
|
166
|
+
} else {
|
|
167
|
+
add('approved-spec', 'pass', `approved commit ${approvedSha.slice(0, 8)} on ${branch}`);
|
|
168
|
+
}
|
|
143
169
|
|
|
144
|
-
// Lightweight tamper guard: in
|
|
170
|
+
// Lightweight tamper guard: in a two-phase implementation PR, locked spec files MUST NOT be modified
|
|
145
171
|
const lockedModified = changedFiles.filter((f) => matchesAny(f, policy?.locked_paths ?? DEFAULT_POLICY.locked_paths));
|
|
146
|
-
if (pr.type === 'impl' && lockedModified.length > 0) {
|
|
172
|
+
if (!isSinglePrLevel(changeLevel) && !wasInitialSinglePr && pr.type === 'impl' && lockedModified.length > 0) {
|
|
147
173
|
add('spec-tamper', 'fail', `${lockedModified.length} locked spec file(s) modified in implementation PR (${lockedModified.join(', ')})`);
|
|
148
174
|
} else {
|
|
149
175
|
add('spec-tamper', 'pass', 'locked spec paths unmodified');
|
|
150
176
|
}
|
|
151
177
|
|
|
152
|
-
|
|
153
|
-
|
|
178
|
+
if (isSinglePrLevel(changeLevel) || wasInitialSinglePr) {
|
|
179
|
+
add('flow-state', 'pass', `${changeLevel || 'single-pr'} single-PR mode (direct implementation)`);
|
|
180
|
+
} else {
|
|
181
|
+
add('flow-state', status === 'ready_for_impl' ? 'pass' : 'fail',
|
|
182
|
+
status === 'ready_for_impl' ? 'READY_FOR_IMPL' : `status is ${status ?? 'unset'} (run: asdlc approve ${changeId})`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const isCI = opts.ci !== undefined
|
|
186
|
+
? Boolean(opts.ci)
|
|
187
|
+
: (opts.openingPr ? false : Boolean(process.env.CI || process.env.GITHUB_ACTIONS || process.env.GITLAB_CI || process.env.JENKINS_URL));
|
|
188
|
+
|
|
189
|
+
const assessment = assess({
|
|
190
|
+
stage: 'impl',
|
|
191
|
+
changeId: slug,
|
|
192
|
+
baseSha: base,
|
|
193
|
+
headSha: head,
|
|
194
|
+
changedFiles,
|
|
195
|
+
repoRoot,
|
|
196
|
+
persist: Boolean(repoRoot && changeId && exists(cdir) && !opts.openingPr && !isCI)
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const effectiveLevel = assessment.risk.level;
|
|
200
|
+
const isEscalated = assessment.verdict === 'escalated' || (effLevel?.declaredLevel && effLevel.declaredLevel !== effectiveLevel && effectiveLevel !== 'L1');
|
|
201
|
+
|
|
202
|
+
if (isEscalated) {
|
|
203
|
+
// Synchronize .openspec.yaml on in-place escalation (local runs only, avoid dirtying tree in CI)
|
|
204
|
+
const metaPath = path.join(cdir, '.openspec.yaml');
|
|
205
|
+
if (!isCI && !opts.openingPr && exists(metaPath)) {
|
|
206
|
+
let metaText = readText(metaPath);
|
|
207
|
+
const upsertKey = (text, key, val) => {
|
|
208
|
+
const re = new RegExp(`^\\s*${key}:\\s*.*$`, 'm');
|
|
209
|
+
return re.test(text) ? text.replace(re, `${key}: "${val}"`) : `${text.trimEnd()}\n${key}: "${val}"\n`;
|
|
210
|
+
};
|
|
211
|
+
metaText = upsertKey(metaText, 'level', effectiveLevel);
|
|
212
|
+
metaText = upsertKey(metaText, 'workflowMode', assessment.governance.workflow_mode);
|
|
213
|
+
metaText = upsertKey(metaText, 'escalatedInImpl', 'true');
|
|
214
|
+
writeText(metaPath, metaText);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Humane design: When escalation happens after apply (in implementation),
|
|
218
|
+
// maintain a single PR in-place, but strictly revoke auto-merge and mandate human lead approval.
|
|
219
|
+
add('risk-escape', 'pass', `escalated from ${changeLevel || 'L1'} to ${effectiveLevel} in implementation (in-place single PR maintained; auto-merge revoked, mandatory human lead review required)`);
|
|
220
|
+
} else {
|
|
221
|
+
add('risk-escape', 'pass', `conforms to ${assessment.risk.level} boundary (${assessment.governance.workflow_mode})`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const isHumanReview = assessment.governance.implementation_review === 'ai+human';
|
|
225
|
+
|
|
226
|
+
if (isHumanReview) {
|
|
227
|
+
if (isCI) {
|
|
228
|
+
// In CI, approval cannot rely on self-committed in-tree review.md (P0-3).
|
|
229
|
+
// It must be verified via --approved flag (passed by CI workflow when platform PR approval check passes)
|
|
230
|
+
// or external platform attestation.
|
|
231
|
+
if (opts.approved) {
|
|
232
|
+
add('human-gate', 'pass', `${effectiveLevel}: human lead approval verified on PR`);
|
|
233
|
+
} else {
|
|
234
|
+
add('human-gate', 'fail', `${effectiveLevel}: mandatory human lead approval required before merge (PR not approved by lead)`);
|
|
235
|
+
}
|
|
236
|
+
} else {
|
|
237
|
+
const reviewPath = path.join(cdir, 'review.md');
|
|
238
|
+
const isApprovedInReview = exists(reviewPath) && /##\s*Verdict\s*\n+\s*APPROVE/i.test(readText(reviewPath));
|
|
239
|
+
if (isApprovedInReview || opts.approved) {
|
|
240
|
+
add('human-gate', 'pass', `${effectiveLevel}: local lead review staged (APPROVE in review.md)`);
|
|
241
|
+
} else {
|
|
242
|
+
add('human-gate', 'pass', `${effectiveLevel}: mandatory human lead approval required before merge (auto-merge DENIED)`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
} else {
|
|
246
|
+
add('human-gate', 'pass', `${effectiveLevel}: auto-merge permitted once AI review passes (no human review required)`);
|
|
247
|
+
}
|
|
154
248
|
|
|
155
249
|
const tasksPath = path.join(cdir, 'tasks.md');
|
|
156
250
|
const unchecked = exists(tasksPath) ? countUncheckedTasks(readText(tasksPath)) : Infinity;
|
package/src/lib/agents.js
CHANGED
|
@@ -17,12 +17,13 @@ const ROUTING_BLOCK = `${ROUTING_MARKER}
|
|
|
17
17
|
Everything runs through \`asdlc\` - do NOT call \`openspec ...\` directly. There is no
|
|
18
18
|
raw escape hatch: the wrappers ARE the guards (graph refresh, approval, checks).
|
|
19
19
|
|
|
20
|
-
1. **new** - \`asdlc new "<requirement>"\`: scaffold the change (derives slug from intent) +
|
|
20
|
+
1. **new** - \`asdlc new "<requirement>" [--batch-grill] [--no-grill]\`: scaffold the change (derives slug from intent) +
|
|
21
21
|
refresh the knowledge graph + write \`explore.md\` + seed \`clarifications.md\`.
|
|
22
22
|
▶ **IMMEDIATE ACTION FOR AGENT**: DO NOT stop or ask permission. In the SAME interaction turn,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
ground yourself in \`explore.md\` and codebase facts. Run high-signal conversational **grill-me**
|
|
24
|
+
(or **batch-grill-me** if \`--batch-grill\` was passed) with anti-fatigue guardrails (look facts up yourself,
|
|
25
|
+
focus on material trade-offs, lead with recommendations, early-exit on agreement), and record settled decisions
|
|
26
|
+
in \`clarifications.md\`. (If \`--no-grill\` was passed, self-settle context-inferred assumptions).
|
|
26
27
|
▶ **CRITICAL**: ONLY AFTER \`clarifications.md\` is settled may you author **proposal.md -> specs/**\`. (AI Review is handled on PR by CI).
|
|
27
28
|
2. **grill** - (embedded in Stage 2) settle all design forks in \`clarifications.md\` BEFORE writing specs.
|
|
28
29
|
3. **lint** - \`asdlc lint <ID>\`: artifacts + BDD/acceptance criteria. Open Spec PR via \`asdlc pr <ID> <targetBranch>\`.
|
package/src/lib/fs.js
CHANGED
|
@@ -38,13 +38,36 @@ export function copyTree(src, dest) {
|
|
|
38
38
|
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
39
39
|
const from = path.join(src, entry.name);
|
|
40
40
|
const to = path.join(dest, entry.name);
|
|
41
|
-
if (entry.isDirectory())
|
|
42
|
-
|
|
41
|
+
if (entry.isDirectory()) {
|
|
42
|
+
copyTree(from, to);
|
|
43
|
+
} else {
|
|
44
|
+
let attempts = 0;
|
|
45
|
+
while (attempts < 3) {
|
|
46
|
+
try {
|
|
47
|
+
fs.copyFileSync(from, to);
|
|
48
|
+
break;
|
|
49
|
+
} catch (e) {
|
|
50
|
+
if (e.code === 'EBUSY' && attempts < 2) {
|
|
51
|
+
attempts++;
|
|
52
|
+
const end = Date.now() + 50;
|
|
53
|
+
while (Date.now() < end) {}
|
|
54
|
+
} else {
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
43
60
|
}
|
|
44
61
|
}
|
|
45
62
|
|
|
46
63
|
export function removeDir(p) {
|
|
47
|
-
if (exists(p))
|
|
64
|
+
if (exists(p)) {
|
|
65
|
+
try {
|
|
66
|
+
fs.rmSync(p, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
67
|
+
} catch (err) {
|
|
68
|
+
if (err.code !== 'ENOENT') throw err;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
48
71
|
}
|
|
49
72
|
|
|
50
73
|
const SKIP_DIRS = new Set(['node_modules', '.git', 'dist', 'build', 'out', '.next', 'coverage', 'vendor', 'target']);
|
package/src/lib/level.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { exists, readText } from './fs.js';
|
|
3
|
+
import { changeDir } from './paths.js';
|
|
4
|
+
import { assess, loadMaxHistoryLevel } from '../assess/index.js';
|
|
5
|
+
import { RISK_LEVELS, LEVEL_RANK } from '../assess/rules/builtin.js';
|
|
6
|
+
import { fail } from './errors.js';
|
|
7
|
+
|
|
8
|
+
export const LEVELS = RISK_LEVELS;
|
|
9
|
+
|
|
10
|
+
export function inferInitialLevel({ requirement = '', explicitLevel, repoRoot, changeId } = {}) {
|
|
11
|
+
// Always run assessment to establish immutable baseline in history
|
|
12
|
+
const result = assess({
|
|
13
|
+
stage: 'new',
|
|
14
|
+
changeId: changeId || 'provisional',
|
|
15
|
+
requirement,
|
|
16
|
+
repoRoot,
|
|
17
|
+
persist: Boolean(repoRoot && changeId)
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const systemLevel = result.risk.level;
|
|
21
|
+
let finalLevel = systemLevel;
|
|
22
|
+
let reason = 'bounded local module change';
|
|
23
|
+
|
|
24
|
+
const allEvidence = [
|
|
25
|
+
...(result.vectors.v2_security.evidence || []),
|
|
26
|
+
...(result.vectors.v3_topology.evidence || []),
|
|
27
|
+
...(result.vectors.v1_intent.evidence || [])
|
|
28
|
+
];
|
|
29
|
+
if (allEvidence.length > 0) {
|
|
30
|
+
reason = allEvidence[0].summary;
|
|
31
|
+
} else if (result.reason_codes && result.reason_codes.length > 0) {
|
|
32
|
+
reason = result.reason_codes[0];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (explicitLevel) {
|
|
36
|
+
const norm = String(explicitLevel).toUpperCase();
|
|
37
|
+
if (LEVELS[norm]) {
|
|
38
|
+
const explicitRank = LEVEL_RANK[norm] ?? 1;
|
|
39
|
+
const systemRank = LEVEL_RANK[systemLevel] ?? 1;
|
|
40
|
+
if (explicitRank < systemRank) {
|
|
41
|
+
fail(`cannot downgrade governance level to ${norm}: system evaluation strictly rated requirement as ${systemLevel} (${reason})`, {
|
|
42
|
+
code: 'ILLEGAL_DOWNGRADE',
|
|
43
|
+
hint: 'Level ratings are automated system invariants. Raise a system rule bug if the rating is inaccurate.'
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
finalLevel = norm;
|
|
47
|
+
reason = `explicitly specified via --level ${norm}`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const isSpecFirst = finalLevel === 'L2' || finalLevel === 'L3';
|
|
52
|
+
return {
|
|
53
|
+
level: finalLevel,
|
|
54
|
+
changeType: result.change_type,
|
|
55
|
+
reason,
|
|
56
|
+
workflowMode: isSpecFirst ? 'spec-first' : 'single-pr',
|
|
57
|
+
implementationReview: isSpecFirst ? 'ai+human' : 'ai',
|
|
58
|
+
assessment: result
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function isSinglePrLevel(level) {
|
|
63
|
+
return level === LEVELS.L0 || level === LEVELS.L1;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function requiresHumanReview(level, opts = {}) {
|
|
67
|
+
if (level === LEVELS.L3) return true;
|
|
68
|
+
if (level === LEVELS.L2) return !Boolean(opts?.contractTestVerified);
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function readChangeLevel(repoRoot, changeId) {
|
|
73
|
+
const eff = readEffectiveLevel(repoRoot, changeId);
|
|
74
|
+
return eff ? eff.effectiveLevel : null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function readEffectiveLevel(repoRoot, changeId) {
|
|
78
|
+
if (!repoRoot || !changeId) return null;
|
|
79
|
+
const cdir = changeDir(repoRoot, changeId);
|
|
80
|
+
const metaPath = path.join(cdir, '.openspec.yaml');
|
|
81
|
+
let declaredLevel = null;
|
|
82
|
+
if (exists(metaPath)) {
|
|
83
|
+
const metaText = readText(metaPath);
|
|
84
|
+
const m = metaText.match(/^\s*level:\s*['"]?([^\s'"]+)['"]?\s*$/m);
|
|
85
|
+
declaredLevel = m ? m[1].toUpperCase() : null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let maxHistory = null;
|
|
89
|
+
let chainCorrupt = false;
|
|
90
|
+
try {
|
|
91
|
+
maxHistory = loadMaxHistoryLevel(repoRoot, changeId);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
if (err.code === 'TAMPER_DETECTED' || err.message?.includes('tamper')) {
|
|
94
|
+
chainCorrupt = true;
|
|
95
|
+
} else {
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!declaredLevel && !maxHistory && !chainCorrupt) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const declaredRank = declaredLevel ? (LEVEL_RANK[declaredLevel] ?? 0) : 0;
|
|
105
|
+
const historyRank = maxHistory ? (LEVEL_RANK[maxHistory] ?? 0) : 0;
|
|
106
|
+
|
|
107
|
+
const tampered = chainCorrupt || Boolean(maxHistory && declaredLevel && declaredRank < historyRank);
|
|
108
|
+
const effectiveRank = Math.max(declaredRank, historyRank);
|
|
109
|
+
const effectiveLevel = Object.keys(LEVEL_RANK).find((k) => LEVEL_RANK[k] === effectiveRank) || (declaredLevel || maxHistory || 'L1');
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
declaredLevel,
|
|
113
|
+
maxHistoryLevel: maxHistory,
|
|
114
|
+
effectiveLevel,
|
|
115
|
+
tampered
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
package/src/lib/paths.js
CHANGED
|
@@ -39,5 +39,6 @@ export function openspecDir(repoRoot) {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
export function changeDir(repoRoot, changeId) {
|
|
42
|
-
|
|
42
|
+
const slug = String(changeId).toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
|
|
43
|
+
return path.join(openspecDir(repoRoot), CHANGES_DIR, slug);
|
|
43
44
|
}
|
package/src/lib/prtype.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { matchesAny } from './policy.js';
|
|
2
|
+
import { isSinglePrLevel, readChangeLevel } from './level.js';
|
|
2
3
|
|
|
3
|
-
// PR type is determined by PATHS
|
|
4
|
+
// PR type is determined by PATHS and change governance LEVEL.
|
|
4
5
|
const CHANGE = /^openspec\/changes\/([^/]+)\//;
|
|
5
6
|
const SPECS = /^openspec\/specs\//;
|
|
6
7
|
|
|
@@ -11,7 +12,7 @@ export function changeIdOf(pathname) {
|
|
|
11
12
|
|
|
12
13
|
// -> { type, reason } where type is one of:
|
|
13
14
|
// spec | impl | archive | fast | emergency | normal | invalid
|
|
14
|
-
export function classifyPR({ changedFiles = [], changeOnMain = false, policy, emergency = false } = {}) {
|
|
15
|
+
export function classifyPR({ changedFiles = [], changeOnMain = false, policy, emergency = false, level, repoRoot, singlePr = false } = {}) {
|
|
15
16
|
const files = changedFiles.filter(Boolean);
|
|
16
17
|
if (emergency) return { type: 'emergency', reason: 'marked emergency' };
|
|
17
18
|
|
|
@@ -28,14 +29,26 @@ export function classifyPR({ changedFiles = [], changeOnMain = false, policy, em
|
|
|
28
29
|
&& files.some((f) => SPECS.test(f));
|
|
29
30
|
if (archiveOnly) return { type: 'archive', reason: 'change dir + specs merge only' };
|
|
30
31
|
|
|
32
|
+
// Resolve governance level from argument, repo metadata, or default
|
|
33
|
+
const changeId = changeFiles.length > 0 ? changeIdOf(changeFiles[0]) : null;
|
|
34
|
+
const effectiveLevel = level ?? (repoRoot && changeId ? readChangeLevel(repoRoot, changeId) : null);
|
|
35
|
+
|
|
31
36
|
if (changeFiles.length > 0 && !changeOnMain) {
|
|
32
|
-
|
|
37
|
+
if (singlePr || isSinglePrLevel(effectiveLevel)) {
|
|
38
|
+
// For L0 / L1 or in-place escalated changes, source code and spec delta are permitted in the same Single PR
|
|
39
|
+
return outside.length === 0
|
|
40
|
+
? { type: 'spec', reason: `new change dir, no source (${effectiveLevel ?? 'single-pr'})` }
|
|
41
|
+
: { type: 'impl', reason: `single PR flow for ${effectiveLevel ?? 'escalated'} change (specs and source co-located)` };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// For L2 / L3 (or unlevelled changes): First spec PR must contain the change dir ONLY - no source alongside.
|
|
33
45
|
return outside.length === 0
|
|
34
46
|
? { type: 'spec', reason: 'new change dir, no source' }
|
|
35
|
-
: { type: 'invalid', reason: 'first PR mixes spec files and source; split it' };
|
|
47
|
+
: { type: 'invalid', reason: 'first PR mixes spec files and source; split it for L2/L3 change' };
|
|
36
48
|
}
|
|
37
49
|
|
|
38
50
|
if (changeFiles.length > 0 && changeOnMain) return { type: 'impl', reason: 'existing change dir + source/tasks' };
|
|
39
51
|
if (outside.length > 0) return { type: 'normal', reason: 'no change dir (outside the SDD flow)' };
|
|
40
52
|
return { type: 'normal', reason: files.length === 0 ? 'no changes' : 'unclassified' };
|
|
41
53
|
}
|
|
54
|
+
|
package/src/lib/risk.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { assess } from '../assess/index.js';
|
|
2
|
+
import { RISK_LEVELS, LEVEL_RANK } from '../assess/rules/builtin.js';
|
|
3
|
+
|
|
4
|
+
export { RISK_LEVELS, LEVEL_RANK };
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Backward-compatible adapter for legacy callers/tests, delegating to unified assess engine.
|
|
8
|
+
*/
|
|
9
|
+
export function assessRisk({ requirement = '', changedFiles = [], currentLevel = null, repoRoot = null } = {}) {
|
|
10
|
+
const result = assess({
|
|
11
|
+
stage: changedFiles.length > 0 ? 'impl' : 'new',
|
|
12
|
+
requirement,
|
|
13
|
+
changedFiles,
|
|
14
|
+
previous: currentLevel ? { risk: { level: currentLevel } } : null,
|
|
15
|
+
baseline: currentLevel ? { risk: { level: currentLevel } } : null,
|
|
16
|
+
repoRoot,
|
|
17
|
+
persist: false
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const level = result.risk.level;
|
|
21
|
+
const curRank = currentLevel ? (LEVEL_RANK[currentLevel] ?? 1) : null;
|
|
22
|
+
const assessedRank = LEVEL_RANK[level] ?? 1;
|
|
23
|
+
|
|
24
|
+
let action = 'INITIAL';
|
|
25
|
+
let escalated = false;
|
|
26
|
+
if (curRank !== null) {
|
|
27
|
+
if (assessedRank > curRank) {
|
|
28
|
+
action = 'ESCALATE';
|
|
29
|
+
escalated = true;
|
|
30
|
+
} else {
|
|
31
|
+
action = 'MAINTAIN';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
level,
|
|
37
|
+
currentLevel: currentLevel || null,
|
|
38
|
+
action,
|
|
39
|
+
escalated,
|
|
40
|
+
reasons: result.reason_codes || [],
|
|
41
|
+
findings: {
|
|
42
|
+
regulatory: [],
|
|
43
|
+
architectural: [],
|
|
44
|
+
destructive: [],
|
|
45
|
+
demotionGuards: []
|
|
46
|
+
},
|
|
47
|
+
isSinglePr: level === 'L0' || level === 'L1',
|
|
48
|
+
requiresHumanReview: result.governance.implementation_review === 'ai+human' || level === 'L3'
|
|
49
|
+
};
|
|
50
|
+
}
|
package/src/lib/schema.js
CHANGED
|
@@ -31,6 +31,10 @@ export function installedSchemaDir(opts = {}) {
|
|
|
31
31
|
// Install the BU-managed schema ONCE per machine (not per repo).
|
|
32
32
|
export function installSchema(opts = {}) {
|
|
33
33
|
const dest = installedSchemaDir(opts);
|
|
34
|
+
if (!opts.force && exists(dest)) {
|
|
35
|
+
const installed = readInstalledVersion(opts);
|
|
36
|
+
if (installed) return dest;
|
|
37
|
+
}
|
|
34
38
|
removeDir(dest); // always replace -> no stale half-copies
|
|
35
39
|
ensureDir(globalSchemasDir(opts));
|
|
36
40
|
copyTree(schemaAssetDir, dest);
|