@bluenyang/reins 0.1.0 → 0.1.1
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/README.md +25 -26
- package/cli/dist/commands/check.js +180 -90
- package/cli/dist/commands/configure.js +79 -0
- package/cli/dist/commands/gate.js +54 -0
- package/cli/dist/commands/install.js +80 -30
- package/cli/dist/commands/metrics.js +138 -0
- package/cli/dist/commands/stages.js +435 -0
- package/cli/dist/commands/status.js +249 -0
- package/cli/dist/commands/update.js +70 -0
- package/cli/dist/lib/blocks.js +6 -1
- package/cli/dist/lib/detect.js +91 -1
- package/cli/dist/lib/fsx.js +8 -3
- package/cli/dist/lib/generate.js +143 -0
- package/cli/dist/lib/git.js +12 -1
- package/cli/dist/lib/harness.js +15 -4
- package/cli/dist/lib/metrics.js +223 -0
- package/cli/dist/lib/reader.js +47 -0
- package/cli/dist/lib/schema.js +23 -2
- package/cli/dist/lib/skills.js +13 -2
- package/cli/dist/lib/upstream.js +8 -0
- package/cli/dist/lib/vendor-write.js +136 -0
- package/cli/dist/lib/work.js +106 -0
- package/cli/dist/reins.js +161 -7
- package/docs/README.ko.md +25 -27
- package/harness/CHANGELOG.md +5 -0
- package/harness/VERSION +1 -1
- package/harness/policy/stops.yaml +2 -2
- package/harness/policy/transitions.yaml +1 -1
- package/harness/roles/code-review.md +6 -3
- package/harness/roles/pl.md +2 -1
- package/harness/roles/qa-design.md +1 -1
- package/harness/roles/qa-run.md +1 -1
- package/harness/templates/bus/request.yaml +8 -2
- package/harness/templates/bus/response.yaml +1 -0
- package/package.json +9 -2
- package/skills/reins-loop/SKILL.md +5 -1
- package/skills/reins-loop/references/vendor-cli.md +40 -0
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute, join, relative, sep } from 'node:path';
|
|
3
|
+
import { harnessDir, readYaml } from '../lib/harness.js';
|
|
4
|
+
import { loadSchemas, validateFile } from '../lib/schema.js';
|
|
5
|
+
import { compound } from './gate.js';
|
|
6
|
+
import { approvedBy, busFor, historyPages, intentFile, intentIds, openTickets, recordedDecisions, ticketFile, } from '../lib/work.js';
|
|
7
|
+
const str = (v) => (typeof v === 'string' ? v : '');
|
|
8
|
+
const list = (v) => (Array.isArray(v) ? v.map(String) : []);
|
|
9
|
+
const mentions = (text, id) => new RegExp(`(?<![\\w-])${id.replace(/[-]/g, '\\-')}(?![\\w-])`).test(text);
|
|
10
|
+
/** Words of a phrase, for matching an ask against recorded decisions. */
|
|
11
|
+
function words(s) {
|
|
12
|
+
return new Set(s
|
|
13
|
+
.toLowerCase()
|
|
14
|
+
.split(/[^\p{L}\p{N}]+/u)
|
|
15
|
+
.filter((w) => w.length >= 3 || /\p{Script=Hangul}/u.test(w)));
|
|
16
|
+
}
|
|
17
|
+
/** A recorded decision whose every word appears in the ask — the question was probably answered before. */
|
|
18
|
+
function answeredBefore(root, ask) {
|
|
19
|
+
const a = words(ask);
|
|
20
|
+
return recordedDecisions(root).filter(({ decision }) => {
|
|
21
|
+
const d = [...words(decision)];
|
|
22
|
+
return d.length > 0 && d.every((w) => a.has(w));
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/** The intent → spec → plan chain a ticket stands on (design §4: approved ∧ accepted ∧ reviewed). */
|
|
26
|
+
function approvalChain(root, intent, ticket) {
|
|
27
|
+
const out = [];
|
|
28
|
+
const i = intentFile(root, intent, 'intent');
|
|
29
|
+
if (!i)
|
|
30
|
+
return [
|
|
31
|
+
{
|
|
32
|
+
reason: 'B3',
|
|
33
|
+
verdict: 'HOLD',
|
|
34
|
+
message: `ticket ${ticket} belongs to intent ${intent}, which has no intent.md`,
|
|
35
|
+
resume: 'write the intent with the user (reins-intent) or move the ticket to its intent',
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
if (i.data['status'] !== 'approved')
|
|
39
|
+
out.push({
|
|
40
|
+
reason: 'B3',
|
|
41
|
+
verdict: 'HOLD',
|
|
42
|
+
path: i.path,
|
|
43
|
+
message: `intent ${intent} is ${str(i.data['status']) || 'without a status'} — only the user approves an intent`,
|
|
44
|
+
resume: 'show the intent to the user; on approval set status: approved, add approvals: [{role: user, at}] and commit with an Approved-by: trailer',
|
|
45
|
+
});
|
|
46
|
+
else {
|
|
47
|
+
if (!hasUser(i.data['approvals']))
|
|
48
|
+
out.push({
|
|
49
|
+
reason: 'B3',
|
|
50
|
+
verdict: 'HOLD',
|
|
51
|
+
path: i.path,
|
|
52
|
+
message: 'intent approvals: has no user entry',
|
|
53
|
+
resume: 'record the user approval: approvals: [{role: user, at: <date>}]',
|
|
54
|
+
});
|
|
55
|
+
if (approvedBy(root, i.path).length === 0)
|
|
56
|
+
out.push({
|
|
57
|
+
reason: 'B3',
|
|
58
|
+
verdict: 'HOLD',
|
|
59
|
+
path: i.path,
|
|
60
|
+
message: 'the approval is not recorded in git (no commit with an Approved-by: trailer)',
|
|
61
|
+
resume: 'commit the status change with the trailer "Approved-by: <user>"',
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
const s = intentFile(root, intent, 'spec');
|
|
65
|
+
if (s?.data['status'] !== 'accepted')
|
|
66
|
+
out.push({
|
|
67
|
+
reason: 'B3',
|
|
68
|
+
verdict: 'HOLD',
|
|
69
|
+
path: s?.path,
|
|
70
|
+
message: s
|
|
71
|
+
? `spec of intent ${intent} is ${str(s.data['status'])}, not accepted`
|
|
72
|
+
: `intent ${intent} has no spec.md`,
|
|
73
|
+
resume: 'planner writes spec.md → pm and qa-design review → the user sees summary_for_user and does not veto (R64)',
|
|
74
|
+
});
|
|
75
|
+
const p = intentFile(root, intent, 'plan');
|
|
76
|
+
if (!p || !['reviewed', 'active'].includes(str(p.data['status'])))
|
|
77
|
+
out.push({
|
|
78
|
+
reason: 'F2',
|
|
79
|
+
verdict: 'STOP',
|
|
80
|
+
path: p?.path,
|
|
81
|
+
message: p
|
|
82
|
+
? `plan of intent ${intent} is ${str(p.data['status'])}, not reviewed`
|
|
83
|
+
: `intent ${intent} has no plan.md`,
|
|
84
|
+
resume: 'pl writes plan.md, qa-design reviews it lightly, then status: reviewed',
|
|
85
|
+
});
|
|
86
|
+
else if (!list(p.data['tickets']).includes(ticket))
|
|
87
|
+
out.push({
|
|
88
|
+
reason: 'P2',
|
|
89
|
+
verdict: 'STOP',
|
|
90
|
+
path: p.path,
|
|
91
|
+
message: `ticket ${ticket} is not in the plan's tickets:`,
|
|
92
|
+
resume: 'add it to plan.md (order of work + tickets:) in the same commit, or drop the ticket',
|
|
93
|
+
});
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
function hasUser(approvals) {
|
|
97
|
+
return (Array.isArray(approvals) &&
|
|
98
|
+
approvals.some((a) => a?.['role'] === 'user'));
|
|
99
|
+
}
|
|
100
|
+
/** T1 — before a ticket starts. */
|
|
101
|
+
export function ticketStart(root, id) {
|
|
102
|
+
const t = ticketFile(root, id);
|
|
103
|
+
if (!t)
|
|
104
|
+
return [
|
|
105
|
+
{
|
|
106
|
+
reason: 'F2',
|
|
107
|
+
verdict: 'STOP',
|
|
108
|
+
message: `no ticket ${id} in .loctx/ticket/`,
|
|
109
|
+
resume: 'pl creates it from plan.md (one order-of-work item = one ticket)',
|
|
110
|
+
},
|
|
111
|
+
];
|
|
112
|
+
const findings = [];
|
|
113
|
+
const dir = harnessDir(root);
|
|
114
|
+
const v = dir ? loadSchemas(dir).get('ticket') : undefined;
|
|
115
|
+
if (v)
|
|
116
|
+
for (const i of validateFile(v, join(root, t.path)))
|
|
117
|
+
findings.push({
|
|
118
|
+
reason: 'F2',
|
|
119
|
+
verdict: 'STOP',
|
|
120
|
+
path: t.path,
|
|
121
|
+
message: `ticket: ${i.message}`,
|
|
122
|
+
resume: 'fix the frontmatter to match ticket.schema.json',
|
|
123
|
+
});
|
|
124
|
+
findings.push(...approvalChain(root, str(t.data['intent']), id));
|
|
125
|
+
for (const ask of list(t.data['asks'])) {
|
|
126
|
+
const prior = answeredBefore(root, ask);
|
|
127
|
+
findings.push({
|
|
128
|
+
reason: 'B1',
|
|
129
|
+
verdict: 'HOLD',
|
|
130
|
+
path: t.path,
|
|
131
|
+
message: prior.length
|
|
132
|
+
? `open ask "${ask}" — possibly answered already: ${prior.map((p) => `${p.decision} (${p.path})`).join('; ')}`
|
|
133
|
+
: `open ask "${ask}"`,
|
|
134
|
+
resume: prior.length
|
|
135
|
+
? "read the recorded decision; if it answers the ask, move it to the ticket's decisions section and clear asks:"
|
|
136
|
+
: "ask the user; record the answer in the ticket's decisions section and clear asks:",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const history = historyPages(root);
|
|
140
|
+
for (const dep of list(t.data['depends'])) {
|
|
141
|
+
const d = ticketFile(root, dep);
|
|
142
|
+
if (d && d.data['status'] !== 'done')
|
|
143
|
+
findings.push({
|
|
144
|
+
reason: 'F2',
|
|
145
|
+
verdict: 'STOP',
|
|
146
|
+
path: t.path,
|
|
147
|
+
message: `depends on ${dep}, which is ${str(d.data['status'])}`,
|
|
148
|
+
resume: `finish ${dep} first`,
|
|
149
|
+
});
|
|
150
|
+
else if (!d && !history.some((h) => mentions(h.text, dep)))
|
|
151
|
+
findings.push({
|
|
152
|
+
reason: 'F2',
|
|
153
|
+
verdict: 'STOP',
|
|
154
|
+
path: t.path,
|
|
155
|
+
message: `depends on ${dep}, which is neither open nor recorded as done in history`,
|
|
156
|
+
resume: `check the id; a finished ticket has a line in .loctx/history/`,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
const role = str(t.data['role']);
|
|
160
|
+
if (dir) {
|
|
161
|
+
const roleFile = join(dir, 'roles', `${role}.md`);
|
|
162
|
+
const bindingsPath = join(dir, 'bindings.yaml');
|
|
163
|
+
const bound = existsSync(bindingsPath)
|
|
164
|
+
? (readYaml(bindingsPath).roles ?? {})
|
|
165
|
+
: null;
|
|
166
|
+
if (!existsSync(roleFile))
|
|
167
|
+
findings.push({
|
|
168
|
+
reason: 'F2',
|
|
169
|
+
verdict: 'STOP',
|
|
170
|
+
path: t.path,
|
|
171
|
+
message: `role "${role}" does not exist (roles: ${readdirSync(join(dir, 'roles'))
|
|
172
|
+
.map((f) => f.replace(/\.md$/, ''))
|
|
173
|
+
.join(', ')})`,
|
|
174
|
+
resume: 'set role: to one of the harness roles',
|
|
175
|
+
});
|
|
176
|
+
else if (bound && !(role in bound))
|
|
177
|
+
findings.push({
|
|
178
|
+
reason: 'F2',
|
|
179
|
+
verdict: 'STOP',
|
|
180
|
+
path: '.agents/harness/bindings.yaml',
|
|
181
|
+
message: `role "${role}" has no binding`,
|
|
182
|
+
resume: `reins configure bindings --set ${role}=<model>`,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return findings;
|
|
186
|
+
}
|
|
187
|
+
/** What a raw evidence text says: the command line (`$ cmd`) and the exit code (`exit N`). */
|
|
188
|
+
export function readEvidence(root, evidence) {
|
|
189
|
+
const tokens = evidence.split(/\s+/).filter(Boolean);
|
|
190
|
+
// an absolute path inside the repository reads like a relative one; outside it is refused
|
|
191
|
+
const inside = (t) => {
|
|
192
|
+
const rel = relative(root, t);
|
|
193
|
+
return rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
|
|
194
|
+
};
|
|
195
|
+
const outside = tokens.filter((t) => isAbsolute(t) && !inside(t));
|
|
196
|
+
const resolve = (t) => (isAbsolute(t) ? t : join(root, t));
|
|
197
|
+
const files = tokens.length > 0 && tokens.every((t) => existsSync(resolve(t)));
|
|
198
|
+
const pieces = outside.length > 0
|
|
199
|
+
? outside.map((t) => ({ source: t, text: '', outside: true }))
|
|
200
|
+
: files
|
|
201
|
+
? tokens.map((t) => ({
|
|
202
|
+
source: isAbsolute(t) ? relative(root, t) : t,
|
|
203
|
+
text: readFileSync(resolve(t), 'utf8'),
|
|
204
|
+
}))
|
|
205
|
+
: [{ source: 'inline', text: evidence }];
|
|
206
|
+
return pieces.map((p) => {
|
|
207
|
+
const empty = !p.outside &&
|
|
208
|
+
p.text
|
|
209
|
+
.split(/\r?\n/)
|
|
210
|
+
.filter((l) => !l.startsWith('$ ') && !/^exit[: =]?\s*-?\d+\s*$/i.test(l))
|
|
211
|
+
.join('')
|
|
212
|
+
.trim() === '';
|
|
213
|
+
const code = /^exit[: =]?\s*(-?\d+)\s*$/im.exec(p.text)?.[1];
|
|
214
|
+
return {
|
|
215
|
+
...p,
|
|
216
|
+
empty,
|
|
217
|
+
cmd: /^\$ (.+)$/m.exec(p.text)?.[1]?.trim(),
|
|
218
|
+
exit: code === undefined ? undefined : Number(code),
|
|
219
|
+
};
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
/** T3 — at a gate or test verdict: the latest verification of a ticket must be a real pass. */
|
|
223
|
+
export function verdictCheck(root, id) {
|
|
224
|
+
const bus = busFor(root, id);
|
|
225
|
+
const unreadable = bus.flatMap((b) => b.broken);
|
|
226
|
+
if (unreadable.length > 0)
|
|
227
|
+
return unreadable.map((u) => ({
|
|
228
|
+
reason: 'F2',
|
|
229
|
+
verdict: 'STOP',
|
|
230
|
+
path: u.path,
|
|
231
|
+
message: `bus file is not valid YAML: ${u.message}`,
|
|
232
|
+
resume: 'quote values containing ": " or " #" in double quotes, then re-run the check',
|
|
233
|
+
}));
|
|
234
|
+
const verifies = bus.filter((b) => b.request?.['kind'] === 'verify');
|
|
235
|
+
if (verifies.length === 0)
|
|
236
|
+
return [
|
|
237
|
+
{
|
|
238
|
+
reason: 'D5',
|
|
239
|
+
verdict: 'STOP',
|
|
240
|
+
message: `ticket ${id} has no verification exchange in .loctx/bus/`,
|
|
241
|
+
resume: 'pl sends a verify request; verify-tool runs the gates and answers with raw evidence',
|
|
242
|
+
},
|
|
243
|
+
];
|
|
244
|
+
const findings = [];
|
|
245
|
+
const last = verifies.at(-1);
|
|
246
|
+
const dir = harnessDir(root);
|
|
247
|
+
const schemas = dir ? loadSchemas(dir) : undefined;
|
|
248
|
+
const bad = [];
|
|
249
|
+
for (const [kind, name] of [
|
|
250
|
+
['request', 'bus-request'],
|
|
251
|
+
['response', 'bus-response'],
|
|
252
|
+
]) {
|
|
253
|
+
const v = schemas?.get(name);
|
|
254
|
+
const rel = `.loctx/bus/${last.id}.${kind}.yaml`;
|
|
255
|
+
if (v && last[kind])
|
|
256
|
+
for (const i of validateFile(v, join(root, rel), true))
|
|
257
|
+
bad.push({
|
|
258
|
+
reason: 'F2',
|
|
259
|
+
verdict: 'STOP',
|
|
260
|
+
path: rel,
|
|
261
|
+
message: `${name}: ${i.message}`,
|
|
262
|
+
resume: 'fix the bus file against its schema, then re-run the check',
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
if (bad.length > 0)
|
|
266
|
+
return bad;
|
|
267
|
+
if (!last.response)
|
|
268
|
+
return [
|
|
269
|
+
{
|
|
270
|
+
reason: 'D5',
|
|
271
|
+
verdict: 'STOP',
|
|
272
|
+
path: `.loctx/bus/${last.id}.request.yaml`,
|
|
273
|
+
message: 'verification requested but not answered',
|
|
274
|
+
resume: 'verify-tool answers it with a response file',
|
|
275
|
+
},
|
|
276
|
+
];
|
|
277
|
+
// D1: consecutive failed rounds at the tail; a pass resets the counter
|
|
278
|
+
let fails = 0;
|
|
279
|
+
for (const b of verifies.toReversed()) {
|
|
280
|
+
if (b.response?.['verdict'] !== 'fail')
|
|
281
|
+
break;
|
|
282
|
+
fails++;
|
|
283
|
+
}
|
|
284
|
+
if (fails >= 2)
|
|
285
|
+
findings.push({
|
|
286
|
+
reason: 'D1',
|
|
287
|
+
verdict: 'ESCALATE',
|
|
288
|
+
message: `${fails} failed verification rounds in a row for ${id}`,
|
|
289
|
+
resume: 'the next role in the chain (pl → pm → advisor → user) finds the root cause',
|
|
290
|
+
});
|
|
291
|
+
else if (fails === 1)
|
|
292
|
+
findings.push({
|
|
293
|
+
reason: 'D1',
|
|
294
|
+
verdict: 'WARN',
|
|
295
|
+
path: `.loctx/bus/${last.id}.response.yaml`,
|
|
296
|
+
message: 'the latest verification failed (round 1 of 2)',
|
|
297
|
+
resume: 'fix and verify again; a second failure escalates',
|
|
298
|
+
});
|
|
299
|
+
if (last.response['verdict'] !== 'pass')
|
|
300
|
+
return findings;
|
|
301
|
+
const at = `.loctx/bus/${last.id}.response.yaml`;
|
|
302
|
+
const ev = str(last.response['evidence']);
|
|
303
|
+
if (!ev.trim())
|
|
304
|
+
findings.push({
|
|
305
|
+
reason: 'D5',
|
|
306
|
+
verdict: 'STOP',
|
|
307
|
+
path: at,
|
|
308
|
+
message: 'pass without evidence',
|
|
309
|
+
resume: 'attach the raw gate output (a .loctx/reports/ path or the text itself)',
|
|
310
|
+
});
|
|
311
|
+
else
|
|
312
|
+
for (const e of readEvidence(root, ev)) {
|
|
313
|
+
const at2 = e.source === 'inline' ? at : `${at} → ${e.source}`;
|
|
314
|
+
if (e.outside) {
|
|
315
|
+
findings.push({
|
|
316
|
+
reason: 'D5',
|
|
317
|
+
verdict: 'STOP',
|
|
318
|
+
path: at2,
|
|
319
|
+
message: 'evidence path outside the repository',
|
|
320
|
+
resume: 'copy the gate output under .loctx/reports/ and cite that path',
|
|
321
|
+
});
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
if (e.empty)
|
|
325
|
+
findings.push({
|
|
326
|
+
reason: 'D4',
|
|
327
|
+
verdict: 'WARN',
|
|
328
|
+
path: at2,
|
|
329
|
+
message: 'gate output is empty',
|
|
330
|
+
resume: 'check that the gate produced output; attach it unabridged',
|
|
331
|
+
});
|
|
332
|
+
if (e.exit === undefined)
|
|
333
|
+
findings.push({
|
|
334
|
+
reason: 'D4',
|
|
335
|
+
verdict: 'STOP',
|
|
336
|
+
path: at2,
|
|
337
|
+
message: 'evidence has no exit code line ("exit N")',
|
|
338
|
+
resume: 're-run the gate unmodified and keep its exit code',
|
|
339
|
+
});
|
|
340
|
+
else if (e.exit !== 0)
|
|
341
|
+
findings.push({
|
|
342
|
+
reason: 'D4',
|
|
343
|
+
verdict: 'STOP',
|
|
344
|
+
path: at2,
|
|
345
|
+
message: `reported as pass, but the evidence exits ${e.exit}`,
|
|
346
|
+
resume: 'a non-zero exit is a failure; answer fail',
|
|
347
|
+
});
|
|
348
|
+
if (!e.cmd)
|
|
349
|
+
findings.push({
|
|
350
|
+
reason: 'D4',
|
|
351
|
+
verdict: 'STOP',
|
|
352
|
+
path: at2,
|
|
353
|
+
message: 'evidence does not show the command that ran ("$ <cmd>")',
|
|
354
|
+
resume: 're-run the gate from gates.yaml and keep the command line',
|
|
355
|
+
});
|
|
356
|
+
else if (compound(e.cmd))
|
|
357
|
+
findings.push({
|
|
358
|
+
reason: 'D4',
|
|
359
|
+
verdict: 'STOP',
|
|
360
|
+
path: at2,
|
|
361
|
+
message: `the gate did not run as a single command: ${e.cmd}`,
|
|
362
|
+
resume: 'run the gate exactly as gates.yaml declares it (C5)',
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
return findings;
|
|
366
|
+
}
|
|
367
|
+
/** T5 — an rc closes: every planned ticket of the release's intents is done and recorded. */
|
|
368
|
+
export function rcClose(root, release) {
|
|
369
|
+
if (!release)
|
|
370
|
+
return [
|
|
371
|
+
{
|
|
372
|
+
reason: 'F2',
|
|
373
|
+
verdict: 'STOP',
|
|
374
|
+
message: 'rc-close needs the release version (--release <version>)',
|
|
375
|
+
},
|
|
376
|
+
];
|
|
377
|
+
const intents = intentIds(root).filter((id) => {
|
|
378
|
+
const i = intentFile(root, id, 'intent');
|
|
379
|
+
return i?.data['status'] === 'approved' && String(i.data['version']) === release;
|
|
380
|
+
});
|
|
381
|
+
if (intents.length === 0)
|
|
382
|
+
return [
|
|
383
|
+
{
|
|
384
|
+
reason: 'F2',
|
|
385
|
+
verdict: 'STOP',
|
|
386
|
+
message: `no approved intent has version ${release}`,
|
|
387
|
+
resume: 'check the version, or set version: in the intents this release bundles',
|
|
388
|
+
},
|
|
389
|
+
];
|
|
390
|
+
const findings = [];
|
|
391
|
+
const history = historyPages(root);
|
|
392
|
+
const open = openTickets(root);
|
|
393
|
+
for (const id of intents) {
|
|
394
|
+
const p = intentFile(root, id, 'plan');
|
|
395
|
+
const planned = list(p?.data['tickets']);
|
|
396
|
+
if (!p || planned.length === 0) {
|
|
397
|
+
findings.push({
|
|
398
|
+
reason: 'F2',
|
|
399
|
+
verdict: 'STOP',
|
|
400
|
+
path: p?.path,
|
|
401
|
+
message: `intent ${id} has no planned tickets`,
|
|
402
|
+
resume: 'plan.md tickets: lists one id per order-of-work item',
|
|
403
|
+
});
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
for (const t of planned) {
|
|
407
|
+
const f = open.find((o) => o.data['id'] === t);
|
|
408
|
+
if (f)
|
|
409
|
+
findings.push({
|
|
410
|
+
reason: 'F2',
|
|
411
|
+
verdict: 'STOP',
|
|
412
|
+
path: f.path,
|
|
413
|
+
message: `ticket ${t} is still open (${str(f.data['status'])})`,
|
|
414
|
+
resume: 'finish it, write its history line, then delete the ticket file',
|
|
415
|
+
});
|
|
416
|
+
else if (!history.some((h) => mentions(h.text, t)))
|
|
417
|
+
findings.push({
|
|
418
|
+
reason: 'F2',
|
|
419
|
+
verdict: 'STOP',
|
|
420
|
+
message: `ticket ${t} is gone but no history page mentions it`,
|
|
421
|
+
resume: 'write what was done in .loctx/history/',
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
for (const o of open)
|
|
425
|
+
if (o.data['intent'] === id && !planned.includes(str(o.data['id'])))
|
|
426
|
+
findings.push({
|
|
427
|
+
reason: 'P2',
|
|
428
|
+
verdict: 'STOP',
|
|
429
|
+
path: o.path,
|
|
430
|
+
message: `ticket ${str(o.data['id'])} is not in the plan of intent ${id}`,
|
|
431
|
+
resume: 'add it to plan.md (departure log) or close it',
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
return findings;
|
|
435
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { basename, join } from 'node:path';
|
|
3
|
+
import { parse } from 'yaml';
|
|
4
|
+
import { repoRoot } from '../lib/git.js';
|
|
5
|
+
import { harnessVersion, packageHarness } from '../lib/upstream.js';
|
|
6
|
+
import { driftGenerated } from '../lib/vendor-write.js';
|
|
7
|
+
import { report } from '../lib/verdict.js';
|
|
8
|
+
import { historyPages, intentDir, intentFile, intentIds, openTickets, } from '../lib/work.js';
|
|
9
|
+
import { driftConvention, driftFormat, driftMatch } from './check.js';
|
|
10
|
+
import { ticketStart } from './stages.js';
|
|
11
|
+
const TICKET_STATUSES = ['todo', 'doing', 'blocked', 'done'];
|
|
12
|
+
const str = (v) => (typeof v === 'string' ? v : typeof v === 'number' ? String(v) : '');
|
|
13
|
+
/** Strings and numbers of an array; anything else (objects, null) is dropped, so a malformed value cannot throw. */
|
|
14
|
+
const list = (v) => Array.isArray(v)
|
|
15
|
+
? v.filter((x) => typeof x === 'string' || typeof x === 'number').map(String)
|
|
16
|
+
: [];
|
|
17
|
+
const distinct = (xs) => [...new Set(xs)];
|
|
18
|
+
const mentions = (text, id) => new RegExp(`(?<![\\w-])${id.replace(/[-]/g, '\\-')}(?![\\w-])`).test(text);
|
|
19
|
+
function safe(fn, fallback) {
|
|
20
|
+
try {
|
|
21
|
+
return fn();
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return fallback;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function harnessStatus(root) {
|
|
28
|
+
const pin = join(root, '.agents', 'harness', 'install.yaml');
|
|
29
|
+
const none = { ok: true, reasons: [], findings: [] };
|
|
30
|
+
if (!existsSync(pin))
|
|
31
|
+
return {
|
|
32
|
+
installed: false,
|
|
33
|
+
tier: null,
|
|
34
|
+
installedVersion: null,
|
|
35
|
+
packageVersion: null,
|
|
36
|
+
drift: none,
|
|
37
|
+
};
|
|
38
|
+
const y = safe(() => parse(readFileSync(pin, 'utf8')) ?? {}, {});
|
|
39
|
+
const findings = [driftFormat, driftConvention, driftMatch, driftGenerated].flatMap((f) => safe(() => f(root), []));
|
|
40
|
+
return {
|
|
41
|
+
installed: true,
|
|
42
|
+
tier: str(y['tier']) || null,
|
|
43
|
+
installedVersion: str(y['version']) || null,
|
|
44
|
+
packageVersion: safe(() => harnessVersion(packageHarness()), null),
|
|
45
|
+
drift: {
|
|
46
|
+
ok: findings.length === 0,
|
|
47
|
+
reasons: distinct(findings.map((f) => f.reason)),
|
|
48
|
+
findings,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** The S-3 table: the first row that matches wins. */
|
|
53
|
+
export function nextForIntent(s) {
|
|
54
|
+
if (s.intent === 'draft')
|
|
55
|
+
return 'user: approve the intent';
|
|
56
|
+
if (s.intent === 'approved' && s.spec === null)
|
|
57
|
+
return 'planner: write spec.md';
|
|
58
|
+
if (s.spec === 'draft')
|
|
59
|
+
return 'pm + qa-design: review the spec';
|
|
60
|
+
if (s.spec === 'reviewed')
|
|
61
|
+
return 'user: read summary_for_user (veto or accept)';
|
|
62
|
+
if (s.spec === 'rejected')
|
|
63
|
+
return 'planner: revise spec.md';
|
|
64
|
+
if (s.spec === 'accepted' && s.plan === null)
|
|
65
|
+
return 'pl: write plan.md';
|
|
66
|
+
if (s.plan === 'draft')
|
|
67
|
+
return 'qa-design: review the plan';
|
|
68
|
+
const recorded = (t) => s.history.some((h) => mentions(h, t));
|
|
69
|
+
const closed = (t) => !s.openIds.includes(t);
|
|
70
|
+
if (s.plan !== null && s.planned.length > 0 && s.planned.every((t) => closed(t) && recorded(t)))
|
|
71
|
+
return 'pm: close the rc (reins check --stage rc-close --release <version>)';
|
|
72
|
+
if ((s.plan === 'reviewed' || s.plan === 'active') &&
|
|
73
|
+
s.open === 0 &&
|
|
74
|
+
s.planned.some((t) => closed(t) && !recorded(t)))
|
|
75
|
+
return 'pl: create tickets';
|
|
76
|
+
if (s.broken.length > 0)
|
|
77
|
+
return `invalid frontmatter (F2): ${s.broken.join(', ')}`;
|
|
78
|
+
return '—';
|
|
79
|
+
}
|
|
80
|
+
function statusOf(f) {
|
|
81
|
+
return f ? str(f.data['status']) || '?' : null;
|
|
82
|
+
}
|
|
83
|
+
function intentsStatus(root, tickets) {
|
|
84
|
+
const history = safe(() => historyPages(root).map((h) => h.text), []);
|
|
85
|
+
const intents = [];
|
|
86
|
+
let superseded = 0;
|
|
87
|
+
for (const id of intentIds(root)) {
|
|
88
|
+
const i = intentFile(root, id, 'intent');
|
|
89
|
+
if (i?.data['status'] === 'superseded') {
|
|
90
|
+
superseded++;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const spec = intentFile(root, id, 'spec');
|
|
94
|
+
const plan = intentFile(root, id, 'plan');
|
|
95
|
+
const planned = list(plan?.data['tickets']);
|
|
96
|
+
const mine = tickets.filter((t) => str(t.data['intent']) === id);
|
|
97
|
+
const open = mine.length;
|
|
98
|
+
const dir = intentDir(root, id);
|
|
99
|
+
intents.push({
|
|
100
|
+
id,
|
|
101
|
+
slug: str(i?.data['slug']) || (dir ? basename(dir).replace(/^\d{3}_?/, '') : ''),
|
|
102
|
+
version: str(i?.data['version']) || null,
|
|
103
|
+
intent: statusOf(i),
|
|
104
|
+
spec: statusOf(spec),
|
|
105
|
+
plan: statusOf(plan),
|
|
106
|
+
tickets: { open, planned: planned.length },
|
|
107
|
+
next: nextForIntent({
|
|
108
|
+
intent: statusOf(i),
|
|
109
|
+
spec: statusOf(spec),
|
|
110
|
+
plan: statusOf(plan),
|
|
111
|
+
open,
|
|
112
|
+
openIds: tickets.map(ticketId),
|
|
113
|
+
planned,
|
|
114
|
+
history,
|
|
115
|
+
broken: [i, spec, plan].filter((f) => !!f?.error).map((f) => f.path),
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return { intents, superseded };
|
|
120
|
+
}
|
|
121
|
+
const fileIdOf = (t) => /^([^_.]+)/.exec(basename(t.path))?.[1] ?? basename(t.path);
|
|
122
|
+
const ticketId = (t) => str(t.data['id']) || fileIdOf(t);
|
|
123
|
+
/** A ticket that cannot be read; shown as invalid so one bad file never hides the others. */
|
|
124
|
+
function invalidTicket(t) {
|
|
125
|
+
return {
|
|
126
|
+
id: ticketId(t),
|
|
127
|
+
path: t.path,
|
|
128
|
+
status: null,
|
|
129
|
+
role: null,
|
|
130
|
+
intent: null,
|
|
131
|
+
orphan: false,
|
|
132
|
+
invalid: true,
|
|
133
|
+
asks: [],
|
|
134
|
+
t1: null,
|
|
135
|
+
next: 'invalid (F2)',
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function ticketStatus(root, t) {
|
|
139
|
+
try {
|
|
140
|
+
return readTicket(root, t);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return invalidTicket(t);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function readTicket(root, t) {
|
|
147
|
+
const fileId = fileIdOf(t);
|
|
148
|
+
const id = ticketId(t);
|
|
149
|
+
const status = str(t.data['status']) || null;
|
|
150
|
+
const intent = str(t.data['intent']) || null;
|
|
151
|
+
const role = str(t.data['role']) || null;
|
|
152
|
+
const rawAsks = t.data['asks'];
|
|
153
|
+
const asksOk = rawAsks === undefined ||
|
|
154
|
+
rawAsks === null ||
|
|
155
|
+
(Array.isArray(rawAsks) && rawAsks.every((a) => typeof a === 'string'));
|
|
156
|
+
const asks = asksOk ? list(rawAsks) : [];
|
|
157
|
+
const invalid = !!t.error || !asksOk || status === null || !TICKET_STATUSES.includes(status);
|
|
158
|
+
const orphan = !intent || !intentDir(root, intent);
|
|
159
|
+
const base = { id, path: t.path, status, role, intent, orphan, invalid, asks, t1: null };
|
|
160
|
+
if (invalid)
|
|
161
|
+
return { ...base, next: 'invalid (F2)' };
|
|
162
|
+
if (status === 'todo') {
|
|
163
|
+
const findings = safe(() => ticketStart(root, fileId), [
|
|
164
|
+
{ reason: 'F2', verdict: 'STOP', message: 'T1 could not run' },
|
|
165
|
+
]);
|
|
166
|
+
const r = report('ticket-start', findings);
|
|
167
|
+
const reasons = distinct(findings.filter((f) => f.verdict !== 'WARN').map((f) => f.reason));
|
|
168
|
+
const next = r.verdict === 'CONTINUE'
|
|
169
|
+
? `${role ?? 'implementer'}: start`
|
|
170
|
+
: `T1 ${r.verdict} (${reasons.join(', ')}) — reins check --stage ticket-start --ticket ${fileId}`;
|
|
171
|
+
return { ...base, t1: { verdict: r.verdict, reasons }, next };
|
|
172
|
+
}
|
|
173
|
+
if (status === 'doing')
|
|
174
|
+
return { ...base, next: `reins check --stage verdict --ticket ${id}` };
|
|
175
|
+
if (status === 'blocked')
|
|
176
|
+
return { ...base, next: `blocked — ${asks.length} open ask${asks.length === 1 ? '' : 's'}` };
|
|
177
|
+
return { ...base, next: 'write history, delete the ticket' };
|
|
178
|
+
}
|
|
179
|
+
function busCount(root) {
|
|
180
|
+
const dir = join(root, '.loctx', 'bus');
|
|
181
|
+
return existsSync(dir) ? readdirSync(dir).filter((e) => e.endsWith('.yaml')).length : 0;
|
|
182
|
+
}
|
|
183
|
+
/** Everything `reins status` shows, as plain data. Reads only; never throws on malformed files. */
|
|
184
|
+
export function collectStatus(root) {
|
|
185
|
+
const open = safe(() => openTickets(root), []);
|
|
186
|
+
const { intents, superseded } = safe(() => intentsStatus(root, open), {
|
|
187
|
+
intents: [],
|
|
188
|
+
superseded: 0,
|
|
189
|
+
});
|
|
190
|
+
const pages = safe(() => historyPages(root), []);
|
|
191
|
+
return {
|
|
192
|
+
harness: harnessStatus(root),
|
|
193
|
+
intents,
|
|
194
|
+
supersededIntents: superseded,
|
|
195
|
+
tickets: open.map((t) => ticketStatus(root, t)),
|
|
196
|
+
bus: safe(() => busCount(root), 0),
|
|
197
|
+
lastHistory: pages.length ? basename(pages[pages.length - 1].path) : null,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/** The text view of the same object `--json` prints. */
|
|
201
|
+
export function formatStatus(d) {
|
|
202
|
+
const out = [];
|
|
203
|
+
const h = d.harness;
|
|
204
|
+
if (!h.installed)
|
|
205
|
+
out.push('harness: not installed — run reins install');
|
|
206
|
+
else
|
|
207
|
+
out.push(`harness: ${h.tier ?? '?'} · installed v${h.installedVersion ?? '?'} · package v${h.packageVersion ?? '?'} · drift: ${h.drift.ok ? 'ok' : h.drift.reasons.join(', ')}${h.drift.ok ? '' : ' (reins check --drift)'}`);
|
|
208
|
+
out.push('');
|
|
209
|
+
out.push('intents:');
|
|
210
|
+
if (d.intents.length === 0)
|
|
211
|
+
out.push(' none');
|
|
212
|
+
for (const i of d.intents) {
|
|
213
|
+
out.push(` ${i.id} ${i.slug}${i.version ? ` v${i.version}` : ''} — intent: ${i.intent ?? '—'} · spec: ${i.spec ?? '—'} · plan: ${i.plan ?? '—'} · tickets: ${i.tickets.open} open / ${i.tickets.planned} planned`);
|
|
214
|
+
out.push(` next: ${i.next}`);
|
|
215
|
+
}
|
|
216
|
+
if (d.supersededIntents > 0)
|
|
217
|
+
out.push(` (${d.supersededIntents} superseded, hidden)`);
|
|
218
|
+
out.push('');
|
|
219
|
+
out.push('tickets:');
|
|
220
|
+
if (d.tickets.length === 0)
|
|
221
|
+
out.push(' none open');
|
|
222
|
+
for (const t of d.tickets) {
|
|
223
|
+
out.push(` ${t.id} — ${t.status ?? '?'} · ${t.role ?? '?'} · asks: ${t.asks.length}${t.orphan ? ' · orphan' : ''}`);
|
|
224
|
+
out.push(` next: ${t.next}`);
|
|
225
|
+
if (t.status === 'blocked' && !t.invalid)
|
|
226
|
+
for (const a of t.asks)
|
|
227
|
+
out.push(` ? ${a}`);
|
|
228
|
+
}
|
|
229
|
+
out.push('');
|
|
230
|
+
out.push(`bus: ${d.bus} file${d.bus === 1 ? '' : 's'}`);
|
|
231
|
+
out.push(`last history: ${d.lastHistory ?? '—'}`);
|
|
232
|
+
return `${out.join('\n')}\n`;
|
|
233
|
+
}
|
|
234
|
+
/** `reins status [--json]`: exit 0 anywhere inside a git repository, 2 outside one. */
|
|
235
|
+
export function runStatus(opts = {}) {
|
|
236
|
+
let root;
|
|
237
|
+
try {
|
|
238
|
+
root = repoRoot(opts.cwd ?? process.cwd());
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
return { code: 2, stdout: '', stderr: 'reins status: not inside a git repository\n' };
|
|
242
|
+
}
|
|
243
|
+
const data = collectStatus(root);
|
|
244
|
+
return {
|
|
245
|
+
code: 0,
|
|
246
|
+
stdout: opts.json ? `${JSON.stringify(data, null, 2)}\n` : formatStatus(data),
|
|
247
|
+
stderr: '',
|
|
248
|
+
};
|
|
249
|
+
}
|