arbiterdesign 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/LICENSE +21 -0
- package/README.md +143 -0
- package/dist/candidates.js +155 -0
- package/dist/commands/board.js +41 -0
- package/dist/commands/candidate.js +168 -0
- package/dist/commands/drift.js +67 -0
- package/dist/commands/export.js +160 -0
- package/dist/commands/init.js +194 -0
- package/dist/commands/prompt.js +69 -0
- package/dist/commands/publish.js +139 -0
- package/dist/commands/pull.js +27 -0
- package/dist/commands/record.js +408 -0
- package/dist/commands/review.js +135 -0
- package/dist/commands/rules.js +152 -0
- package/dist/commands/snapshot.js +32 -0
- package/dist/commands/sweep.js +87 -0
- package/dist/commands/verify.js +41 -0
- package/dist/destination.js +99 -0
- package/dist/findings.js +70 -0
- package/dist/format.js +247 -0
- package/dist/hosted.js +222 -0
- package/dist/index.js +232 -0
- package/dist/overlap.js +71 -0
- package/dist/schema.js +233 -0
- package/dist/store.js +133 -0
- package/dist/sweep.js +128 -0
- package/dist/test/hosted.test.js +189 -0
- package/dist/test/init.test.js +54 -0
- package/dist/test/pending.test.js +198 -0
- package/dist/test/polish.test.js +130 -0
- package/dist/test/record.test.js +102 -0
- package/dist/test/stages.test.js +188 -0
- package/dist/verify.js +78 -0
- package/dist/work.js +64 -0
- package/package.json +44 -0
- package/templates/SKILL.md +338 -0
- package/templates/review.html +479 -0
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `arbiter record` — the act verb.
|
|
4
|
+
*
|
|
5
|
+
* record '<json>' append a decision now
|
|
6
|
+
* record --pending '<json>' queue it in .arbiter/pending.md for later judgment
|
|
7
|
+
* record P-0003 --as rule judge a queued decision (accept | rule | skip | fix --to "<what instead>")
|
|
8
|
+
* record --all --as accept judge every queued decision the same way [--level polish] [--trigger "…"]
|
|
9
|
+
* record --retire D-0003 --why "…" drop a rule from DECISIONS.md with no replacement; the archive keeps both
|
|
10
|
+
* record (a person at a terminal) walk through it step by step
|
|
11
|
+
*
|
|
12
|
+
* Output is JSON on stdout, always — an agent is usually on the other end.
|
|
13
|
+
*
|
|
14
|
+
* Exit codes:
|
|
15
|
+
* 0 recorded / queued / skipped
|
|
16
|
+
* 1 invalid input or I/O error
|
|
17
|
+
* 2 overlap — nothing written, caller must answer with --supersedes or --keep-both
|
|
18
|
+
* 3 active cap reached — nothing written
|
|
19
|
+
* 4 contradicted — the files named don't match the claim; nothing written (override with --unverified)
|
|
20
|
+
*/
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.record = record;
|
|
26
|
+
exports.queue = queue;
|
|
27
|
+
exports.queueFindings = queueFindings;
|
|
28
|
+
exports.retire = retire;
|
|
29
|
+
exports.judge = judge;
|
|
30
|
+
exports.judgeAll = judgeAll;
|
|
31
|
+
exports.recordInput = recordInput;
|
|
32
|
+
exports.interactiveInput = interactiveInput;
|
|
33
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
34
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
35
|
+
const schema_1 = require("../schema");
|
|
36
|
+
const store_1 = require("../store");
|
|
37
|
+
const overlap_1 = require("../overlap");
|
|
38
|
+
const verify_1 = require("../verify");
|
|
39
|
+
const sweep_1 = require("../sweep");
|
|
40
|
+
const findings_1 = require("../findings");
|
|
41
|
+
const prompt_1 = require("./prompt");
|
|
42
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
43
|
+
const candidate_1 = require("./candidate");
|
|
44
|
+
// ── Parse + record ─────────────────────────────────────────────────────────
|
|
45
|
+
function record(rawJson, opts = {}) {
|
|
46
|
+
const parsed = parse(rawJson);
|
|
47
|
+
if (!parsed.ok)
|
|
48
|
+
return parsed.result;
|
|
49
|
+
return recordInput(parsed.input, opts);
|
|
50
|
+
}
|
|
51
|
+
function queue(rawJson, opts = {}) {
|
|
52
|
+
const parsed = parse(rawJson, { pending: true });
|
|
53
|
+
if (!parsed.ok)
|
|
54
|
+
return parsed.result;
|
|
55
|
+
const paths = (0, store_1.resolvePaths)((0, store_1.findRoot)(opts.cwd));
|
|
56
|
+
const pending = (0, store_1.readPending)(paths);
|
|
57
|
+
const entry = { ...fill(parsed.input, opts, paths.root), id: (0, store_1.nextPendingId)(pending), verdict: 'pending' };
|
|
58
|
+
const checked = check(entry, paths.root, opts);
|
|
59
|
+
if (checked)
|
|
60
|
+
return checked;
|
|
61
|
+
if (!opts.dryRun) {
|
|
62
|
+
(0, store_1.writePending)(paths, [...pending, entry]);
|
|
63
|
+
if (entry.candidate)
|
|
64
|
+
(0, candidate_1.refreshCandidates)(paths);
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
exitCode: 0,
|
|
68
|
+
output: {
|
|
69
|
+
status: 'queued',
|
|
70
|
+
id: entry.id,
|
|
71
|
+
dimension: entry.dimension,
|
|
72
|
+
verified: entry.verified,
|
|
73
|
+
pending: pending.length + 1,
|
|
74
|
+
review: 'npx arbiter review',
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** Queue one pending decision per finding in a scanner's JSON output. */
|
|
79
|
+
function queueFindings(file, opts = {}) {
|
|
80
|
+
const paths = (0, store_1.resolvePaths)((0, store_1.findRoot)(opts.cwd));
|
|
81
|
+
let raw;
|
|
82
|
+
try {
|
|
83
|
+
raw = JSON.parse(node_fs_1.default.readFileSync(node_path_1.default.resolve(paths.root, file), 'utf8'));
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
return fail([`could not read findings: ${e.message}`]);
|
|
87
|
+
}
|
|
88
|
+
const findings = (0, findings_1.parseFindings)(raw);
|
|
89
|
+
if (!findings.length)
|
|
90
|
+
return fail(['no findings recognised in that file — expected an array of { file, message, line?, rule?, suggestion? } or ESLint-style output']);
|
|
91
|
+
const tool = opts.tool ?? node_path_1.default.basename(file, node_path_1.default.extname(file));
|
|
92
|
+
const pending = (0, store_1.readPending)(paths);
|
|
93
|
+
const seen = new Set(pending.map((d) => d.trigger));
|
|
94
|
+
const added = [];
|
|
95
|
+
let skipped = 0;
|
|
96
|
+
for (const f of findings) {
|
|
97
|
+
const input = (0, findings_1.toDecisionInput)(f, paths.root, tool);
|
|
98
|
+
if (seen.has(input.trigger)) {
|
|
99
|
+
skipped++;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const entry = { ...fill(input, opts, paths.root), id: (0, store_1.nextPendingId)([...pending]), verdict: 'pending' };
|
|
103
|
+
pending.push(entry);
|
|
104
|
+
seen.add(entry.trigger);
|
|
105
|
+
added.push(entry.id);
|
|
106
|
+
}
|
|
107
|
+
if (!opts.dryRun)
|
|
108
|
+
(0, store_1.writePending)(paths, pending);
|
|
109
|
+
return { exitCode: 0, output: { status: 'queued', tool, queued: added.length, skippedDuplicates: skipped, ids: added, pending: pending.length, review: 'npx arbiter review' } };
|
|
110
|
+
}
|
|
111
|
+
function parse(rawJson, options = {}) {
|
|
112
|
+
let raw;
|
|
113
|
+
try {
|
|
114
|
+
raw = JSON.parse(rawJson);
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
return { ok: false, result: fail(['input is not valid JSON: ' + e.message]) };
|
|
118
|
+
}
|
|
119
|
+
const v = (0, schema_1.validateInput)(raw, options);
|
|
120
|
+
if (!v.ok)
|
|
121
|
+
return { ok: false, result: fail(v.errors) };
|
|
122
|
+
return { ok: true, input: v.value };
|
|
123
|
+
}
|
|
124
|
+
// ── Retire a rule ──────────────────────────────────────────────────────────
|
|
125
|
+
/** A rule dies with no replacement. Appends a `retire` entry pointing at it; the rule leaves DECISIONS.md. */
|
|
126
|
+
function retire(ruleId, why, opts = {}) {
|
|
127
|
+
const paths = (0, store_1.resolvePaths)((0, store_1.findRoot)(opts.cwd));
|
|
128
|
+
const active = (0, store_1.readActive)(paths);
|
|
129
|
+
const rule = active.find((d) => d.id === ruleId);
|
|
130
|
+
if (!rule)
|
|
131
|
+
return fail([`${ruleId} is not an active rule`]);
|
|
132
|
+
if (!why?.trim())
|
|
133
|
+
return fail(['retire needs --why "<reason>" — the reason is the record']);
|
|
134
|
+
const input = {
|
|
135
|
+
decision: `Retired: ${rule.decision}`,
|
|
136
|
+
rationale: why.trim(),
|
|
137
|
+
rejected: [],
|
|
138
|
+
trigger: `retire ${rule.id}`,
|
|
139
|
+
scope: rule.scope,
|
|
140
|
+
dimension: rule.dimension,
|
|
141
|
+
class: rule.class,
|
|
142
|
+
verdict: 'retire',
|
|
143
|
+
supersedes: rule.id,
|
|
144
|
+
};
|
|
145
|
+
return recordInput(input, opts);
|
|
146
|
+
}
|
|
147
|
+
// ── Judge a pending item ───────────────────────────────────────────────────
|
|
148
|
+
function judge(pendingId, opts) {
|
|
149
|
+
if (!schema_1.PENDING_ID.test(pendingId))
|
|
150
|
+
return fail([`not a pending id: ${pendingId} (looks like P-0003)`]);
|
|
151
|
+
const paths = (0, store_1.resolvePaths)((0, store_1.findRoot)(opts.cwd));
|
|
152
|
+
const pending = (0, store_1.readPending)(paths);
|
|
153
|
+
const item = pending.find((d) => d.id === pendingId);
|
|
154
|
+
if (!item)
|
|
155
|
+
return fail([`${pendingId} is not in the pending queue`]);
|
|
156
|
+
const rest = pending.filter((d) => d.id !== pendingId);
|
|
157
|
+
if (opts.as === 'skip') {
|
|
158
|
+
if (!opts.dryRun)
|
|
159
|
+
(0, store_1.writePending)(paths, rest);
|
|
160
|
+
return { exitCode: 0, output: { status: 'skipped', id: pendingId, pending: rest.length } };
|
|
161
|
+
}
|
|
162
|
+
const { id: _id, verdict: _v, ...base } = item;
|
|
163
|
+
let input;
|
|
164
|
+
if (opts.as === 'fix') {
|
|
165
|
+
const to = opts.to?.trim();
|
|
166
|
+
if (!to)
|
|
167
|
+
return fail(['fix needs --to "<what it should be instead>"']);
|
|
168
|
+
input = {
|
|
169
|
+
...base,
|
|
170
|
+
decision: to,
|
|
171
|
+
rejected: [`${item.decision} — corrected by ${opts.author ?? item.author}`, ...item.rejected],
|
|
172
|
+
verdict: 'fix',
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
input = { ...base, verdict: opts.as };
|
|
177
|
+
}
|
|
178
|
+
const result = recordInput(input, opts);
|
|
179
|
+
if (result.exitCode === 0 && !opts.dryRun)
|
|
180
|
+
(0, store_1.writePending)(paths, rest);
|
|
181
|
+
if (result.exitCode === 0)
|
|
182
|
+
result.output.pending = rest.length;
|
|
183
|
+
result.output.judged = pendingId;
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
/** Judge every pending item at once — the bulk verb. Filter by --level and/or --trigger. */
|
|
187
|
+
function judgeAll(opts) {
|
|
188
|
+
if (opts.as === 'fix')
|
|
189
|
+
return fail(['fix is one at a time — it needs its own --to']);
|
|
190
|
+
const paths = (0, store_1.resolvePaths)((0, store_1.findRoot)(opts.cwd));
|
|
191
|
+
const targets = (0, store_1.readPending)(paths).filter((d) => (!opts.level || d.level === opts.level) && (!opts.trigger || d.trigger === opts.trigger));
|
|
192
|
+
if (!targets.length)
|
|
193
|
+
return { exitCode: 0, output: { status: 'nothing-to-judge', judged: [], pending: (0, store_1.readPending)(paths).length } };
|
|
194
|
+
const judged = [];
|
|
195
|
+
const failed = [];
|
|
196
|
+
for (const d of targets) {
|
|
197
|
+
const r = judge(d.id, opts);
|
|
198
|
+
if (r.exitCode === 0)
|
|
199
|
+
judged.push(String(r.output.id ?? d.id));
|
|
200
|
+
else
|
|
201
|
+
failed.push({ id: d.id, output: r.output });
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
exitCode: failed.length ? 1 : 0,
|
|
205
|
+
output: {
|
|
206
|
+
status: failed.length ? 'partial' : 'recorded',
|
|
207
|
+
as: opts.as,
|
|
208
|
+
judged,
|
|
209
|
+
...(failed.length && { failed }),
|
|
210
|
+
pending: (0, store_1.readPending)(paths).length,
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
// ── Core ───────────────────────────────────────────────────────────────────
|
|
215
|
+
function recordInput(input, opts = {}) {
|
|
216
|
+
if (opts.supersedes && opts.keepBoth)
|
|
217
|
+
return fail(['--supersedes and --keep-both are mutually exclusive']);
|
|
218
|
+
if (opts.supersedes && input.verdict !== 'rule' && input.verdict !== 'retire')
|
|
219
|
+
return fail(['--supersedes only applies when verdict is `rule`']);
|
|
220
|
+
const root = (0, store_1.findRoot)(opts.cwd);
|
|
221
|
+
const paths = (0, store_1.resolvePaths)(root);
|
|
222
|
+
const config = (0, store_1.readConfig)(paths);
|
|
223
|
+
const cap = config?.activeCap ?? store_1.ACTIVE_CAP;
|
|
224
|
+
const active = (0, store_1.readActive)(paths);
|
|
225
|
+
const archive = (0, store_1.readArchive)(paths);
|
|
226
|
+
const supersedes = opts.supersedes ?? input.supersedes ?? null;
|
|
227
|
+
const entry = { ...fill(input, opts, root), id: (0, store_1.nextId)(archive), supersedes };
|
|
228
|
+
// An `accept` is a deviation by definition — nothing to verify. Rules and fixes make claims.
|
|
229
|
+
if (entry.verdict !== 'accept') {
|
|
230
|
+
const checked = check(entry, root, opts);
|
|
231
|
+
if (checked)
|
|
232
|
+
return checked;
|
|
233
|
+
}
|
|
234
|
+
let nextActive = active;
|
|
235
|
+
if (entry.verdict === 'retire') {
|
|
236
|
+
if (!active.some((d) => d.id === supersedes))
|
|
237
|
+
return fail([`${supersedes} is not an active rule`]);
|
|
238
|
+
nextActive = active.filter((d) => d.id !== supersedes);
|
|
239
|
+
}
|
|
240
|
+
if (entry.verdict === 'rule') {
|
|
241
|
+
if (supersedes) {
|
|
242
|
+
const target = active.find((d) => d.id === supersedes);
|
|
243
|
+
if (!target) {
|
|
244
|
+
const inArchive = archive.some((d) => d.id === supersedes);
|
|
245
|
+
return fail([
|
|
246
|
+
inArchive
|
|
247
|
+
? `${supersedes} is in the archive but not active — it was already superseded or was never a rule`
|
|
248
|
+
: `${supersedes} does not exist`,
|
|
249
|
+
]);
|
|
250
|
+
}
|
|
251
|
+
nextActive = active.filter((d) => d.id !== supersedes);
|
|
252
|
+
}
|
|
253
|
+
else if (!opts.keepBoth) {
|
|
254
|
+
const overlaps = (0, overlap_1.findOverlaps)(input, active, { ignore: config?.overlap?.ignore });
|
|
255
|
+
if (overlaps.length) {
|
|
256
|
+
return {
|
|
257
|
+
exitCode: 2,
|
|
258
|
+
output: {
|
|
259
|
+
status: 'overlap',
|
|
260
|
+
message: `This rule overlaps ${overlaps.length} active rule${overlaps.length === 1 ? '' : 's'}. Nothing was written. Ask the user which it replaces, then re-run.`,
|
|
261
|
+
candidate: input,
|
|
262
|
+
overlaps,
|
|
263
|
+
options: [
|
|
264
|
+
...overlaps.map((o) => ({
|
|
265
|
+
flag: `--supersedes ${o.id}`,
|
|
266
|
+
effect: `${o.id} leaves DECISIONS.md and stays in the archive; the new rule records supersedes: ${o.id}`,
|
|
267
|
+
})),
|
|
268
|
+
{ flag: '--keep-both', effect: 'both rules stay active' },
|
|
269
|
+
],
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (nextActive.length >= cap) {
|
|
275
|
+
return {
|
|
276
|
+
exitCode: 3,
|
|
277
|
+
output: {
|
|
278
|
+
status: 'cap',
|
|
279
|
+
message: `DECISIONS.md holds ${nextActive.length} of ${cap} rules. Nothing was written. Supersede an existing rule with --supersedes <id> to make room.`,
|
|
280
|
+
candidate: input,
|
|
281
|
+
active: nextActive.map((d) => ({ id: d.id, decision: d.decision, scope: d.scope })),
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
nextActive = [...nextActive, entry];
|
|
286
|
+
}
|
|
287
|
+
if (!opts.dryRun) {
|
|
288
|
+
(0, store_1.appendArchive)(paths, entry);
|
|
289
|
+
if (entry.verdict === 'rule' || entry.verdict === 'retire')
|
|
290
|
+
(0, store_1.writeActive)(paths, nextActive, cap);
|
|
291
|
+
if (entry.candidate)
|
|
292
|
+
(0, candidate_1.refreshCandidates)(paths);
|
|
293
|
+
}
|
|
294
|
+
// A new mechanical rule reaches backwards: say how much existing code it touches.
|
|
295
|
+
let sweepSummary;
|
|
296
|
+
if (entry.verdict === 'rule' && entry.class === 'mechanical' && entry.expect?.absent?.length) {
|
|
297
|
+
const r = (0, sweep_1.sweep)(root, entry, { exclude: config?.sweep?.exclude });
|
|
298
|
+
const sum = (0, sweep_1.summarize)(r);
|
|
299
|
+
sweepSummary = { ...sum, command: `npx arbiter sweep ${entry.id}`, hint: sum.violations ? 'Ask the user: fix now, queue for review, or ignore?' : 'No existing violations.' };
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
exitCode: 0,
|
|
303
|
+
output: {
|
|
304
|
+
status: opts.dryRun ? 'dry-run' : 'recorded',
|
|
305
|
+
id: entry.id,
|
|
306
|
+
decision: entry.decision,
|
|
307
|
+
verdict: entry.verdict,
|
|
308
|
+
scope: entry.scope,
|
|
309
|
+
dimension: entry.dimension,
|
|
310
|
+
supersedes: entry.supersedes,
|
|
311
|
+
verified: entry.verified,
|
|
312
|
+
candidate: entry.candidate,
|
|
313
|
+
active: `${nextActive.length} of ${cap}`,
|
|
314
|
+
wrote: opts.dryRun ? [] : entry.verdict === 'rule' || entry.verdict === 'retire' ? ['.arbiter/archive.md', 'DECISIONS.md'] : ['.arbiter/archive.md'],
|
|
315
|
+
...(sweepSummary && { sweep: sweepSummary }),
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Check a decision's claim against its files. Sets `verified`; returns a
|
|
321
|
+
* failing result if contradicted and the caller didn't say --unverified.
|
|
322
|
+
*/
|
|
323
|
+
function check(entry, root, opts) {
|
|
324
|
+
if (!(0, verify_1.checkable)(entry.files, entry.expect))
|
|
325
|
+
return null;
|
|
326
|
+
const r = (0, verify_1.verify)(root, entry.files, entry.expect);
|
|
327
|
+
entry.verified = r.ok;
|
|
328
|
+
if (r.ok || opts.unverified)
|
|
329
|
+
return null;
|
|
330
|
+
const failed = r.checks.filter((c) => !c.ok);
|
|
331
|
+
return {
|
|
332
|
+
exitCode: 4,
|
|
333
|
+
output: {
|
|
334
|
+
status: 'contradicted',
|
|
335
|
+
message: `The files don't match the claim (${failed.length} check${failed.length === 1 ? '' : 's'} failed). Nothing was written. Fix the code or correct the claim; --unverified records it anyway, marked unverified.`,
|
|
336
|
+
candidate: { decision: entry.decision, files: entry.files, expect: entry.expect },
|
|
337
|
+
failed,
|
|
338
|
+
},
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
/** Fill the fields the caller doesn't supply. Id is set by the caller. */
|
|
342
|
+
function fill(input, opts, root) {
|
|
343
|
+
const config = (0, store_1.readConfig)((0, store_1.resolvePaths)(root));
|
|
344
|
+
return {
|
|
345
|
+
date: input.date ?? (0, store_1.now)(),
|
|
346
|
+
author: opts.author ?? input.author ?? config?.author ?? (0, store_1.gitUserName)(root) ?? node_os_1.default.userInfo().username,
|
|
347
|
+
class: input.class,
|
|
348
|
+
dimension: input.dimension,
|
|
349
|
+
decision: input.decision,
|
|
350
|
+
change: input.change ?? null,
|
|
351
|
+
level: input.level ?? 'pattern',
|
|
352
|
+
rejected: input.rejected ?? [],
|
|
353
|
+
trigger: input.trigger,
|
|
354
|
+
verdict: input.verdict,
|
|
355
|
+
scope: input.scope,
|
|
356
|
+
rationale: input.rationale,
|
|
357
|
+
supersedes: input.supersedes ?? null,
|
|
358
|
+
ref: opts.ref ?? input.ref ?? null,
|
|
359
|
+
commit: input.commit ?? (0, store_1.currentCommit)(root),
|
|
360
|
+
files: input.files ?? [],
|
|
361
|
+
paths: input.paths ?? [],
|
|
362
|
+
expect: input.expect ?? null,
|
|
363
|
+
verified: null,
|
|
364
|
+
candidate: opts.candidate ?? input.candidate ?? null,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function fail(errors) {
|
|
368
|
+
return { exitCode: 1, output: { status: 'invalid', errors } };
|
|
369
|
+
}
|
|
370
|
+
// ── Interactive ────────────────────────────────────────────────────────────
|
|
371
|
+
/**
|
|
372
|
+
* Walk a person through a decision at the terminal. Selection where possible,
|
|
373
|
+
* one line of typing where not. Returns the JSON `record` expects.
|
|
374
|
+
*/
|
|
375
|
+
async function interactiveInput() {
|
|
376
|
+
const ask = new prompt_1.Prompter();
|
|
377
|
+
try {
|
|
378
|
+
process.stdout.write('Record a design decision. One line each; Enter accepts the default.\n\n');
|
|
379
|
+
const decision = await ask.required('Decision — what was chosen, as a rule someone could follow');
|
|
380
|
+
const rationale = await ask.required('Why');
|
|
381
|
+
const rejected = [];
|
|
382
|
+
for (;;) {
|
|
383
|
+
const r = (await ask.question(rejected.length ? 'Another rejected option (Enter to stop): ' : 'Rejected option, as "<option> — <why it lost>" (Enter to skip): ')).trim();
|
|
384
|
+
if (!r)
|
|
385
|
+
break;
|
|
386
|
+
rejected.push(r);
|
|
387
|
+
}
|
|
388
|
+
const dimension = await ask.pick('Dimension', schema_1.DIMENSIONS, 'interaction');
|
|
389
|
+
const scope = await ask.required('Scope — global, pattern:<name>, or file:<path>', 'global');
|
|
390
|
+
const verdictPick = await ask.pick('Is this', ['a standing rule', 'a one-off exception'], 'a standing rule');
|
|
391
|
+
const cls = await ask.pick('Could a tool check it (colours, spacing, type)?', ['no — judgment', 'yes — mechanical'], 'no — judgment');
|
|
392
|
+
const input = {
|
|
393
|
+
decision,
|
|
394
|
+
rationale,
|
|
395
|
+
rejected,
|
|
396
|
+
trigger: 'Recorded by hand at the terminal',
|
|
397
|
+
scope,
|
|
398
|
+
dimension,
|
|
399
|
+
class: cls.startsWith('yes') ? 'mechanical' : 'judgment',
|
|
400
|
+
verdict: verdictPick.startsWith('a standing') ? 'rule' : 'accept',
|
|
401
|
+
};
|
|
402
|
+
process.stdout.write('\n');
|
|
403
|
+
return JSON.stringify(input);
|
|
404
|
+
}
|
|
405
|
+
finally {
|
|
406
|
+
ask.close();
|
|
407
|
+
}
|
|
408
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `arbiter review` — a local page for judging pending decisions.
|
|
4
|
+
*
|
|
5
|
+
* Plain node:http on 127.0.0.1, one HTML file, no framework. Reads and writes
|
|
6
|
+
* the same files the CLI does; the page has no state of its own. Stops when
|
|
7
|
+
* the user presses Done or Ctrl-C.
|
|
8
|
+
*/
|
|
9
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.review = review;
|
|
14
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
15
|
+
const node_http_1 = __importDefault(require("node:http"));
|
|
16
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
17
|
+
const node_child_process_1 = require("node:child_process");
|
|
18
|
+
const record_1 = require("./record");
|
|
19
|
+
const candidate_1 = require("./candidate");
|
|
20
|
+
const candidates_1 = require("../candidates");
|
|
21
|
+
const hosted_1 = require("../hosted");
|
|
22
|
+
const store_1 = require("../store");
|
|
23
|
+
const PAGE = node_path_1.default.join(__dirname, '..', '..', 'templates', 'review.html');
|
|
24
|
+
function review(opts = {}) {
|
|
25
|
+
const root = (0, store_1.findRoot)(opts.cwd);
|
|
26
|
+
const paths = (0, store_1.resolvePaths)(root);
|
|
27
|
+
const cwd = root;
|
|
28
|
+
const state = () => {
|
|
29
|
+
const comments = (0, hosted_1.readComments)(paths);
|
|
30
|
+
const candidates = (0, candidates_1.readCandidates)(paths).map((c) => ({
|
|
31
|
+
...c,
|
|
32
|
+
snapshotUrl: c.snapshot ? `/snapshots/${c.snapshot}` : null,
|
|
33
|
+
decisions: (0, candidates_1.decisionsFor)(paths, c.id).map((d) => ({ id: d.id, decision: d.decision, verdict: d.verdict })),
|
|
34
|
+
comments: comments.filter((x) => x.candidate_id === c.id),
|
|
35
|
+
}));
|
|
36
|
+
const byId = new Map(candidates.map((c) => [c.id, c]));
|
|
37
|
+
return {
|
|
38
|
+
project: node_path_1.default.basename(root),
|
|
39
|
+
pending: (0, store_1.readPending)(paths).map((d) => ({ ...d, candidateName: d.candidate ? byId.get(d.candidate)?.name ?? null : null, snapshotUrl: d.candidate ? byId.get(d.candidate)?.snapshotUrl ?? null : null })),
|
|
40
|
+
candidates,
|
|
41
|
+
hosted: (0, hosted_1.readLink)(paths)?.shareUrl ?? null,
|
|
42
|
+
boardVersion: (0, hosted_1.readLink)(paths)?.boardVersion ?? null,
|
|
43
|
+
active: (0, store_1.readActive)(paths).length,
|
|
44
|
+
cap: (0, store_1.readConfig)(paths)?.activeCap ?? store_1.ACTIVE_CAP,
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
return new Promise((resolve) => {
|
|
48
|
+
const server = node_http_1.default.createServer(async (req, res) => {
|
|
49
|
+
const url = new URL(req.url ?? '/', 'http://127.0.0.1');
|
|
50
|
+
if (req.method === 'GET' && url.pathname === '/') {
|
|
51
|
+
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
|
52
|
+
res.end(node_fs_1.default.readFileSync(PAGE, 'utf8'));
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (req.method === 'GET' && url.pathname === '/api/state') {
|
|
56
|
+
json(res, 200, state());
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (req.method === 'POST' && url.pathname === '/api/judge') {
|
|
60
|
+
const body = JSON.parse(await readBody(req));
|
|
61
|
+
const result = (0, record_1.judge)(body.id, { as: body.as, to: body.to, supersedes: body.supersedes, keepBoth: body.keepBoth, cwd });
|
|
62
|
+
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (req.method === 'POST' && url.pathname === '/api/judge-all') {
|
|
66
|
+
const body = JSON.parse(await readBody(req));
|
|
67
|
+
const result = (0, record_1.judgeAll)({ as: body.as, level: body.level, trigger: body.trigger, cwd });
|
|
68
|
+
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (req.method === 'GET' && url.pathname.startsWith('/snapshots/')) {
|
|
72
|
+
const file = node_path_1.default.basename(url.pathname);
|
|
73
|
+
const abs = node_path_1.default.join((0, candidates_1.candidatesDir)(paths), file);
|
|
74
|
+
if (!/^C-\d{4,}\.(png|jpe?g|webp|gif|svg)$/.test(file) || !node_fs_1.default.existsSync(abs)) {
|
|
75
|
+
res.writeHead(404);
|
|
76
|
+
res.end();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const type = { '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.webp': 'image/webp', '.gif': 'image/gif', '.svg': 'image/svg+xml' }[node_path_1.default.extname(file)] ?? 'application/octet-stream';
|
|
80
|
+
res.writeHead(200, { 'content-type': type, 'cache-control': 'no-cache' });
|
|
81
|
+
node_fs_1.default.createReadStream(abs).pipe(res);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (req.method === 'POST' && url.pathname === '/api/candidate') {
|
|
85
|
+
const body = JSON.parse(await readBody(req));
|
|
86
|
+
const result = (0, candidate_1.updateCandidate)(body.id, { state: body.state, why: body.why, keepOthers: body.keepOthers, cwd });
|
|
87
|
+
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (req.method === 'POST' && url.pathname === '/api/done') {
|
|
91
|
+
json(res, 200, { ok: true });
|
|
92
|
+
stop();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
res.writeHead(404);
|
|
96
|
+
res.end();
|
|
97
|
+
});
|
|
98
|
+
server.listen(opts.port ?? 0, '127.0.0.1', () => {
|
|
99
|
+
const addr = server.address();
|
|
100
|
+
const url = `http://127.0.0.1:${addr.port}/`;
|
|
101
|
+
const s = state();
|
|
102
|
+
process.stdout.write(`Arbiter review — ${s.pending.length} pending · ${s.candidates.length} candidate${s.candidates.length === 1 ? '' : 's'} · ${s.active} of ${s.cap} rules\n${url}\nPress Done in the page, or Ctrl-C here, to stop.\n`);
|
|
103
|
+
if (opts.open !== false)
|
|
104
|
+
openBrowser(url);
|
|
105
|
+
});
|
|
106
|
+
// Browsers keep connections alive; close() alone would wait on them forever.
|
|
107
|
+
const stop = () => {
|
|
108
|
+
server.close(() => resolve());
|
|
109
|
+
server.closeAllConnections();
|
|
110
|
+
};
|
|
111
|
+
process.on('SIGINT', stop);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
function json(res, status, body) {
|
|
115
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' });
|
|
116
|
+
res.end(JSON.stringify(body));
|
|
117
|
+
}
|
|
118
|
+
function readBody(req) {
|
|
119
|
+
return new Promise((resolve) => {
|
|
120
|
+
let data = '';
|
|
121
|
+
req.setEncoding('utf8');
|
|
122
|
+
req.on('data', (c) => (data += c));
|
|
123
|
+
req.on('end', () => resolve(data));
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function openBrowser(url) {
|
|
127
|
+
const cmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'cmd' : 'xdg-open';
|
|
128
|
+
const args = process.platform === 'win32' ? ['/c', 'start', '', url] : [url];
|
|
129
|
+
try {
|
|
130
|
+
(0, node_child_process_1.spawn)(cmd, args, { stdio: 'ignore', detached: true }).unref();
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
// Printed URL is the fallback.
|
|
134
|
+
}
|
|
135
|
+
}
|