@akjamie/agentic-sdlc 0.0.0-stage → 1.1.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 +219 -2
- package/assets/ci/asdlc-ai-review.yml +65 -0
- package/assets/config/config.yaml +76 -0
- package/assets/githooks/post-merge +22 -0
- package/assets/githooks/pre-push +17 -0
- package/assets/policy/policy.yaml +46 -0
- package/assets/schema/schema.yaml +168 -0
- package/assets/schema/templates/design.md +134 -0
- package/assets/schema/templates/proposal.md +25 -0
- package/assets/schema/templates/review.md +24 -0
- package/assets/schema/templates/spec.md +37 -0
- package/assets/schema/templates/tasks.md +10 -0
- package/assets/skills/asdlc-apply/SKILL.md +128 -0
- package/assets/skills/asdlc-approve/SKILL.md +107 -0
- package/assets/skills/asdlc-archive/SKILL.md +49 -0
- package/assets/skills/asdlc-doctor/SKILL.md +82 -0
- package/assets/skills/asdlc-impl-review/SKILL.md +219 -0
- package/assets/skills/asdlc-init/SKILL.md +68 -0
- package/assets/skills/asdlc-lint/SKILL.md +59 -0
- package/assets/skills/asdlc-new/SKILL.md +107 -0
- package/assets/skills/asdlc-pr/SKILL.md +59 -0
- package/assets/skills/asdlc-review/SKILL.md +80 -0
- package/assets/skills/asdlc-spec-review/SKILL.md +206 -0
- package/assets/skills/asdlc-status/SKILL.md +49 -0
- package/assets/skills/asdlc-upgrade/SKILL.md +34 -0
- package/assets/skills/asdlc-verify/SKILL.md +55 -0
- package/assets/skills/batch-grill-me/SKILL.md +136 -0
- package/assets/skills/grilling/SKILL.md +136 -0
- package/bin/asdlc.js +6 -0
- package/package.json +43 -4
- package/src/cli.js +89 -0
- package/src/commands/apply.js +114 -0
- package/src/commands/approve.js +166 -0
- package/src/commands/archive.js +143 -0
- package/src/commands/doctor.js +163 -0
- package/src/commands/init.js +201 -0
- package/src/commands/lint.js +127 -0
- package/src/commands/new.js +171 -0
- package/src/commands/pr.js +495 -0
- package/src/commands/skills.js +184 -0
- package/src/commands/status.js +65 -0
- package/src/commands/test.js +137 -0
- package/src/commands/update.js +103 -0
- package/src/commands/upgrade.js +175 -0
- package/src/commands/verify.js +229 -0
- package/src/index.js +3 -0
- package/src/lib/agents.js +73 -0
- package/src/lib/cliutil.js +99 -0
- package/src/lib/errors.js +18 -0
- package/src/lib/exec.js +23 -0
- package/src/lib/fs.js +83 -0
- package/src/lib/git.js +76 -0
- package/src/lib/graphify.js +106 -0
- package/src/lib/log.js +15 -0
- package/src/lib/openspec.js +91 -0
- package/src/lib/paths.js +43 -0
- package/src/lib/policy.js +60 -0
- package/src/lib/prtype.js +41 -0
- package/src/lib/schema.js +86 -0
- package/src/lib/speclint.js +173 -0
- package/src/lib/tdd.js +71 -0
- package/src/lib/yaml.js +18 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { exists, readText, walkFiles } from '../lib/fs.js';
|
|
3
|
+
import { readYaml } from '../lib/yaml.js';
|
|
4
|
+
import { openspecDir, changeDir } from '../lib/paths.js';
|
|
5
|
+
import { requireRepo, toChangeSlug, readLintConfig } from '../lib/cliutil.js';
|
|
6
|
+
import { validateChange } from '../lib/openspec.js';
|
|
7
|
+
import { runShell } from '../lib/exec.js';
|
|
8
|
+
import { lastCommitTouching, changeOnRef, git } from '../lib/git.js';
|
|
9
|
+
import { lintSpecText, countBlockers } from '../lib/speclint.js';
|
|
10
|
+
import { checkSchema } from '../lib/schema.js';
|
|
11
|
+
import { loadPolicy, isAdvisory, DEFAULT_POLICY, matchesAny } from '../lib/policy.js';
|
|
12
|
+
import { classifyPR } from '../lib/prtype.js';
|
|
13
|
+
import { checkTestPresence, checkTaskEvidence } from '../lib/tdd.js';
|
|
14
|
+
import { log, dim } from '../lib/log.js';
|
|
15
|
+
|
|
16
|
+
// THE implementation check (v10: gate absorbed verify). Deterministic checks
|
|
17
|
+
// first, then - if a verifier is configured - independent AI verification
|
|
18
|
+
// (player != referee).
|
|
19
|
+
|
|
20
|
+
function meta(repoRoot, changeId) {
|
|
21
|
+
const p = path.join(changeDir(repoRoot, toChangeSlug(changeId)), '.openspec.yaml');
|
|
22
|
+
if (!exists(p)) return {};
|
|
23
|
+
const text = readText(p);
|
|
24
|
+
const sha = text.match(/^\s*specSha:\s*['"]?([^\s'"]+)['"]?\s*$/m);
|
|
25
|
+
const status = text.match(/^\s*status:\s*['"]?([^\s'"]+)['"]?\s*$/m);
|
|
26
|
+
return { specSha: sha ? sha[1] : null, status: status ? status[1] : null };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function readYamlConfig(repoRoot) {
|
|
30
|
+
const cfg = path.join(openspecDir(repoRoot), 'config.yaml');
|
|
31
|
+
if (!exists(cfg)) return {};
|
|
32
|
+
try { return readYaml(cfg) ?? {}; } catch { return {}; }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function countUncheckedTasks(text) {
|
|
36
|
+
return (text.match(/^\s*-\s+\[\s\]/gm) ?? []).length;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function readExternalChecks(repoRoot) {
|
|
40
|
+
const ext = (readYamlConfig(repoRoot).gates ?? {}).external;
|
|
41
|
+
if (!Array.isArray(ext)) return [];
|
|
42
|
+
return ext.filter((e) => e && typeof e.command === 'string').map((e) => ({ name: e.name ?? e.command, command: e.command }));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function finish(results, policy) {
|
|
46
|
+
let failed = 0;
|
|
47
|
+
log.info('');
|
|
48
|
+
for (const r of results) {
|
|
49
|
+
const status = r.status === 'fail' && isAdvisory(policy, r.name) ? 'warn' : r.status;
|
|
50
|
+
const badge = { pass: 'PASS', fail: 'FAIL', warn: 'WARN', skip: 'SKIP' }[status];
|
|
51
|
+
if (status === 'fail') failed += 1;
|
|
52
|
+
log.plain(` [${badge}] ${r.name}`);
|
|
53
|
+
if (r.detail) log.plain(` ${dim(r.detail)}`);
|
|
54
|
+
}
|
|
55
|
+
log.info('');
|
|
56
|
+
if (failed) { log.plain(`verify failed: ${failed} check(s)`); return 1; }
|
|
57
|
+
log.plain('verify passed');
|
|
58
|
+
return 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function intent(cdir) {
|
|
62
|
+
const parts = [];
|
|
63
|
+
for (const f of ['proposal.md', 'review.md', 'tasks.md']) {
|
|
64
|
+
const p = path.join(cdir, f);
|
|
65
|
+
if (exists(p)) parts.push(`===== ${f} =====\n${readText(p)}`);
|
|
66
|
+
}
|
|
67
|
+
const specsDir = path.join(cdir, 'specs');
|
|
68
|
+
if (exists(specsDir)) {
|
|
69
|
+
for (const f of walkFiles(specsDir).filter((x) => x.endsWith('spec.md'))) {
|
|
70
|
+
parts.push(`===== specs/${f} =====\n${readText(path.join(specsDir, f))}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return parts.join('\n\n');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function buildPrompt(changeId, intentText, diff, base) {
|
|
77
|
+
return `You are an INDEPENDENT VERIFIER for change ${changeId}. You did NOT author it.
|
|
78
|
+
Verify the IMPLEMENTATION satisfies the APPROVED INTENT. Be skeptical.
|
|
79
|
+
- Decide PASS/FAIL for every requirement / acceptance criterion and every task.
|
|
80
|
+
- FAIL if a requirement is unmet, a task is unchecked/unimplemented, or test evidence is missing.
|
|
81
|
+
Output ONLY strict JSON: {"verdict":"PASS|FAIL","summary":"...","requirements":[{"id":"...","verdict":"PASS|FAIL","evidence":"...","gaps":"..."}]}
|
|
82
|
+
|
|
83
|
+
===== APPROVED INTENT =====
|
|
84
|
+
${intentText}
|
|
85
|
+
|
|
86
|
+
===== IMPLEMENTATION DIFF (base ${base}) =====
|
|
87
|
+
${diff || '(empty diff)'}
|
|
88
|
+
`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function extractJson(text) {
|
|
92
|
+
const start = text.indexOf('{');
|
|
93
|
+
const end = text.lastIndexOf('}');
|
|
94
|
+
if (start < 0 || end <= start) return null;
|
|
95
|
+
try { return JSON.parse(text.slice(start, end + 1)); } catch { return null; }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function runVerify(changeId, opts = {}) {
|
|
99
|
+
const results = [];
|
|
100
|
+
const add = (name, status, detail) => results.push({ name, status, detail });
|
|
101
|
+
const branch = opts.protectedBranch ?? 'origin/main';
|
|
102
|
+
|
|
103
|
+
let repoRoot;
|
|
104
|
+
try {
|
|
105
|
+
repoRoot = requireRepo(opts);
|
|
106
|
+
add('repo', 'pass', repoRoot);
|
|
107
|
+
} catch (err) {
|
|
108
|
+
add('repo', 'fail', err.message);
|
|
109
|
+
return finish(results, DEFAULT_POLICY);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const policy = loadPolicy(repoRoot);
|
|
113
|
+
const slug = toChangeSlug(changeId);
|
|
114
|
+
const base = opts.base ?? branch;
|
|
115
|
+
const head = opts.head ?? 'HEAD';
|
|
116
|
+
|
|
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
|
+
const cdir = changeDir(repoRoot, slug);
|
|
124
|
+
if (!exists(cdir)) {
|
|
125
|
+
add('change', 'fail', `change '${slug}' not found`);
|
|
126
|
+
return finish(results, policy);
|
|
127
|
+
}
|
|
128
|
+
add('change', 'pass', slug);
|
|
129
|
+
const { specSha, status } = meta(repoRoot, changeId);
|
|
130
|
+
|
|
131
|
+
const required = ['proposal.md', 'tasks.md'];
|
|
132
|
+
const missing = required.filter((f) => !exists(path.join(cdir, f)));
|
|
133
|
+
const specFiles = exists(path.join(cdir, 'specs'))
|
|
134
|
+
? walkFiles(path.join(cdir, 'specs')).filter((f) => f.endsWith('spec.md')) : [];
|
|
135
|
+
if (missing.length === 0 && specFiles.length > 0) add('artifacts', 'pass', 'proposal, tasks, spec delta present');
|
|
136
|
+
else add('artifacts', 'fail', [...missing.map((f) => `${f} missing`), ...(specFiles.length === 0 ? ['no spec delta'] : [])].join('; '));
|
|
137
|
+
|
|
138
|
+
const lockedPaths = [`openspec/changes/${slug}/proposal.md`, `openspec/changes/${slug}/specs`];
|
|
139
|
+
const approvedSha = lastCommitTouching(repoRoot, branch, lockedPaths);
|
|
140
|
+
if (!approvedSha) add('approved-spec', 'fail', `no commit on locked paths is merged into ${branch} (not approved)`);
|
|
141
|
+
else if (specSha && specSha !== approvedSha) add('approved-spec', 'fail', `declared specSha ${specSha.slice(0, 8)} != approved commit ${approvedSha.slice(0, 8)}`);
|
|
142
|
+
else add('approved-spec', 'pass', `approved commit ${approvedSha.slice(0, 8)} on ${branch}`);
|
|
143
|
+
|
|
144
|
+
// Lightweight tamper guard: in an implementation PR, locked spec files MUST NOT be modified
|
|
145
|
+
const lockedModified = changedFiles.filter((f) => matchesAny(f, policy?.locked_paths ?? DEFAULT_POLICY.locked_paths));
|
|
146
|
+
if (pr.type === 'impl' && lockedModified.length > 0) {
|
|
147
|
+
add('spec-tamper', 'fail', `${lockedModified.length} locked spec file(s) modified in implementation PR (${lockedModified.join(', ')})`);
|
|
148
|
+
} else {
|
|
149
|
+
add('spec-tamper', 'pass', 'locked spec paths unmodified');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
add('flow-state', status === 'ready_for_impl' ? 'pass' : 'fail',
|
|
153
|
+
status === 'ready_for_impl' ? 'READY_FOR_IMPL' : `status is ${status ?? 'unset'} (run: asdlc approve ${changeId})`);
|
|
154
|
+
|
|
155
|
+
const tasksPath = path.join(cdir, 'tasks.md');
|
|
156
|
+
const unchecked = exists(tasksPath) ? countUncheckedTasks(readText(tasksPath)) : Infinity;
|
|
157
|
+
add('tasks-complete', unchecked === 0 ? 'pass' : 'fail',
|
|
158
|
+
unchecked === 0 ? 'all tasks checked' : (Number.isFinite(unchecked) ? `${unchecked} unchecked task(s)` : 'tasks.md missing'));
|
|
159
|
+
|
|
160
|
+
const testDiff = checkTestPresence(changedFiles);
|
|
161
|
+
add('test-presence', testDiff.ok ? 'pass' : 'fail', testDiff.detail);
|
|
162
|
+
|
|
163
|
+
if (exists(tasksPath)) {
|
|
164
|
+
const evidence = checkTaskEvidence(readText(tasksPath));
|
|
165
|
+
add('tdd-evidence', evidence.ok ? 'pass' : 'fail', evidence.detail);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const { rules } = readLintConfig(repoRoot);
|
|
169
|
+
const findings = [];
|
|
170
|
+
for (const file of specFiles) {
|
|
171
|
+
for (const fnd of lintSpecText(readText(path.join(cdir, 'specs', file)), { repoRoot, rules })) findings.push({ ...fnd, file });
|
|
172
|
+
}
|
|
173
|
+
const blockers = countBlockers(findings);
|
|
174
|
+
add('BDD + acceptance criteria', blockers === 0 ? 'pass' : 'fail', blockers === 0 ? `${findings.length} advisory finding(s)` : `${blockers} blocker(s)`);
|
|
175
|
+
|
|
176
|
+
if (opts.skipOpenspec) {
|
|
177
|
+
add('openspec-validate', 'skip', '--skip-openspec');
|
|
178
|
+
} else {
|
|
179
|
+
const res = validateChange(repoRoot, { strict: true, all: true });
|
|
180
|
+
if (res.missing) add('openspec-validate', 'fail', 'openspec CLI not found');
|
|
181
|
+
else if (res.status === 0) add('openspec-validate', 'pass', 'strict validation passed');
|
|
182
|
+
else add('openspec-validate', 'fail', `${res.stdout}${res.stderr}`.trim().slice(0, 600));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const schema = checkSchema(repoRoot, { expectedVersion: readYamlConfig(repoRoot)?.schemaVersion ?? null });
|
|
186
|
+
add('schema-managed', schema.ok ? 'pass' : 'fail',
|
|
187
|
+
schema.ok ? `managed schema v${schema.installed}` : `${schema.reason}${schema.hint ? ` (${schema.hint})` : ''}`);
|
|
188
|
+
|
|
189
|
+
if (!opts.skipExternal) {
|
|
190
|
+
const SECRET = /TOKEN|SECRET|PASSWORD|PASSWD|API_?KEY|PRIVATE_?KEY|CREDENTIAL/i;
|
|
191
|
+
const cleanEnv = Object.fromEntries(Object.entries(process.env).filter(([k]) => !SECRET.test(k)));
|
|
192
|
+
const env = { ...cleanEnv, ASDLC_CHANGE_ID: changeId, ASDLC_REPO: opts.repo ?? '', ASDLC_SPEC_SHA: specSha ?? '' };
|
|
193
|
+
for (const check of readExternalChecks(repoRoot)) {
|
|
194
|
+
const res = runShell(check.command, repoRoot, { env });
|
|
195
|
+
if (res.status === 0) add(check.name, 'pass', '');
|
|
196
|
+
else add(check.name, 'fail', `${res.stdout}${res.stderr}`.trim().slice(0, 600) || `exit ${res.status}`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const vcfg = readYamlConfig(repoRoot).verify ?? {};
|
|
201
|
+
const command = process.env.ASDLC_VERIFY_COMMAND ?? vcfg.command;
|
|
202
|
+
const required2 = Boolean(opts.required ?? vcfg.required ?? false);
|
|
203
|
+
if (!command) {
|
|
204
|
+
add('AI verification', required2 ? 'fail' : 'skip', required2 ? 'no verifier configured (set ASDLC_VERIFY_COMMAND)' : 'no verifier configured');
|
|
205
|
+
} else {
|
|
206
|
+
const diff = git(['diff', `${base}...${head}`], repoRoot, { allowFail: true }) ?? '';
|
|
207
|
+
const res = runShell(command, repoRoot, { input: buildPrompt(changeId, intent(cdir), diff, base), timeout: 900000 });
|
|
208
|
+
const report = extractJson(res.stdout) ?? { verdict: 'FAIL', summary: 'verifier produced no parseable JSON' };
|
|
209
|
+
const ok = String(report.verdict).toUpperCase() === 'PASS' && res.status === 0;
|
|
210
|
+
add('AI verification', ok ? 'pass' : 'fail', String(report.summary ?? '').slice(0, 300));
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return finish(results, policy);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function register(program) {
|
|
217
|
+
program
|
|
218
|
+
.command('verify <changeId>')
|
|
219
|
+
.description('check the implementation: artifacts + approved SHA + tasks complete + lint + validate + AI verification')
|
|
220
|
+
.option('--repo <name>', 'repository name')
|
|
221
|
+
.option('--protected-branch <ref>', 'branch the approved spec must be merged into', 'origin/main')
|
|
222
|
+
.option('--base <ref>', 'base ref for the PR diff (default: protected branch)')
|
|
223
|
+
.option('--head <ref>', 'head ref (default: HEAD)')
|
|
224
|
+
.option('--skip-openspec', 'skip openspec validation')
|
|
225
|
+
.option('--skip-external', 'skip external checks from config')
|
|
226
|
+
.action((changeId, opts) => {
|
|
227
|
+
process.exitCode = runVerify(changeId, opts);
|
|
228
|
+
});
|
|
229
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { exists, readText, writeText } from './fs.js';
|
|
3
|
+
import { runShell } from './exec.js';
|
|
4
|
+
import { graphExists } from './graphify.js';
|
|
5
|
+
|
|
6
|
+
// AGENTS.md is the routing layer the agentic IDE reads. Two machine-managed
|
|
7
|
+
// blocks (markers): the asdlc workflow, and the system status derived from the
|
|
8
|
+
// graphify graph (deterministic query, not an LLM summary). Hand-written
|
|
9
|
+
// content elsewhere in AGENTS.md is preserved.
|
|
10
|
+
|
|
11
|
+
export const ROUTING_MARKER = '<!-- asdlc:routing -->';
|
|
12
|
+
export const STATUS_MARKER = '<!-- asdlc:system-status -->';
|
|
13
|
+
|
|
14
|
+
const ROUTING_BLOCK = `${ROUTING_MARKER}
|
|
15
|
+
## Agentic SDLC — the 7 stages (one front door)
|
|
16
|
+
|
|
17
|
+
Everything runs through \`asdlc\` - do NOT call \`openspec ...\` directly. There is no
|
|
18
|
+
raw escape hatch: the wrappers ARE the guards (graph refresh, approval, checks).
|
|
19
|
+
|
|
20
|
+
1. **new** - \`asdlc new "<requirement>"\`: scaffold the change (derives slug from intent) +
|
|
21
|
+
refresh the knowledge graph + write \`explore.md\` + seed \`clarifications.md\`.
|
|
22
|
+
▶ **IMMEDIATE ACTION FOR AGENT**: DO NOT stop or ask permission. In the SAME interaction turn,
|
|
23
|
+
immediately run the **batch-grill-me** (or **grilling**) skill using the **batch-grill-me** protocol (evaluate the full frontier
|
|
24
|
+
in 1-2 structured batches with recommendations, looking facts up yourself) and record settled decisions
|
|
25
|
+
in \`clarifications.md\`.
|
|
26
|
+
▶ **CRITICAL**: ONLY AFTER \`clarifications.md\` is settled may you author **proposal.md -> specs/**\`. (AI Review is handled on PR by CI).
|
|
27
|
+
2. **grill** - (embedded in Stage 2) settle all design forks in \`clarifications.md\` BEFORE writing specs.
|
|
28
|
+
3. **lint** - \`asdlc lint <ID>\`: artifacts + BDD/acceptance criteria. Open Spec PR via \`asdlc pr <ID> <targetBranch>\`.
|
|
29
|
+
4. **approve** - human/TL approves (via PR merge, on PR, or in-place); \`asdlc approve <ID>\` records approved SHA + READY_FOR_IMPL.
|
|
30
|
+
5. **apply** - \`asdlc apply <ID>\` (guard: the spec must be approved), then
|
|
31
|
+
\`/opsx-apply\` with task-level TDD + task-level converge.
|
|
32
|
+
6. **verify** - \`asdlc verify <ID>\`: approved SHA, artifacts, tasks complete, tests, AI.
|
|
33
|
+
7. **archive** - \`asdlc archive <ID>\`: sync specs to openspec/specs/ + update README.md (updates AGENTS.md only for constitutional changes).
|
|
34
|
+
|
|
35
|
+
Need orientation? \`asdlc status <ID>\` (where am I / next command) and \`asdlc doctor\`
|
|
36
|
+
(environment health). A bad spec is never edited: close it and \`asdlc new\` again.`;
|
|
37
|
+
|
|
38
|
+
function upsertBlock(text, marker, block) {
|
|
39
|
+
const idx = text.indexOf(marker);
|
|
40
|
+
if (idx < 0) {
|
|
41
|
+
const sep = text && !text.endsWith('\n') ? '\n' : '';
|
|
42
|
+
return `${text}${sep}${sep}${block}\n`;
|
|
43
|
+
}
|
|
44
|
+
const rest = text.slice(idx + marker.length);
|
|
45
|
+
const next = rest.match(/\n<!-- asdlc:/);
|
|
46
|
+
const end = next ? idx + marker.length + next.index : text.length;
|
|
47
|
+
return `${text.slice(0, idx)}${block}\n${text.slice(end)}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function systemStatusBlock(repoRoot) {
|
|
51
|
+
if (!graphExists(repoRoot)) return `${STATUS_MARKER}
|
|
52
|
+
## System status (from the knowledge graph)
|
|
53
|
+
|
|
54
|
+
_(graph not built yet - run \`asdlc init\` or \`asdlc new <change>\`)_`;
|
|
55
|
+
const res = runShell('graphify god-nodes', repoRoot);
|
|
56
|
+
const body = (res.stdout || '').trim().split(/\r?\n/).slice(0, 15).join('\n');
|
|
57
|
+
return `${STATUS_MARKER}
|
|
58
|
+
## System status (from the knowledge graph, generated - do not hand-edit)
|
|
59
|
+
|
|
60
|
+
${body || '_(no nodes)_'}
|
|
61
|
+
|
|
62
|
+
_Architectural hubs = the most-connected modules. Run \`asdlc new <change>\`
|
|
63
|
+
to query the graph for detail._`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function refreshAgents(repoRoot, { systemStatus = true } = {}) {
|
|
67
|
+
const p = path.join(repoRoot, 'AGENTS.md');
|
|
68
|
+
let text = exists(p) ? readText(p) : '';
|
|
69
|
+
text = upsertBlock(text, ROUTING_MARKER, ROUTING_BLOCK);
|
|
70
|
+
if (systemStatus) text = upsertBlock(text, STATUS_MARKER, systemStatusBlock(repoRoot));
|
|
71
|
+
writeText(p, text);
|
|
72
|
+
return p;
|
|
73
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { resolveRepoRoot, openspecDir } from './paths.js';
|
|
3
|
+
import { exists, readText } from './fs.js';
|
|
4
|
+
import { readYaml } from './yaml.js';
|
|
5
|
+
import { fail } from './errors.js';
|
|
6
|
+
|
|
7
|
+
export function toChangeSlug(changeId) {
|
|
8
|
+
return String(changeId)
|
|
9
|
+
.toLowerCase()
|
|
10
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
11
|
+
.replace(/^-+|-+$/g, '');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function extractRequirementTerms(text) {
|
|
15
|
+
const s = String(text ?? '')
|
|
16
|
+
.replace(/\b[A-Za-z]{2,10}-\d+\b[:\s-]*/g, '')
|
|
17
|
+
.toLowerCase()
|
|
18
|
+
.replace(/\b(i\s+want\s+to\s+build|i\s+want\s+to\s+add|i\s+want\s+to|we\s+need\s+to|please\s+add|build|create|add|implement)\b/gi, '')
|
|
19
|
+
.replace(/\b(a|an|the|of|for|with|in|on|to|and|by)\b/gi, '')
|
|
20
|
+
.replace(/[^a-z0-9\s]/g, ' ')
|
|
21
|
+
.trim();
|
|
22
|
+
const words = s.split(/\s+/).filter(Boolean);
|
|
23
|
+
return words.slice(0, 5).join(' ');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function deriveChangeSlug(input, opts = {}) {
|
|
27
|
+
if (opts.id) return toChangeSlug(opts.id);
|
|
28
|
+
const text = String(input ?? '').trim();
|
|
29
|
+
if (!text) return 'change';
|
|
30
|
+
|
|
31
|
+
// If already a short alphanumeric ID or slug (e.g. TECH-1234 or rate-limiting) without spaces
|
|
32
|
+
if (!text.includes(' ') && text.length <= 40 && /^[a-zA-Z0-9_-]+$/.test(text)) {
|
|
33
|
+
return toChangeSlug(text);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Extract ticket ID if present (e.g. "TECH-1234: i want to build...")
|
|
37
|
+
const ticketMatch = text.match(/\b([A-Za-z]{2,10}-\d+)\b/);
|
|
38
|
+
const ticket = ticketMatch ? ticketMatch[1].toLowerCase() : null;
|
|
39
|
+
|
|
40
|
+
// Strip filler phrases and ticket prefix
|
|
41
|
+
let cleaned = text
|
|
42
|
+
.replace(/\b[A-Za-z]{2,10}-\d+\b[:\s-]*/g, '')
|
|
43
|
+
.toLowerCase()
|
|
44
|
+
.replace(/\b(i\s+want\s+to\s+build|i\s+want\s+to\s+add|i\s+want\s+to|we\s+need\s+to|please\s+add|build|create|add|implement)\b/gi, '')
|
|
45
|
+
.replace(/\b(a|an|the|of|for|with|in|on|to|and|by)\b/gi, '')
|
|
46
|
+
.trim();
|
|
47
|
+
|
|
48
|
+
if (!cleaned) cleaned = text.toLowerCase();
|
|
49
|
+
|
|
50
|
+
const words = cleaned
|
|
51
|
+
.replace(/[^a-z0-9\s-]/g, ' ')
|
|
52
|
+
.split(/\s+/)
|
|
53
|
+
.filter(Boolean)
|
|
54
|
+
.slice(0, 4);
|
|
55
|
+
|
|
56
|
+
const keywords = words.join('-');
|
|
57
|
+
if (ticket && keywords) return `${ticket}-${keywords}`.slice(0, 40).replace(/-+$/, '');
|
|
58
|
+
if (ticket) return ticket;
|
|
59
|
+
if (keywords) return keywords.slice(0, 40).replace(/-+$/, '');
|
|
60
|
+
const fallback = toChangeSlug(text).slice(0, 40);
|
|
61
|
+
if (fallback) return fallback;
|
|
62
|
+
|
|
63
|
+
// Non-ASCII fallback (e.g. pure Chinese input): generate deterministic slug from content hash
|
|
64
|
+
const hash = Math.abs(text.split('').reduce((acc, c) => ((acc << 5) - acc) + c.charCodeAt(0), 0)).toString(36);
|
|
65
|
+
return `change-${hash || Date.now().toString(36)}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function requireRepo(opts = {}) {
|
|
69
|
+
const repoRoot = resolveRepoRoot(opts);
|
|
70
|
+
if (!exists(openspecDir(repoRoot))) {
|
|
71
|
+
fail(`no OpenSpec project found at ${repoRoot}`, {
|
|
72
|
+
code: 'NOT_INITIALIZED',
|
|
73
|
+
hint: 'Run `asdlc init` first.',
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return repoRoot;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function jsonOut(value) {
|
|
80
|
+
process.stdout.write(`${JSON.stringify(value, null, 2)}\n`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Policy-as-code for the lint harness: severities live in the repo's
|
|
84
|
+
// openspec/config.yaml so a BU can tune policy without forking the tool.
|
|
85
|
+
export function readLintConfig(repoRoot) {
|
|
86
|
+
const cfg = path.join(openspecDir(repoRoot), 'config.yaml');
|
|
87
|
+
const empty = { rules: {}, version: null };
|
|
88
|
+
if (!exists(cfg)) return empty;
|
|
89
|
+
try {
|
|
90
|
+
const parsed = readYaml(cfg) ?? {};
|
|
91
|
+
const lint = parsed.lint ?? {};
|
|
92
|
+
return {
|
|
93
|
+
rules: typeof lint.rules === 'object' && lint.rules !== null ? lint.rules : {},
|
|
94
|
+
version: typeof lint.version === 'string' ? lint.version : null,
|
|
95
|
+
};
|
|
96
|
+
} catch {
|
|
97
|
+
return empty;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export class AsdlcError extends Error {
|
|
2
|
+
constructor(message, { code = 'ASDLC_ERROR', exitCode = 1, detail, hint } = {}) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = 'AsdlcError';
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.exitCode = exitCode;
|
|
7
|
+
this.detail = detail;
|
|
8
|
+
this.hint = hint;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function fail(message, opts) {
|
|
13
|
+
throw new AsdlcError(message, opts);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function assert(condition, message, opts) {
|
|
17
|
+
if (!condition) fail(message, opts);
|
|
18
|
+
}
|
package/src/lib/exec.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
export function runShell(command, cwd, { env = {}, timeout = 600000, input } = {}) {
|
|
4
|
+
const mergedEnv = { ...process.env, ...env };
|
|
5
|
+
for (const [k, v] of Object.entries(env)) {
|
|
6
|
+
if (v === undefined) delete mergedEnv[k];
|
|
7
|
+
}
|
|
8
|
+
const res = spawnSync(command, {
|
|
9
|
+
cwd,
|
|
10
|
+
encoding: 'utf8',
|
|
11
|
+
shell: true,
|
|
12
|
+
timeout,
|
|
13
|
+
env: mergedEnv,
|
|
14
|
+
input, // optional stdin: feeds prompts to a headless verifier agent
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
status: res.status ?? 1,
|
|
18
|
+
stdout: res.stdout ?? '',
|
|
19
|
+
stderr: res.stderr ?? '',
|
|
20
|
+
failed: res.error != null || res.status === null,
|
|
21
|
+
error: res.error?.message,
|
|
22
|
+
};
|
|
23
|
+
}
|
package/src/lib/fs.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
export function exists(p) {
|
|
5
|
+
return fs.existsSync(p);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function ensureDir(p) {
|
|
9
|
+
fs.mkdirSync(p, { recursive: true });
|
|
10
|
+
return p;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function readText(p) {
|
|
14
|
+
return fs.readFileSync(p, 'utf8');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function writeText(p, content) {
|
|
18
|
+
ensureDir(path.dirname(p));
|
|
19
|
+
fs.writeFileSync(p, content, 'utf8');
|
|
20
|
+
return p;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function readJson(p) {
|
|
24
|
+
return JSON.parse(readText(p));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function writeJson(p, value) {
|
|
28
|
+
return writeText(p, `${JSON.stringify(value, null, 2)}\n`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function listDir(p) {
|
|
32
|
+
if (!exists(p)) return [];
|
|
33
|
+
return fs.readdirSync(p, { withFileTypes: true });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function copyTree(src, dest) {
|
|
37
|
+
ensureDir(dest);
|
|
38
|
+
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
39
|
+
const from = path.join(src, entry.name);
|
|
40
|
+
const to = path.join(dest, entry.name);
|
|
41
|
+
if (entry.isDirectory()) copyTree(from, to);
|
|
42
|
+
else fs.copyFileSync(from, to);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function removeDir(p) {
|
|
47
|
+
if (exists(p)) fs.rmSync(p, { recursive: true, force: true });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const SKIP_DIRS = new Set(['node_modules', '.git', 'dist', 'build', 'out', '.next', 'coverage', 'vendor', 'target']);
|
|
51
|
+
|
|
52
|
+
// Recursively list files under `root` as POSIX-style paths relative to root.
|
|
53
|
+
export function walkFiles(root, { maxDepth = 4, limit = 4000 } = {}) {
|
|
54
|
+
const out = [];
|
|
55
|
+
const visit = (dir, depth) => {
|
|
56
|
+
if (out.length >= limit || depth > maxDepth) return;
|
|
57
|
+
let entries;
|
|
58
|
+
try {
|
|
59
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
60
|
+
} catch {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
for (const entry of entries) {
|
|
64
|
+
if (out.length >= limit) return;
|
|
65
|
+
if (entry.isDirectory() && (entry.name.startsWith('.') || SKIP_DIRS.has(entry.name))) continue;
|
|
66
|
+
const full = path.join(dir, entry.name);
|
|
67
|
+
if (entry.isDirectory()) visit(full, depth + 1);
|
|
68
|
+
else out.push(path.relative(root, full).replaceAll('\\', '/'));
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
visit(root, 0);
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function findUp(startDir, predicate) {
|
|
76
|
+
let dir = path.resolve(startDir);
|
|
77
|
+
for (;;) {
|
|
78
|
+
if (predicate(dir)) return dir;
|
|
79
|
+
const parent = path.dirname(dir);
|
|
80
|
+
if (parent === dir) return null;
|
|
81
|
+
dir = parent;
|
|
82
|
+
}
|
|
83
|
+
}
|
package/src/lib/git.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { exists } from './fs.js';
|
|
3
|
+
|
|
4
|
+
export function git(args, cwd = process.cwd(), { allowFail = false } = {}) {
|
|
5
|
+
try {
|
|
6
|
+
return execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
|
|
7
|
+
} catch (err) {
|
|
8
|
+
if (allowFail) return null;
|
|
9
|
+
const detail = err.stderr ? String(err.stderr).trim() : err.message;
|
|
10
|
+
throw Object.assign(new Error(detail), { code: 'GIT_ERROR', exitCode: 1 });
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function isGitRepo(dir) {
|
|
15
|
+
return exists(`${dir}/.git`) && git(['rev-parse', '--is-inside-work-tree'], dir, { allowFail: true }) === 'true';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function headSha(cwd) {
|
|
19
|
+
return git(['rev-parse', 'HEAD'], cwd, { allowFail: true });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function currentBranch(cwd) {
|
|
23
|
+
return git(['rev-parse', '--abbrev-ref', 'HEAD'], cwd, { allowFail: true });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function commitAll(cwd, message) {
|
|
27
|
+
git(['add', '-A'], cwd);
|
|
28
|
+
git(
|
|
29
|
+
['-c', 'user.name=asdlc-bot', '-c', 'user.email=asdlc-bot@example.invalid', 'commit', '-m', message],
|
|
30
|
+
cwd,
|
|
31
|
+
);
|
|
32
|
+
return headSha(cwd);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function tag(cwd, name, { message } = {}) {
|
|
36
|
+
if (message) git(['tag', '-a', name, '-m', message], cwd);
|
|
37
|
+
else git(['tag', name], cwd);
|
|
38
|
+
return name;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// TRUE only when `sha` is an ancestor of `ref` (i.e. actually merged into ref).
|
|
42
|
+
export function isAncestor(cwd, sha, ref) {
|
|
43
|
+
try {
|
|
44
|
+
execFileSync('git', ['merge-base', '--is-ancestor', sha, ref], { cwd, stdio: 'ignore' });
|
|
45
|
+
return true;
|
|
46
|
+
} catch {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function commitInfo(cwd, sha) {
|
|
52
|
+
const out = git(['log', '-1', '--format=%an%x09%ae%x09%s', sha], cwd, { allowFail: true });
|
|
53
|
+
if (!out) return null;
|
|
54
|
+
const [name, email, subject] = out.split('\t');
|
|
55
|
+
return { name, email, subject };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// TRUE when `relPath` is committed (tracked) in this repo. Used to distinguish a
|
|
59
|
+
// committed schema copy (an override) from a CI-materialized working-tree copy.
|
|
60
|
+
export function isPathTracked(cwd, relPath) {
|
|
61
|
+
const out = git(['ls-files', '--', relPath], cwd, { allowFail: true });
|
|
62
|
+
return Boolean(out && out.trim());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// The last commit on `ref` that touched any of `paths` - this IS the "approved
|
|
66
|
+
// commit" (v6 4): path-limited, so review.md/tasks.md commits never move it.
|
|
67
|
+
export function lastCommitTouching(cwd, ref, paths = []) {
|
|
68
|
+
if (!paths.length) return null;
|
|
69
|
+
return git(['log', '-1', '--format=%H', ref, '--', ...paths], cwd, { allowFail: true }) || null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// TRUE when the change dir already exists on `ref` (so this is a revision/impl,
|
|
73
|
+
// not a first spec PR).
|
|
74
|
+
export function changeOnRef(cwd, ref, slug) {
|
|
75
|
+
return Boolean(git(['log', '-1', '--format=%H', ref, '--', `openspec/changes/${slug}/proposal.md`], cwd, { allowFail: true }));
|
|
76
|
+
}
|