cliewen-openspec 0.0.0-stage → 0.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/CHANGELOG.md +69 -0
- package/LICENSE +21 -0
- package/README.md +101 -2
- package/adapters/node-test.mjs +65 -0
- package/dist/ac.d.ts +16 -0
- package/dist/ac.js +124 -0
- package/dist/all.d.ts +1 -0
- package/dist/all.js +28 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +81 -0
- package/dist/core.d.ts +55 -0
- package/dist/core.js +651 -0
- package/dist/delivery.d.ts +1 -0
- package/dist/delivery.js +21 -0
- package/dist/discovery.d.ts +1 -0
- package/dist/discovery.js +15 -0
- package/dist/quality.d.ts +27 -0
- package/dist/quality.js +412 -0
- package/dist/schema-source.d.ts +3 -0
- package/dist/schema-source.js +9 -0
- package/docs/configuration.md +278 -0
- package/docs/testing/evidence.md +88 -0
- package/docs/testing/pilot-prompt.md +20 -0
- package/docs/testing/test-quality.md +84 -0
- package/package.json +63 -4
- package/schemas/README.md +25 -0
- package/schemas/all/instructions/apply.md +4 -0
- package/schemas/all/instructions/approval.md +4 -0
- package/schemas/all/instructions/decision.md +7 -0
- package/schemas/all/instructions/investigation.md +1 -0
- package/schemas/all/instructions/proposal.md +3 -0
- package/schemas/all/instructions/specs.md +2 -0
- package/schemas/all/instructions/tasks.md +2 -0
- package/schemas/all/templates/decision.md +20 -0
- package/schemas/all/templates/proposal.md +19 -0
- package/schemas/all/templates/tasks.md +9 -0
- package/schemas/base/instructions/diagrams.md +7 -0
- package/schemas/base/instructions/humanizer.md +7 -0
- package/schemas/base/instructions/learnings.md +6 -0
- package/schemas/base/instructions/routing.md +5 -0
- package/schemas/base/instructions/testing-approach.md +13 -0
- package/schemas/base/instructions/writing.md +24 -0
- package/schemas/base/templates/decision.md +18 -0
- package/schemas/base/templates/learning.md +19 -0
- package/schemas/delivery/instructions/apply.md +10 -0
- package/schemas/delivery/instructions/design.md +9 -0
- package/schemas/delivery/instructions/proposal.md +3 -0
- package/schemas/delivery/instructions/specs.md +11 -0
- package/schemas/delivery/instructions/tasks.md +13 -0
- package/schemas/delivery/templates/design.md +9 -0
- package/schemas/delivery/templates/proposal.md +13 -0
- package/schemas/delivery/templates/spec.md +13 -0
- package/schemas/delivery/templates/tasks.md +17 -0
- package/schemas/discovery/instructions/apply.md +5 -0
- package/schemas/discovery/instructions/decision.md +7 -0
- package/schemas/discovery/instructions/investigation.md +8 -0
- package/schemas/discovery/instructions/proposal.md +4 -0
- package/schemas/discovery/templates/decision.md +17 -0
- package/schemas/discovery/templates/investigation.md +11 -0
- package/schemas/discovery/templates/proposal.md +9 -0
package/dist/core.js
ADDED
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { execFileSync } from 'node:child_process';
|
|
5
|
+
import { createRequire } from 'node:module';
|
|
6
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
7
|
+
import { parse, parseDocument, stringify, isMap } from 'yaml';
|
|
8
|
+
import { deliveryFiles } from './delivery.js';
|
|
9
|
+
import { discoveryFiles } from './discovery.js';
|
|
10
|
+
import { allFiles } from './all.js';
|
|
11
|
+
import { template } from './schema-source.js';
|
|
12
|
+
import { validateRegistry } from './ac.js';
|
|
13
|
+
import { qualityDefaults, configureQuality, checkQuality } from './quality.js';
|
|
14
|
+
export { nextId, renamePrefix, capabilityForPrefix } from './ac.js';
|
|
15
|
+
export const version = '0.1.0';
|
|
16
|
+
const state = '.cliewen-openspec';
|
|
17
|
+
const features = ['documentation', 'decisions', 'mermaid', 'planning', 'review', 'evidence'];
|
|
18
|
+
export function local(root, relative) {
|
|
19
|
+
if (!relative || path.isAbsolute(relative) || path.win32.parse(relative).root || relative.split(/[\\/]/).includes('..'))
|
|
20
|
+
throw Error(`Unsafe repository path: ${relative}`);
|
|
21
|
+
const target = path.resolve(root, relative);
|
|
22
|
+
const within = path.relative(path.resolve(root), target);
|
|
23
|
+
if (path.isAbsolute(within) || within === '..' || within.startsWith(`..${path.sep}`))
|
|
24
|
+
throw Error(`Unsafe repository path: ${relative}`);
|
|
25
|
+
let cursor = root;
|
|
26
|
+
for (const part of path.relative(root, target).split(path.sep)) {
|
|
27
|
+
cursor = path.join(cursor, part);
|
|
28
|
+
let entry;
|
|
29
|
+
try {
|
|
30
|
+
entry = fs.lstatSync(cursor);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (error.code !== 'ENOENT')
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
if (entry?.isSymbolicLink())
|
|
37
|
+
throw Error(`Symlink not supported: ${relative}`);
|
|
38
|
+
}
|
|
39
|
+
return target;
|
|
40
|
+
}
|
|
41
|
+
// Owned Markdown/YAML may acquire CRLF when Git checks them out on Windows.
|
|
42
|
+
const normalize = (s) => s.replace(/\r\n/g, '\n');
|
|
43
|
+
const hash = (s) => createHash('sha256').update(normalize(s)).digest('hex');
|
|
44
|
+
const read = (p) => fs.readFileSync(p, 'utf8');
|
|
45
|
+
export function defaults() {
|
|
46
|
+
return { docsRoot: 'docs', learnings: { enabled: false, directory: 'learnings' }, writing: { humanizer: 'auto' }, features: Object.fromEntries(features.map(f => [f, true])), evidence: { report: '.cliewen-openspec/evidence.json' }, ac: { capabilities: {}, padding: 3 }, testing: { ...qualityDefaults(), placement: 'per-task', types: {
|
|
47
|
+
Unit: { description: 'Isolated behavior without external dependencies' },
|
|
48
|
+
Integration: { description: 'Interaction between components or dependencies' },
|
|
49
|
+
E2E: { description: 'Complete user or system workflow' }
|
|
50
|
+
} } };
|
|
51
|
+
}
|
|
52
|
+
function keys(o, allowed, label) {
|
|
53
|
+
if (!o || typeof o !== 'object' || Array.isArray(o))
|
|
54
|
+
throw Error(`${label} must be an object`);
|
|
55
|
+
for (const key of Object.keys(o))
|
|
56
|
+
if (!allowed.includes(key))
|
|
57
|
+
throw Error(`Unknown ${label} key: ${key}`);
|
|
58
|
+
}
|
|
59
|
+
export function config(root) {
|
|
60
|
+
const p = local(root, `${state}/config.yaml`);
|
|
61
|
+
const raw = parse(read(p));
|
|
62
|
+
keys(raw, ['docsRoot', 'learnings', 'writing', 'features', 'evidence', 'ac', 'testing'], 'config');
|
|
63
|
+
const c = defaults();
|
|
64
|
+
if (raw.learnings !== undefined) {
|
|
65
|
+
keys(raw.learnings, ['enabled', 'directory'], 'learnings');
|
|
66
|
+
if (raw.learnings.enabled !== undefined) {
|
|
67
|
+
if (typeof raw.learnings.enabled !== 'boolean')
|
|
68
|
+
throw Error('learnings.enabled must be boolean');
|
|
69
|
+
c.learnings.enabled = raw.learnings.enabled;
|
|
70
|
+
}
|
|
71
|
+
if (raw.learnings.directory !== undefined) {
|
|
72
|
+
const directory = raw.learnings.directory;
|
|
73
|
+
if (typeof directory !== 'string' || !directory.trim() || directory !== directory.trim())
|
|
74
|
+
throw Error('learnings.directory must be a nonempty trimmed relative path');
|
|
75
|
+
if (path.isAbsolute(directory) || path.win32.parse(directory).root || directory.split(/[\\/]/).includes('..'))
|
|
76
|
+
throw Error('learnings.directory must stay within docsRoot');
|
|
77
|
+
c.learnings.directory = directory;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (raw.writing !== undefined) {
|
|
81
|
+
keys(raw.writing, ['humanizer'], 'writing');
|
|
82
|
+
if (raw.writing.humanizer !== undefined) {
|
|
83
|
+
if (!['auto', 'on', 'off'].includes(raw.writing.humanizer))
|
|
84
|
+
throw Error('writing.humanizer must be auto, on, or off');
|
|
85
|
+
c.writing.humanizer = raw.writing.humanizer;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (raw.ac !== undefined) {
|
|
89
|
+
keys(raw.ac, ['capabilities', 'padding'], 'ac');
|
|
90
|
+
if (raw.ac.capabilities !== undefined) {
|
|
91
|
+
if (!raw.ac.capabilities || typeof raw.ac.capabilities !== 'object' || Array.isArray(raw.ac.capabilities))
|
|
92
|
+
throw Error('ac.capabilities must be a capability-to-prefix mapping');
|
|
93
|
+
const prefixes = new Set();
|
|
94
|
+
for (const [name, prefix] of Object.entries(raw.ac.capabilities)) {
|
|
95
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name) || typeof prefix !== 'string' || !/^[A-Z][A-Z0-9]*$/.test(prefix))
|
|
96
|
+
throw Error('AC mappings require kebab-case capabilities and uppercase letter/digit prefixes');
|
|
97
|
+
if (prefixes.has(prefix))
|
|
98
|
+
throw Error(`AC prefix already assigned: ${prefix}`);
|
|
99
|
+
prefixes.add(prefix);
|
|
100
|
+
}
|
|
101
|
+
c.ac.capabilities = raw.ac.capabilities;
|
|
102
|
+
}
|
|
103
|
+
if (raw.ac.padding !== undefined) {
|
|
104
|
+
if (!Number.isInteger(raw.ac.padding) || raw.ac.padding < 1 || raw.ac.padding > 12)
|
|
105
|
+
throw Error('ac.padding must be an integer from 1 to 12');
|
|
106
|
+
c.ac.padding = raw.ac.padding;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (raw.testing !== undefined) {
|
|
110
|
+
if (Object.hasOwn(raw.testing, 'approach'))
|
|
111
|
+
throw Error('testing.approach has been retired. Remove it from .cliewen-openspec/config.yaml; use evidence-first verification. See docs/configuration.md#evidence-first-verification.');
|
|
112
|
+
keys(raw.testing, ['placement', 'types', 'quality', 'lifecycle'], 'testing');
|
|
113
|
+
configureQuality(c.testing, raw.testing);
|
|
114
|
+
if (raw.testing.types !== undefined) {
|
|
115
|
+
const types = raw.testing.types;
|
|
116
|
+
if (!types || typeof types !== 'object' || Array.isArray(types) || !Object.keys(types).length)
|
|
117
|
+
throw Error('testing.types must be a nonempty name-to-description mapping');
|
|
118
|
+
for (const [name, definition] of Object.entries(types)) {
|
|
119
|
+
if (!name.trim() || name !== name.trim() || /[\r\n\[\]]/.test(name))
|
|
120
|
+
throw Error('Test type names must be nonempty, trimmed, single-line names without brackets');
|
|
121
|
+
keys(definition, ['description'], `testing.types.${name}`);
|
|
122
|
+
if (typeof definition.description !== 'string' || !definition.description.trim())
|
|
123
|
+
throw Error(`testing.types.${name}.description must be a nonempty string`);
|
|
124
|
+
}
|
|
125
|
+
c.testing.types = types;
|
|
126
|
+
}
|
|
127
|
+
if (raw.testing.placement !== undefined) {
|
|
128
|
+
if (!['per-task', 'end-of-change'].includes(raw.testing.placement))
|
|
129
|
+
throw Error('testing.placement must be per-task or end-of-change');
|
|
130
|
+
c.testing.placement = raw.testing.placement;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (raw.docsRoot !== undefined) {
|
|
134
|
+
if (typeof raw.docsRoot !== 'string')
|
|
135
|
+
throw Error('docsRoot must be a string');
|
|
136
|
+
c.docsRoot = raw.docsRoot;
|
|
137
|
+
}
|
|
138
|
+
if (raw.features !== undefined) {
|
|
139
|
+
keys(raw.features, features, 'features');
|
|
140
|
+
for (const [k, v] of Object.entries(raw.features)) {
|
|
141
|
+
if (typeof v !== 'boolean')
|
|
142
|
+
throw Error(`features.${k} must be boolean`);
|
|
143
|
+
c.features[k] = v;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (raw.evidence !== undefined) {
|
|
147
|
+
keys(raw.evidence, ['report'], 'evidence');
|
|
148
|
+
if (typeof raw.evidence.report !== 'string')
|
|
149
|
+
throw Error('evidence.report must be a string');
|
|
150
|
+
c.evidence.report = raw.evidence.report;
|
|
151
|
+
}
|
|
152
|
+
if (!c.features.evidence && [c.testing.quality.coverage.mode, c.testing.quality.effectiveness.mode, c.testing.lifecycle.mode].includes('enforce'))
|
|
153
|
+
throw Error('Test policy enforcement requires features.evidence: true; enable evidence or use review/off mode.');
|
|
154
|
+
local(root, c.docsRoot);
|
|
155
|
+
if (c.learnings.enabled && c.features.documentation)
|
|
156
|
+
local(root, `${c.docsRoot}/${c.learnings.directory}`);
|
|
157
|
+
local(root, c.evidence.report);
|
|
158
|
+
return c;
|
|
159
|
+
}
|
|
160
|
+
export function transaction(root, writes, dryRun = false) {
|
|
161
|
+
const jp = local(root, `${state}/transaction.json`);
|
|
162
|
+
if (fs.existsSync(jp))
|
|
163
|
+
throw Error('Interrupted transaction exists; run recover before writing.');
|
|
164
|
+
for (const p of Object.keys(writes))
|
|
165
|
+
local(root, p);
|
|
166
|
+
if (dryRun || !Object.keys(writes).length)
|
|
167
|
+
return Object.keys(writes);
|
|
168
|
+
const journal = { files: Object.fromEntries(Object.keys(writes).map(p => [p, fs.existsSync(local(root, p)) ? read(local(root, p)) : null])) };
|
|
169
|
+
fs.mkdirSync(path.dirname(jp), { recursive: true });
|
|
170
|
+
fs.writeFileSync(jp, JSON.stringify(journal), { flag: 'wx' });
|
|
171
|
+
try {
|
|
172
|
+
for (const [p, content] of Object.entries(writes)) {
|
|
173
|
+
const dest = local(root, p);
|
|
174
|
+
if (content === null) {
|
|
175
|
+
if (fs.existsSync(dest))
|
|
176
|
+
fs.unlinkSync(dest);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
180
|
+
fs.writeFileSync(dest, content);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
fs.unlinkSync(jp);
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
recover(root);
|
|
187
|
+
throw error;
|
|
188
|
+
}
|
|
189
|
+
return Object.keys(writes);
|
|
190
|
+
}
|
|
191
|
+
export function recover(root) {
|
|
192
|
+
const jp = local(root, `${state}/transaction.json`);
|
|
193
|
+
if (!fs.existsSync(jp))
|
|
194
|
+
return;
|
|
195
|
+
const j = JSON.parse(read(jp));
|
|
196
|
+
keys(j, ['files'], 'journal');
|
|
197
|
+
if (!j.files || typeof j.files !== 'object' || Array.isArray(j.files))
|
|
198
|
+
throw Error('Invalid transaction journal');
|
|
199
|
+
for (const [p, v] of Object.entries(j.files)) {
|
|
200
|
+
local(root, p);
|
|
201
|
+
if (v !== null && typeof v !== 'string')
|
|
202
|
+
throw Error('Invalid journal content');
|
|
203
|
+
}
|
|
204
|
+
for (const [p, v] of Object.entries(j.files)) {
|
|
205
|
+
const dest = local(root, p);
|
|
206
|
+
if (v === null) {
|
|
207
|
+
if (fs.existsSync(dest))
|
|
208
|
+
fs.unlinkSync(dest);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
212
|
+
fs.writeFileSync(dest, v);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
fs.unlinkSync(jp);
|
|
216
|
+
}
|
|
217
|
+
function bundledFiles() { return { ...deliveryFiles(), ...discoveryFiles(), ...allFiles(), [`${state}/templates/learning.md`]: template('base/templates/learning.md') }; }
|
|
218
|
+
function manifestText(files) {
|
|
219
|
+
return JSON.stringify({ version, files: Object.fromEntries(Object.entries(files).map(([file, content]) => [file, hash(content)])) }, null, 2) + '\n';
|
|
220
|
+
}
|
|
221
|
+
function ownedManifest(root, operation) {
|
|
222
|
+
const mp = local(root, `${state}/manifest.json`);
|
|
223
|
+
if (!fs.existsSync(mp))
|
|
224
|
+
throw Error(operation === 'update'
|
|
225
|
+
? 'No Cliewen installation manifest; run init first. Update does not adopt unowned files.'
|
|
226
|
+
: 'No Cliewen installation manifest; no files removed. Uninstall requires recorded ownership.');
|
|
227
|
+
const previous = JSON.parse(read(mp));
|
|
228
|
+
keys(previous, ['version', 'files'], 'manifest');
|
|
229
|
+
if (previous.version !== version)
|
|
230
|
+
throw Error(`Unsupported installed version: ${previous.version}; ${operation} supports ${version} manifests only. This package does not migrate other versions.`);
|
|
231
|
+
if (!previous.files || typeof previous.files !== 'object' || Array.isArray(previous.files) || !Object.keys(previous.files).length)
|
|
232
|
+
throw Error('Manifest files must be a nonempty ownership mapping');
|
|
233
|
+
for (const [file, digest] of Object.entries(previous.files)) {
|
|
234
|
+
local(root, file);
|
|
235
|
+
const owned = [`${state}/templates/decision.md`, `${state}/templates/learning.md`].includes(file) || /^openspec\/schemas\/cliewen-(delivery|discovery|all)\/(schema\.yaml|templates\/[a-z0-9][a-z0-9-]*\.md)$/.test(file);
|
|
236
|
+
if (!owned || typeof digest !== 'string' || !/^[a-f0-9]{64}$/.test(digest))
|
|
237
|
+
throw Error(`Invalid owned-file entry in manifest: ${file}`);
|
|
238
|
+
}
|
|
239
|
+
return previous;
|
|
240
|
+
}
|
|
241
|
+
function validateReplacementSchema(root, name) {
|
|
242
|
+
let entry;
|
|
243
|
+
for (const require of [createRequire(path.join(path.resolve(root), 'package.json')), createRequire(import.meta.url)]) {
|
|
244
|
+
try {
|
|
245
|
+
entry = require.resolve('@fission-ai/openspec');
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
catch { /* Try the package's own resolution context next. */ }
|
|
249
|
+
}
|
|
250
|
+
if (!entry)
|
|
251
|
+
throw Error('Install OpenSpec locally to validate --default-schema before uninstalling.');
|
|
252
|
+
let cursor = path.dirname(entry);
|
|
253
|
+
while (true) {
|
|
254
|
+
const metadata = path.join(cursor, 'package.json');
|
|
255
|
+
if (fs.existsSync(metadata)) {
|
|
256
|
+
const pkg = JSON.parse(read(metadata));
|
|
257
|
+
if (pkg.name === '@fission-ai/openspec') {
|
|
258
|
+
const bin = pkg.bin?.openspec;
|
|
259
|
+
if (typeof bin !== 'string')
|
|
260
|
+
throw Error('Installed OpenSpec does not declare its public CLI.');
|
|
261
|
+
const cli = path.resolve(cursor, bin);
|
|
262
|
+
const relative = path.relative(cursor, cli);
|
|
263
|
+
if (!relative || relative.startsWith('..') || path.isAbsolute(relative))
|
|
264
|
+
throw Error('Invalid OpenSpec CLI path');
|
|
265
|
+
try {
|
|
266
|
+
execFileSync(process.execPath, [cli, 'schema', 'validate', name, '--json'], { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], timeout: 30000, env: { ...process.env, OPENSPEC_TELEMETRY: '0', CI: '1' } });
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
throw Error(`Replacement schema failed OpenSpec validation: ${name}`);
|
|
270
|
+
}
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
const parent = path.dirname(cursor);
|
|
275
|
+
if (parent === cursor)
|
|
276
|
+
throw Error('Cannot locate the public OpenSpec CLI metadata.');
|
|
277
|
+
cursor = parent;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
export function uninstall(root, dryRun = false, replacement) {
|
|
281
|
+
if (!dryRun)
|
|
282
|
+
recover(root);
|
|
283
|
+
else if (fs.existsSync(local(root, `${state}/transaction.json`)))
|
|
284
|
+
throw Error('Interrupted transaction exists; run recover before uninstall preview.');
|
|
285
|
+
const manifest = ownedManifest(root, 'uninstall');
|
|
286
|
+
const removed = new Set(Object.keys(manifest.files).flatMap(file => file.match(/^openspec\/schemas\/([^/]+)\//)?.[1] ?? []));
|
|
287
|
+
const canonical = (name) => name.replace(/\.ya?ml$/, '').toLowerCase();
|
|
288
|
+
const cfgPath = 'openspec/config.yaml';
|
|
289
|
+
const original = fs.existsSync(local(root, cfgPath)) ? read(local(root, cfgPath)) : '';
|
|
290
|
+
const doc = parseDocument(original || '{}');
|
|
291
|
+
if (doc.errors.length)
|
|
292
|
+
throw Error(doc.errors[0].message);
|
|
293
|
+
if (doc.contents !== null && !isMap(doc.contents))
|
|
294
|
+
throw Error('OpenSpec config must be a YAML mapping');
|
|
295
|
+
const originalConfig = doc.toJS();
|
|
296
|
+
const current = originalConfig?.schema;
|
|
297
|
+
if (current !== undefined && typeof current !== 'string')
|
|
298
|
+
throw Error('OpenSpec default schema must be a string');
|
|
299
|
+
const defaultRemoved = typeof current === 'string' && removed.has(canonical(current));
|
|
300
|
+
if (defaultRemoved && !replacement)
|
|
301
|
+
throw Error(`Default schema ${current} is being removed; pass --default-schema NAME explicitly.`);
|
|
302
|
+
if (!defaultRemoved && replacement !== undefined)
|
|
303
|
+
throw Error('--default-schema is only supported when the current default uses a schema being removed.');
|
|
304
|
+
if (replacement !== undefined && (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(replacement) || removed.has(canonical(replacement))))
|
|
305
|
+
throw Error('Replacement must name a schema that is not being removed.');
|
|
306
|
+
const changes = {};
|
|
307
|
+
for (const [file, digest] of Object.entries(manifest.files)) {
|
|
308
|
+
const dest = local(root, file);
|
|
309
|
+
if (!fs.existsSync(dest))
|
|
310
|
+
continue;
|
|
311
|
+
if (hash(read(dest)) !== digest)
|
|
312
|
+
throw Error(`Conflict: ${file} has local edits; no uninstall files changed.`);
|
|
313
|
+
changes[file] = null;
|
|
314
|
+
}
|
|
315
|
+
const inspect = (folder) => {
|
|
316
|
+
const directory = local(root, folder);
|
|
317
|
+
const metadata = path.join(directory, '.openspec.yaml');
|
|
318
|
+
const hasMetadata = fs.existsSync(local(root, `${folder}/.openspec.yaml`));
|
|
319
|
+
const hasArtifacts = ['proposal.md', 'investigation.md', 'decision.md', 'design.md', 'tasks.md', 'specs'].some(file => fs.existsSync(local(root, `${folder}/${file}`)));
|
|
320
|
+
if (hasMetadata || hasArtifacts) {
|
|
321
|
+
const selected = hasMetadata ? parse(read(metadata)) : {};
|
|
322
|
+
if (!selected || typeof selected !== 'object' || Array.isArray(selected) || (selected.schema !== undefined && typeof selected.schema !== 'string'))
|
|
323
|
+
throw Error(`Cannot establish the schema for active change: ${folder}`);
|
|
324
|
+
const schema = selected.schema ?? current;
|
|
325
|
+
if (typeof schema === 'string' && removed.has(canonical(schema)))
|
|
326
|
+
throw Error(`Active change ${folder} uses ${schema}; finish/archive it or explicitly migrate it before uninstalling. No change metadata was rewritten.`);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
|
330
|
+
if (entry.isSymbolicLink())
|
|
331
|
+
throw Error(`Symlink not supported: ${folder}/${entry.name}`);
|
|
332
|
+
if (entry.isDirectory())
|
|
333
|
+
inspect(`${folder}/${entry.name}`);
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
const active = local(root, 'openspec/changes');
|
|
337
|
+
if (fs.existsSync(active))
|
|
338
|
+
for (const entry of fs.readdirSync(active, { withFileTypes: true })) {
|
|
339
|
+
if (entry.name === 'archive')
|
|
340
|
+
continue;
|
|
341
|
+
if (entry.isSymbolicLink())
|
|
342
|
+
throw Error(`Symlink not supported: openspec/changes/${entry.name}`);
|
|
343
|
+
if (entry.isDirectory())
|
|
344
|
+
inspect(`openspec/changes/${entry.name}`);
|
|
345
|
+
}
|
|
346
|
+
if (replacement !== undefined) {
|
|
347
|
+
validateReplacementSchema(root, replacement);
|
|
348
|
+
doc.set('schema', replacement);
|
|
349
|
+
const before = { ...originalConfig };
|
|
350
|
+
delete before.schema;
|
|
351
|
+
const after = { ...doc.toJS() };
|
|
352
|
+
delete after.schema;
|
|
353
|
+
if (!isDeepStrictEqual(before, after))
|
|
354
|
+
throw Error('Replacing the default would change other YAML values through shared anchors; resolve those aliases explicitly before uninstalling.');
|
|
355
|
+
// Replace only the explicitly selected default; preserve unrelated YAML values and comments.
|
|
356
|
+
changes[cfgPath] = doc.toString();
|
|
357
|
+
}
|
|
358
|
+
changes[`${state}/manifest.json`] = null;
|
|
359
|
+
transaction(root, changes, dryRun);
|
|
360
|
+
return { writes: Object.keys(changes).filter(file => changes[file] !== null), deletes: Object.keys(changes).filter(file => changes[file] === null) };
|
|
361
|
+
}
|
|
362
|
+
export function update(root, dryRun = false) {
|
|
363
|
+
if (!fs.existsSync(local(root, 'openspec/specs')) || !fs.existsSync(local(root, 'openspec/changes')))
|
|
364
|
+
throw Error('Run openspec init first (specs and changes directories are required).');
|
|
365
|
+
if (!dryRun)
|
|
366
|
+
recover(root);
|
|
367
|
+
const manifestPath = `${state}/manifest.json`;
|
|
368
|
+
const mp = local(root, manifestPath);
|
|
369
|
+
const previous = ownedManifest(root, 'update');
|
|
370
|
+
const settings = config(root);
|
|
371
|
+
validateRegistry(root, settings);
|
|
372
|
+
const files = bundledFiles();
|
|
373
|
+
const changes = {};
|
|
374
|
+
for (const [file, content] of Object.entries(files)) {
|
|
375
|
+
const dest = local(root, file);
|
|
376
|
+
const exists = fs.existsSync(dest);
|
|
377
|
+
const oldHash = previous.files[file];
|
|
378
|
+
if (exists && !oldHash)
|
|
379
|
+
throw Error(`Unowned schema file already exists: ${file}; no update files written.`);
|
|
380
|
+
if (exists) {
|
|
381
|
+
const current = read(dest);
|
|
382
|
+
if (hash(current) !== oldHash && normalize(current) !== normalize(content))
|
|
383
|
+
throw Error(`Conflict: ${file}; local edits differ from the update. No update files written.`);
|
|
384
|
+
if (normalize(current) !== normalize(content))
|
|
385
|
+
changes[file] = content;
|
|
386
|
+
}
|
|
387
|
+
else
|
|
388
|
+
changes[file] = content;
|
|
389
|
+
}
|
|
390
|
+
for (const [file, digest] of Object.entries(previous.files)) {
|
|
391
|
+
if (Object.hasOwn(files, file))
|
|
392
|
+
continue;
|
|
393
|
+
const dest = local(root, file);
|
|
394
|
+
if (fs.existsSync(dest)) {
|
|
395
|
+
if (hash(read(dest)) !== digest)
|
|
396
|
+
throw Error(`Conflict: retired owned file ${file} has local edits. No update files written.`);
|
|
397
|
+
changes[file] = null;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
// Publish new ownership only after every content change; rollback includes the old manifest.
|
|
401
|
+
const manifest = manifestText(files);
|
|
402
|
+
if (read(mp) !== manifest)
|
|
403
|
+
changes[manifestPath] = manifest;
|
|
404
|
+
transaction(root, changes, dryRun);
|
|
405
|
+
return { writes: Object.keys(changes).filter(file => changes[file] !== null), deletes: Object.keys(changes).filter(file => changes[file] === null) };
|
|
406
|
+
}
|
|
407
|
+
export function init(root, dryRun, makeDefault) {
|
|
408
|
+
if (!fs.existsSync(local(root, 'openspec/specs')) || !fs.existsSync(local(root, 'openspec/changes')))
|
|
409
|
+
throw Error('Run openspec init first (specs and changes directories are required).');
|
|
410
|
+
if (!dryRun)
|
|
411
|
+
recover(root);
|
|
412
|
+
const mp = local(root, `${state}/manifest.json`);
|
|
413
|
+
const previous = fs.existsSync(mp) ? JSON.parse(read(mp)) : undefined;
|
|
414
|
+
if (previous && previous.version !== version)
|
|
415
|
+
throw Error(`Installed version differs; init supports ${version} manifests only and does not migrate other versions.`);
|
|
416
|
+
const files = bundledFiles();
|
|
417
|
+
const writes = {};
|
|
418
|
+
for (const [p, content] of Object.entries(files)) {
|
|
419
|
+
const dest = local(root, p);
|
|
420
|
+
if (fs.existsSync(dest) && normalize(read(dest)) !== normalize(content))
|
|
421
|
+
throw Error(`Conflict: ${p}; no files written.`);
|
|
422
|
+
if (fs.existsSync(dest) && !previous?.files[p])
|
|
423
|
+
throw Error(`Unowned schema file already exists: ${p}`);
|
|
424
|
+
if (!fs.existsSync(dest))
|
|
425
|
+
writes[p] = content;
|
|
426
|
+
}
|
|
427
|
+
const cp = `${state}/config.yaml`;
|
|
428
|
+
if (!fs.existsSync(local(root, cp)))
|
|
429
|
+
writes[cp] = stringify(defaults());
|
|
430
|
+
else
|
|
431
|
+
config(root);
|
|
432
|
+
// Authored docs/config are never included in the owned-file manifest.
|
|
433
|
+
const docsRoot = fs.existsSync(local(root, cp)) ? config(root).docsRoot : 'docs';
|
|
434
|
+
const entry = `${docsRoot}/README.md`;
|
|
435
|
+
if (!fs.existsSync(local(root, entry)))
|
|
436
|
+
writes[entry] = '---\ntype: overview\n---\n# Project documentation\n\nKeep architecture/, design/, decisions/, testing/ and optional plans/ here as needed. Link to OpenSpec specs for behavior. Do not invent project intent.\n';
|
|
437
|
+
const ignore = `${state}/.gitignore`;
|
|
438
|
+
if (!fs.existsSync(local(root, ignore)))
|
|
439
|
+
writes[ignore] = 'evidence.json\ntransaction.json\nreview-output/\n';
|
|
440
|
+
const cfgPath = 'openspec/config.yaml';
|
|
441
|
+
const original = fs.existsSync(local(root, cfgPath)) ? read(local(root, cfgPath)) : '';
|
|
442
|
+
if (makeDefault) {
|
|
443
|
+
const doc = parseDocument(original || '{}');
|
|
444
|
+
if (doc.errors.length)
|
|
445
|
+
throw Error(doc.errors[0].message);
|
|
446
|
+
doc.set('schema', 'cliewen-delivery');
|
|
447
|
+
const next = doc.toString();
|
|
448
|
+
if (next !== original)
|
|
449
|
+
writes[cfgPath] = next;
|
|
450
|
+
}
|
|
451
|
+
const manifest = manifestText(files);
|
|
452
|
+
if (!fs.existsSync(mp) || read(mp) !== manifest)
|
|
453
|
+
writes[`${state}/manifest.json`] = manifest;
|
|
454
|
+
return transaction(root, writes, dryRun);
|
|
455
|
+
}
|
|
456
|
+
export function markdowns(dir) {
|
|
457
|
+
if (!fs.existsSync(dir))
|
|
458
|
+
return [];
|
|
459
|
+
return fs.readdirSync(dir, { withFileTypes: true }).flatMap(e => e.isDirectory() ? markdowns(path.join(dir, e.name)) : e.isFile() && e.name.endsWith('.md') ? [path.join(dir, e.name)] : []);
|
|
460
|
+
}
|
|
461
|
+
export function scenarios(root, change) {
|
|
462
|
+
if (change && !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(change))
|
|
463
|
+
throw Error('Change must be a kebab-case name');
|
|
464
|
+
let folder = change ? local(root, `openspec/changes/${change}/specs`) : local(root, 'openspec/specs');
|
|
465
|
+
const metadata = change ? local(root, `openspec/changes/${change}/.openspec.yaml`) : undefined;
|
|
466
|
+
if (metadata && fs.existsSync(metadata) && parse(read(metadata))?.skip_specs === true) {
|
|
467
|
+
if (markdowns(folder).length)
|
|
468
|
+
throw Error('skip_specs changes must not contain product spec deltas');
|
|
469
|
+
folder = local(root, 'openspec/specs');
|
|
470
|
+
}
|
|
471
|
+
if (change && !fs.existsSync(folder))
|
|
472
|
+
throw Error(`Change specs do not exist: ${change}`);
|
|
473
|
+
const out = [];
|
|
474
|
+
for (const file of markdowns(folder)) {
|
|
475
|
+
let fence = false, removed = false;
|
|
476
|
+
let current;
|
|
477
|
+
for (const line of read(file).split(/\r?\n/)) {
|
|
478
|
+
if (/^\s*(```|~~~)/.test(line)) {
|
|
479
|
+
fence = !fence;
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (fence)
|
|
483
|
+
continue;
|
|
484
|
+
if (/^#{1,4} /.test(line))
|
|
485
|
+
current = undefined;
|
|
486
|
+
if (/^## /.test(line))
|
|
487
|
+
removed = /^## REMOVED Requirements/.test(line);
|
|
488
|
+
if (!removed && /^#### Scenario:/.test(line)) {
|
|
489
|
+
const m = line.match(/\[([A-Z][A-Z0-9]*-\d+)\]\s*$/);
|
|
490
|
+
current = { id: m?.[1] ?? '', title: line, file: path.relative(root, file), testTypes: [] };
|
|
491
|
+
out.push(current);
|
|
492
|
+
}
|
|
493
|
+
const type = line.match(/^Test-type:\s*(.*?)\s*$/);
|
|
494
|
+
if (current && type)
|
|
495
|
+
current.testTypes.push(type[1]);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
return out;
|
|
499
|
+
}
|
|
500
|
+
export function git(root, args) { return execFileSync('git', ['-C', root, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim(); }
|
|
501
|
+
export function check(root, change, failPending = false) {
|
|
502
|
+
const findings = [];
|
|
503
|
+
const add = (code, message, severity = 'error') => findings.push({ code, severity, message });
|
|
504
|
+
const c = config(root);
|
|
505
|
+
if (fs.existsSync(local(root, `${state}/transaction.json`)))
|
|
506
|
+
add('interrupted-transaction', 'Run recover before continuing');
|
|
507
|
+
const mp = local(root, `${state}/manifest.json`);
|
|
508
|
+
if (!fs.existsSync(mp))
|
|
509
|
+
add('not-installed', 'Run init');
|
|
510
|
+
else {
|
|
511
|
+
const m = JSON.parse(read(mp));
|
|
512
|
+
if (m.version !== version)
|
|
513
|
+
add('unsupported-installed-version', `Installed manifest ${m.version}; this package supports ${version} only. No automatic migration is available.`);
|
|
514
|
+
for (const [p, h] of Object.entries(m.files))
|
|
515
|
+
if (!fs.existsSync(local(root, p)) || hash(read(local(root, p))) !== h)
|
|
516
|
+
add('owned-file-modified', p);
|
|
517
|
+
}
|
|
518
|
+
try {
|
|
519
|
+
validateRegistry(root, c);
|
|
520
|
+
}
|
|
521
|
+
catch (e) {
|
|
522
|
+
add('invalid-ac-registry', e instanceof Error ? e.message : String(e));
|
|
523
|
+
}
|
|
524
|
+
if (change && /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(change)) {
|
|
525
|
+
const metadata = local(root, `openspec/changes/${change}/.openspec.yaml`);
|
|
526
|
+
const selection = fs.existsSync(metadata) ? parse(read(metadata)) : undefined;
|
|
527
|
+
if (selection?.schema === 'cliewen-discovery') {
|
|
528
|
+
if (c.features.evidence)
|
|
529
|
+
add('discovery-review-required', 'Installation and registry checks only: review investigation sources, experiment outcomes, uncertainty, and the decision. Discovery findings do not prove product behavior.', 'info');
|
|
530
|
+
else
|
|
531
|
+
add('evidence-disabled', 'Evidence was explicitly disabled', 'info');
|
|
532
|
+
return findings;
|
|
533
|
+
}
|
|
534
|
+
if (selection?.schema === 'cliewen-all') {
|
|
535
|
+
const decisionPath = local(root, `openspec/changes/${change}/decision.md`);
|
|
536
|
+
const frontmatter = fs.existsSync(decisionPath) ? read(decisionPath).match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/) : undefined;
|
|
537
|
+
const decision = frontmatter ? parse(frontmatter[1]) : undefined;
|
|
538
|
+
if (!decision || decision.type !== 'decision' || !['delivery', 'no-change', 'further-investigation'].includes(decision.outcome) || !['proposed', 'accepted'].includes(decision.status)) {
|
|
539
|
+
add('all-decision-required', 'decision.md needs type: decision, status: proposed or accepted, and outcome: delivery, no-change, or further-investigation');
|
|
540
|
+
return findings;
|
|
541
|
+
}
|
|
542
|
+
if (decision.outcome !== 'delivery') {
|
|
543
|
+
if (selection.skip_specs !== true)
|
|
544
|
+
add('all-outcome-conflict', 'A no-delivery outcome requires skip_specs: true in .openspec.yaml');
|
|
545
|
+
if (markdowns(local(root, `openspec/changes/${change}/specs`)).length)
|
|
546
|
+
add('all-outcome-conflict', 'A no-delivery outcome must not contain product spec deltas');
|
|
547
|
+
add('discovery-review-required', 'No-delivery outcome: review investigation evidence and recommendation. Product execution evidence is not assessed.', 'info');
|
|
548
|
+
return findings;
|
|
549
|
+
}
|
|
550
|
+
if (selection.skip_specs === true) {
|
|
551
|
+
add('all-outcome-conflict', 'Delivery must not skip product specs');
|
|
552
|
+
return findings;
|
|
553
|
+
}
|
|
554
|
+
if (decision.status !== 'accepted') {
|
|
555
|
+
add('all-decision-unaccepted', 'Delivery requires an accepted decision following human agreement; artifact completion is not approval');
|
|
556
|
+
return findings;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
if (!c.features.evidence) {
|
|
561
|
+
add('evidence-disabled', 'Evidence was explicitly disabled', 'info');
|
|
562
|
+
if ([c.testing.quality.coverage.mode, c.testing.quality.effectiveness.mode, c.testing.lifecycle.mode].includes('review'))
|
|
563
|
+
add('quality-evidence-unavailable', 'Test policy review remains advisory: enable evidence for execution-backed assessment.', 'warning');
|
|
564
|
+
return findings;
|
|
565
|
+
}
|
|
566
|
+
const specs = scenarios(root, change);
|
|
567
|
+
const ids = new Set();
|
|
568
|
+
for (const s of specs) {
|
|
569
|
+
if (!s.id)
|
|
570
|
+
add('missing-ac-id', `${s.file}: ${s.title}`);
|
|
571
|
+
else if (ids.has(s.id))
|
|
572
|
+
add('duplicate-ac-id', s.id);
|
|
573
|
+
else
|
|
574
|
+
ids.add(s.id);
|
|
575
|
+
}
|
|
576
|
+
for (const s of specs) {
|
|
577
|
+
if (s.testTypes.length !== 1 || !s.testTypes[0])
|
|
578
|
+
add('invalid-scenario-test-type', `${s.file}: ${s.id || s.title} requires exactly one nonempty Test-type line`);
|
|
579
|
+
for (const type of s.testTypes)
|
|
580
|
+
if (!Object.hasOwn(c.testing.types, type))
|
|
581
|
+
add('unknown-test-type', `${s.id}: ${type}`);
|
|
582
|
+
}
|
|
583
|
+
const reportPath = local(root, c.evidence.report);
|
|
584
|
+
if (!fs.existsSync(reportPath)) {
|
|
585
|
+
add('evidence-not-configured', `No report at ${c.evidence.report}`);
|
|
586
|
+
return findings;
|
|
587
|
+
}
|
|
588
|
+
const report = JSON.parse(read(reportPath));
|
|
589
|
+
if (!report || report.version !== 1 || typeof report.revision !== 'string' || !/^[a-f0-9]{40,64}$/.test(report.revision) || !Array.isArray(report.results))
|
|
590
|
+
throw Error('Invalid evidence report: expected version 1, full revision and results');
|
|
591
|
+
if (report.scope !== undefined) {
|
|
592
|
+
keys(report.scope, ['change'], 'evidence scope');
|
|
593
|
+
if (typeof report.scope.change !== 'string' || !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(report.scope.change))
|
|
594
|
+
throw Error('Invalid evidence scope: expected a kebab-case change name');
|
|
595
|
+
if (report.scope.change !== change)
|
|
596
|
+
add('evidence-scope-mismatch', `Report is scoped to ${report.scope.change}; check ${change ? `requests ${change}` : 'requests permanent specs'}`);
|
|
597
|
+
if (report.execution === undefined)
|
|
598
|
+
throw Error('Scoped evidence requires the complete test runner exit code');
|
|
599
|
+
}
|
|
600
|
+
if (report.execution !== undefined && (!report.execution || !Number.isInteger(report.execution.exitCode)))
|
|
601
|
+
throw Error('Invalid evidence execution result');
|
|
602
|
+
if (report.execution?.exitCode !== undefined && report.execution.exitCode !== 0)
|
|
603
|
+
add('runner-failed', `Test runner exited ${report.execution.exitCode}`);
|
|
604
|
+
try {
|
|
605
|
+
if (git(root, ['rev-parse', 'HEAD']) !== report.revision)
|
|
606
|
+
add('stale-evidence', 'Report revision differs from HEAD');
|
|
607
|
+
if (git(root, ['status', '--porcelain', '--untracked-files=all']))
|
|
608
|
+
add('unverifiable-evidence', 'Working tree is not clean');
|
|
609
|
+
}
|
|
610
|
+
catch {
|
|
611
|
+
add('unverifiable-evidence', 'Cannot establish Git revision');
|
|
612
|
+
}
|
|
613
|
+
const seen = new Set();
|
|
614
|
+
const tests = new Set();
|
|
615
|
+
for (const r of report.results) {
|
|
616
|
+
if (!r || typeof r.scenario !== 'string' || typeof r.test !== 'string' || !r.test || !['passed', 'failed', 'skipped', 'pending', 'manual'].includes(r.outcome))
|
|
617
|
+
throw Error('Invalid evidence result');
|
|
618
|
+
const key = `${r.scenario}:${r.test}`;
|
|
619
|
+
if (tests.has(key))
|
|
620
|
+
add('duplicate-test-result', key);
|
|
621
|
+
tests.add(key);
|
|
622
|
+
if (!ids.has(r.scenario))
|
|
623
|
+
add('unknown-ac-reference', r.scenario);
|
|
624
|
+
else
|
|
625
|
+
seen.add(r.scenario);
|
|
626
|
+
if (typeof r.testType !== 'string' || !r.testType.trim())
|
|
627
|
+
add('missing-test-type', key);
|
|
628
|
+
else if (!Object.hasOwn(c.testing.types, r.testType))
|
|
629
|
+
add('unknown-test-type', `${key}: ${r.testType}`);
|
|
630
|
+
else {
|
|
631
|
+
const scenario = specs.find(s => s.id === r.scenario);
|
|
632
|
+
if (scenario?.testTypes.length === 1 && scenario.testTypes[0] !== r.testType)
|
|
633
|
+
add('test-type-mismatch', `${key}: expected ${scenario.testTypes[0]}, received ${r.testType}`);
|
|
634
|
+
}
|
|
635
|
+
if (r.outcome === 'failed' || r.outcome === 'skipped')
|
|
636
|
+
add(`test-${r.outcome}`, key);
|
|
637
|
+
if (r.outcome === 'pending') {
|
|
638
|
+
if (typeof r.reason !== 'string' || !r.reason.trim())
|
|
639
|
+
add('pending-without-reason', key);
|
|
640
|
+
add('pending', `${key}: ${r.reason ?? ''}`, failPending ? 'error' : 'warning');
|
|
641
|
+
}
|
|
642
|
+
if (r.outcome === 'manual')
|
|
643
|
+
add('manual-verification', `${key}: ${r.reason ?? 'No review result recorded'}`, 'warning');
|
|
644
|
+
}
|
|
645
|
+
for (const id of ids)
|
|
646
|
+
if (!seen.has(id))
|
|
647
|
+
add('missing-evidence', id);
|
|
648
|
+
findings.push(...checkQuality(root, c, specs, report, change, local));
|
|
649
|
+
add('sufficiency-not-assessed', 'Execution and linkage do not prove assertion quality; review the assertions against the scenarios.', 'info');
|
|
650
|
+
return findings;
|
|
651
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function deliveryFiles(): Record<string, string>;
|