check-openspec-archive-ready.mjs
4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env node
import { existsSync, readFileSync } from 'node:fs';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
const args = process.argv.slice(2);
function readFlag(name) {
const index = args.indexOf(name);
if (index === -1) {
return undefined;
}
return args[index + 1];
}
function fail(message) {
console.error(`Archive gate failed: ${message}`);
process.exit(1);
}
function info(message) {
console.log(`Archive gate: ${message}`);
}
const projectArg = readFlag('--project');
const stateArg = readFlag('--state');
const changeArg = readFlag('--change');
const repoRoot = path.resolve(import.meta.dirname, '..');
if (!projectArg && !stateArg) {
fail('pass --project workspace/<project> or --state path/to/.workflow-state.json');
}
const statePath = path.resolve(repoRoot, stateArg ?? path.join(projectArg, '.workflow-state.json'));
if (!existsSync(statePath)) {
fail(`state file not found: ${path.relative(repoRoot, statePath)}`);
}
let state;
try {
state = JSON.parse(readFileSync(statePath, 'utf8'));
} catch (error) {
fail(`state file is not valid JSON: ${error.message}`);
}
const project = projectArg ?? state.project;
const change = changeArg ?? state.change;
if (!project) {
fail('state.project is missing; pass --project explicitly');
}
if (!change) {
fail('state.change is missing; pass --change explicitly');
}
if (changeArg && state.change && changeArg !== state.change) {
fail(`--change ${changeArg} does not match state.change ${state.change}`);
}
if (state.stage !== 'READY_TO_ARCHIVE') {
fail(`stage must be READY_TO_ARCHIVE, got ${state.stage ?? 'missing'}`);
}
const gates = state.gates ?? {};
if (gates.review_completed !== true) {
fail('gates.review_completed must be true');
}
if (gates.validation_completed !== true) {
fail('gates.validation_completed must be true');
}
if (gates.openspec_locked !== true) {
fail('gates.openspec_locked must be true');
}
if (state.last_validation?.result !== 'PASS') {
fail('last_validation.result must be PASS');
}
if (!Array.isArray(state.last_validation?.commands) || state.last_validation.commands.length === 0) {
fail('last_validation.commands must record the fresh verification commands');
}
const projectCwd = path.resolve(repoRoot, project);
if (!existsSync(projectCwd)) {
fail(`project directory not found: ${project}`);
}
const changeDir = path.join(projectCwd, 'openspec', 'changes', change);
if (!existsSync(changeDir)) {
fail(`OpenSpec change directory not found: ${path.relative(repoRoot, changeDir)}`);
}
function commandBin() {
const rtk = spawnSync('rtk', ['--version'], { cwd: repoRoot, encoding: 'utf8' });
return rtk.error ? 'openspec' : 'rtk';
}
function runOpenSpec(subArgs) {
const bin = commandBin();
const commandArgs = bin === 'rtk' ? ['openspec', ...subArgs] : subArgs;
const result = spawnSync(bin, commandArgs, {
cwd: projectCwd,
encoding: 'utf8',
});
if (result.error) {
fail(`failed to run ${bin}: ${result.error.message}`);
}
if (result.status !== 0) {
const output = `${result.stdout ?? ''}${result.stderr ?? ''}`.trim();
fail(`${bin} ${commandArgs.join(' ')} exited ${result.status}${output ? `\n${output}` : ''}`);
}
return result.stdout ?? '';
}
function parseJsonFromOutput(output) {
const start = output.indexOf('{');
const end = output.lastIndexOf('}');
if (start === -1 || end === -1 || end < start) {
fail('openspec status did not return a JSON object');
}
try {
return JSON.parse(output.slice(start, end + 1));
} catch (error) {
fail(`failed to parse openspec status JSON: ${error.message}`);
}
}
info(`checking ${project} / ${change}`);
runOpenSpec(['validate', change, '--strict']);
const status = parseJsonFromOutput(runOpenSpec(['status', '--change', change, '--json']));
if (status.changeName !== change) {
fail(`openspec status changeName mismatch: ${status.changeName ?? 'missing'}`);
}
if (status.isComplete !== true) {
fail('openspec status isComplete must be true');
}
info('ready to archive');