arbiterdesign 0.1.8 → 0.1.10
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 +288 -242
- package/dist/area-names.js +120 -0
- package/dist/areas.js +131 -0
- package/dist/board-types.js +9 -0
- package/dist/candidates.js +7 -1
- package/dist/commands/areas.js +153 -0
- package/dist/commands/candidate.js +4 -2
- package/dist/commands/decision.js +100 -0
- package/dist/commands/export.js +88 -125
- package/dist/commands/init.js +198 -73
- package/dist/commands/invite.js +67 -0
- package/dist/commands/publish.js +4 -4
- package/dist/commands/record.js +133 -8
- package/dist/commands/remove.js +1 -1
- package/dist/commands/rename.js +25 -11
- package/dist/commands/review.js +458 -78
- package/dist/commands/rules.js +22 -3
- package/dist/commands/setup.js +1 -0
- package/dist/commands/snapshot.js +24 -2
- package/dist/commands/sweep.js +1 -1
- package/dist/commands/update.js +5 -0
- package/dist/comments.js +24 -0
- package/dist/format.js +112 -7
- package/dist/handoff.js +286 -0
- package/dist/hook.js +82 -0
- package/dist/hosted.js +50 -17
- package/dist/index.js +99 -37
- package/dist/localboard.js +219 -0
- package/dist/lock.js +134 -0
- package/dist/overlap.js +16 -0
- package/dist/pictures.js +4 -0
- package/dist/settings.js +107 -0
- package/dist/store.js +164 -8
- package/dist/verify.js +4 -2
- package/dist/work.js +110 -1
- package/package.json +3 -2
- package/templates/SKILL.md +146 -51
- package/templates/board/board.css +2 -0
- package/templates/board/board.js +25 -0
- package/templates/board/index.html +15 -0
- package/templates/hooks/arbiter-board.cjs +50 -0
- package/templates/review.html +0 -757
package/dist/commands/review.js
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
* `arbiter review` —
|
|
3
|
+
* `arbiter review` — the local board: what's waiting for review, and everything decided.
|
|
4
4
|
*
|
|
5
|
-
* Plain node:http on 127.0.0.1
|
|
6
|
-
* the
|
|
7
|
-
* the
|
|
5
|
+
* Plain node:http on 127.0.0.1. The page is built from board/ into templates/board, and its data from
|
|
6
|
+
* the project's files (src/localboard.ts); it reads and writes the same files the CLI does and has no
|
|
7
|
+
* state of its own. It's meant to stay open beside the work: the page redraws when the files
|
|
8
|
+
* change, whoever changed them. Stops a few seconds after its last tab closes, or on Ctrl-C.
|
|
9
|
+
* Each project gets the same address every time (steadyPort), so a tab you kept picks up again
|
|
10
|
+
* when the board restarts, and running it twice opens the one already running.
|
|
11
|
+
*
|
|
12
|
+
* Every answer is added to the handoff (src/handoff.ts) as it's given: what's left for the agent
|
|
13
|
+
* to do in the code, which it takes with your next message or at the next /arbiter. With `json`,
|
|
14
|
+
* an agent is waiting on the page instead, and gets the handoff when the last tab closes.
|
|
8
15
|
*/
|
|
9
16
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
18
|
};
|
|
12
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.steadyPort = steadyPort;
|
|
13
21
|
exports.review = review;
|
|
14
22
|
exports.fromHere = fromHere;
|
|
23
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
15
24
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
16
25
|
const node_http_1 = __importDefault(require("node:http"));
|
|
17
26
|
const node_path_1 = __importDefault(require("node:path"));
|
|
@@ -19,70 +28,127 @@ const node_child_process_1 = require("node:child_process");
|
|
|
19
28
|
const record_1 = require("./record");
|
|
20
29
|
const candidate_1 = require("./candidate");
|
|
21
30
|
const candidates_1 = require("../candidates");
|
|
22
|
-
const hosted_1 = require("../hosted");
|
|
23
31
|
const store_1 = require("../store");
|
|
32
|
+
const lock_1 = require("../lock");
|
|
24
33
|
const autopublish_1 = require("../autopublish");
|
|
25
|
-
const pictures_1 = require("../pictures");
|
|
26
|
-
const requests_1 = require("../requests");
|
|
27
34
|
const work_1 = require("../work");
|
|
28
|
-
const
|
|
29
|
-
const
|
|
35
|
+
const localboard_1 = require("../localboard");
|
|
36
|
+
const areas_1 = require("../areas");
|
|
37
|
+
const settings_1 = require("../settings");
|
|
38
|
+
const rename_1 = require("./rename");
|
|
39
|
+
const decision_1 = require("./decision");
|
|
40
|
+
const remove_1 = require("./remove");
|
|
41
|
+
const handoff_1 = require("../handoff");
|
|
42
|
+
const hook_1 = require("../hook");
|
|
43
|
+
const GRACE = 10_000;
|
|
44
|
+
// Where projects' boards live: below the ports Windows and macOS hand out for outgoing connections,
|
|
45
|
+
// and clear of the usual dev servers. A project whose port is taken tries the next few.
|
|
46
|
+
const STEADY_FROM = 47000;
|
|
47
|
+
const STEADY_SPAN = 1000;
|
|
48
|
+
const STEADY_TRIES = 10;
|
|
49
|
+
/** The same folder on Windows, whatever its case. */
|
|
50
|
+
const sameFolder = (dir) => (process.platform === 'win32' ? node_path_1.default.resolve(dir).toLowerCase() : node_path_1.default.resolve(dir));
|
|
51
|
+
/** This project's board's port: the same every time for the same folder, so its address doesn't change. */
|
|
52
|
+
function steadyPort(root) {
|
|
53
|
+
return STEADY_FROM + (node_crypto_1.default.createHash('sha1').update(sameFolder(root)).digest().readUInt32BE(0) % STEADY_SPAN);
|
|
54
|
+
}
|
|
30
55
|
// Geist, as on arbiter.design. Shipped with the package: the page never loads anything from the network.
|
|
31
56
|
const FONTS = node_path_1.default.join(__dirname, '..', '..', 'templates', 'fonts');
|
|
32
57
|
const FONT_FILES = new Set(['Geist-Variable.woff2', 'GeistMono-Variable.woff2']);
|
|
58
|
+
// The local board's page, built from board/ by `npm run build`.
|
|
59
|
+
const BOARD = node_path_1.default.join(__dirname, '..', '..', 'templates', 'board');
|
|
60
|
+
const BOARD_FILES = { '/': 'index.html', '/board.js': 'board.js', '/board.css': 'board.css' };
|
|
61
|
+
const TYPES = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8' };
|
|
33
62
|
function review(opts = {}) {
|
|
34
63
|
const root = (0, store_1.findRoot)(opts.cwd);
|
|
35
64
|
const paths = (0, store_1.resolvePaths)(root);
|
|
36
65
|
const cwd = root;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
snapshotUrl: c.snapshot ? `/snapshots/${c.snapshot}` : null,
|
|
42
|
-
decisions: (0, candidates_1.decisionsFor)(paths, c.id).map((d) => ({ id: d.id, decision: d.decision, verdict: d.verdict })),
|
|
43
|
-
comments: comments.filter((x) => x.candidate_id === c.id),
|
|
44
|
-
}));
|
|
45
|
-
const byId = new Map(candidates.map((c) => [c.id, c]));
|
|
46
|
-
const version = (0, hosted_1.readLinkOrNull)(paths)?.boardVersion ?? null;
|
|
47
|
-
const archive = (0, store_1.readArchive)(paths);
|
|
48
|
-
// The card a request was made on: its picture, a candidate's or a piece of work's.
|
|
49
|
-
const shotOf = (screen) => {
|
|
50
|
-
if (screen.startsWith('C-'))
|
|
51
|
-
return byId.get(screen)?.snapshotUrl ?? null;
|
|
52
|
-
const f = (0, work_1.workSnapshot)(paths, screen);
|
|
53
|
-
return f ? `/snapshots/${node_path_1.default.basename(f)}` : null;
|
|
54
|
-
};
|
|
55
|
-
const active = (0, store_1.readActive)(paths);
|
|
56
|
-
return {
|
|
57
|
-
project: node_path_1.default.basename(root),
|
|
58
|
-
pending: (0, store_1.readPending)(paths).map((d) => ({
|
|
59
|
-
...d,
|
|
60
|
-
// The rule a clash is with, while it's still in force: the page offers Follow / Replace / Keep as exception.
|
|
61
|
-
againstRule: d.against ? active.filter((r) => r.id === d.against).map((r) => ({ id: r.id, decision: r.decision }))[0] ?? null : null,
|
|
62
|
-
candidateName: d.candidate ? byId.get(d.candidate)?.name ?? null : null,
|
|
63
|
-
snapshotUrl: d.candidate ? byId.get(d.candidate)?.snapshotUrl ?? null : null,
|
|
64
|
-
picture: (0, pictures_1.missingPictures)(paths, [d], candidates)[0] ?? null,
|
|
65
|
-
})),
|
|
66
|
-
// Open ones to answer; approved ones waiting on the agent — shown so it's clear nothing is lost.
|
|
67
|
-
requests: (0, requests_1.readRequests)(paths)
|
|
68
|
-
.filter((r) => r.status === 'open' || r.status === 'approved')
|
|
69
|
-
.map((r) => ({ ...(0, requests_1.describe)(paths, r, version, archive), snapshotUrl: shotOf(r.screen) })),
|
|
70
|
-
candidates,
|
|
71
|
-
hosted: (0, hosted_1.readLinkOrNull)(paths)?.shareUrl ?? null,
|
|
72
|
-
boardVersion: (0, hosted_1.readLinkOrNull)(paths)?.boardVersion ?? null,
|
|
73
|
-
// The rules in force, for the Rules tab: what's already settled before you make another.
|
|
74
|
-
rules: active,
|
|
75
|
-
active: active.length,
|
|
76
|
-
cap: (0, store_1.readConfig)(paths)?.activeCap ?? store_1.ACTIVE_CAP,
|
|
77
|
-
};
|
|
78
|
-
};
|
|
66
|
+
// The review queue, and when the agent makes a code change you ask for: when the last tab closes if
|
|
67
|
+
// it's waiting on the page, with your next message if the Claude Code hook is installed, else at the
|
|
68
|
+
// next /arbiter.
|
|
69
|
+
const state = () => (0, localboard_1.reviewState)(paths, opts.json ? 'waiting' : (0, hook_1.hookInstalled)(root) ? 'next-message' : 'arbiter');
|
|
79
70
|
// Judging twenty items shouldn't mean twenty publishes, so the board is updated once, at the
|
|
80
71
|
// end. The page writes the same files the CLI does, but doesn't go through its handlers.
|
|
72
|
+
const boardData = () => (0, localboard_1.liveBoard)(paths);
|
|
73
|
+
// What the board is drawn from, as one value that changes when any of it does: the page checks it
|
|
74
|
+
// every few seconds and redraws only then, so work the agent records shows up without a reload.
|
|
75
|
+
// The handoff isn't part of the board, and changes with every answer anyway; nor is .gitignore,
|
|
76
|
+
// which the first answer adds the handoff to.
|
|
77
|
+
const stamp = () => {
|
|
78
|
+
let files = 0;
|
|
79
|
+
let bytes = 0;
|
|
80
|
+
let latest = 0;
|
|
81
|
+
const visit = (f) => {
|
|
82
|
+
let st;
|
|
83
|
+
try {
|
|
84
|
+
st = node_fs_1.default.statSync(f);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (st.isDirectory()) {
|
|
90
|
+
for (const e of node_fs_1.default.readdirSync(f))
|
|
91
|
+
if (e !== handoff_1.HANDOFF_NAME && e !== '.gitignore')
|
|
92
|
+
visit(node_path_1.default.join(f, e));
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
files++;
|
|
96
|
+
bytes += st.size;
|
|
97
|
+
latest = Math.max(latest, st.mtimeMs);
|
|
98
|
+
};
|
|
99
|
+
// The rules file as arbiter.json names it now: it can move while the board is open.
|
|
100
|
+
for (const f of [(0, store_1.resolvePaths)(root).decisions, paths.archiveDir, paths.config])
|
|
101
|
+
visit(f);
|
|
102
|
+
return `${files}.${bytes}.${latest}`;
|
|
103
|
+
};
|
|
81
104
|
let changed = false;
|
|
105
|
+
// Every answer that went through, in order: the terminal's line about this sitting.
|
|
106
|
+
const answers = [];
|
|
107
|
+
// Decisions removed on a change's page, with where each was, so Undo can put them back while the board is open.
|
|
108
|
+
const removedDecisions = new Map();
|
|
109
|
+
// Each one is handed over as it's given, so the agent can take it while the board stays open.
|
|
110
|
+
const answer = (a) => {
|
|
111
|
+
answers.push(a);
|
|
112
|
+
(0, handoff_1.saveAnswer)(paths, a);
|
|
113
|
+
};
|
|
114
|
+
// What each judgement on the page did, by its queued id, so a misclick can be taken back while the board is open.
|
|
115
|
+
const judged = new Map();
|
|
116
|
+
const judgedAs = (j, a) => {
|
|
117
|
+
judged.set(j.item.id, { j, a });
|
|
118
|
+
answer(a);
|
|
119
|
+
};
|
|
120
|
+
// Each rule removed on the Design rules page, as it was, so Undo can put it back.
|
|
121
|
+
const retired = new Map();
|
|
122
|
+
// The tabs showing the board. Each one says it's there whenever it checks for changes, and says
|
|
123
|
+
// goodbye as it closes; when the last one has gone and none comes back (a reload does), the board stops.
|
|
124
|
+
const pages = new Set();
|
|
125
|
+
let closing = null;
|
|
126
|
+
let finish = () => { };
|
|
127
|
+
const seen = (page) => {
|
|
128
|
+
pages.add(page);
|
|
129
|
+
if (closing)
|
|
130
|
+
clearTimeout(closing);
|
|
131
|
+
closing = null;
|
|
132
|
+
};
|
|
133
|
+
const gone = (page) => {
|
|
134
|
+
pages.delete(page);
|
|
135
|
+
if (!pages.size && !closing)
|
|
136
|
+
closing = setTimeout(() => finish('done'), opts.grace ?? GRACE);
|
|
137
|
+
};
|
|
82
138
|
return new Promise((resolve) => {
|
|
139
|
+
// One write at a time: the board's own in the order they came, and each against any `arbiter`
|
|
140
|
+
// command the agent runs meanwhile (.arbiter/.lock), so neither writes over the other.
|
|
141
|
+
let writing = Promise.resolve();
|
|
83
142
|
const server = node_http_1.default.createServer(async (req, res) => {
|
|
84
143
|
try {
|
|
85
|
-
|
|
144
|
+
if (req.method === 'POST' && fromHere(req) && req.url !== '/api/bye') {
|
|
145
|
+
await readBody(req); // read before its turn: the handler gets the same body back
|
|
146
|
+
const turn = writing.then(() => (0, lock_1.withLockAsync)(paths, () => handle(req, res)));
|
|
147
|
+
writing = turn.catch(() => { });
|
|
148
|
+
await turn;
|
|
149
|
+
}
|
|
150
|
+
else
|
|
151
|
+
await handle(req, res);
|
|
86
152
|
}
|
|
87
153
|
catch (e) {
|
|
88
154
|
// A broken requests.json, say: the page shows the message instead of the server dying.
|
|
@@ -102,9 +168,16 @@ function review(opts = {}) {
|
|
|
102
168
|
return;
|
|
103
169
|
}
|
|
104
170
|
const url = new URL(req.url ?? '/', 'http://127.0.0.1');
|
|
105
|
-
if (req.method === 'GET' && url.pathname
|
|
106
|
-
|
|
107
|
-
res.
|
|
171
|
+
if (req.method === 'GET' && BOARD_FILES[url.pathname]) {
|
|
172
|
+
const file = node_path_1.default.join(BOARD, BOARD_FILES[url.pathname]);
|
|
173
|
+
res.writeHead(200, { 'content-type': TYPES[node_path_1.default.extname(file)], 'cache-control': 'no-cache' });
|
|
174
|
+
node_fs_1.default.createReadStream(file).pipe(res);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
// Where the board was before the review queue moved into it.
|
|
178
|
+
if (req.method === 'GET' && (url.pathname === '/board' || url.pathname === '/board/')) {
|
|
179
|
+
res.writeHead(301, { location: '/' });
|
|
180
|
+
res.end();
|
|
108
181
|
return;
|
|
109
182
|
}
|
|
110
183
|
// Every write is JSON from this page. A form or a plain-text POST from another site in the same
|
|
@@ -118,30 +191,132 @@ function review(opts = {}) {
|
|
|
118
191
|
json(res, 200, state());
|
|
119
192
|
return;
|
|
120
193
|
}
|
|
194
|
+
if (req.method === 'GET' && url.pathname === '/api/board') {
|
|
195
|
+
json(res, 200, boardData());
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
// DECISIONS.md as it is, for Download on Design rules: the file the agent reads, not a copy of it.
|
|
199
|
+
if (req.method === 'GET' && url.pathname === '/api/decisions') {
|
|
200
|
+
// Resolved now, not at start-up: the rules may have moved to a file of their own since.
|
|
201
|
+
const now = (0, store_1.resolvePaths)(root);
|
|
202
|
+
const text = node_fs_1.default.existsSync(now.decisions) ? node_fs_1.default.readFileSync(now.decisions, 'utf8') : '';
|
|
203
|
+
res.writeHead(200, { 'content-type': 'text/markdown; charset=utf-8', 'content-disposition': `attachment; filename="${(0, store_1.rulesName)(now)}"`, 'cache-control': 'no-store' });
|
|
204
|
+
res.end(text);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
121
207
|
if (req.method === 'POST' && url.pathname === '/api/judge') {
|
|
122
208
|
const body = JSON.parse(await readBody(req));
|
|
123
|
-
|
|
124
|
-
|
|
209
|
+
// Read before judging: judging takes it off the queue, and a rule it replaces out of DECISIONS.md.
|
|
210
|
+
const queued = (0, store_1.readPending)(paths);
|
|
211
|
+
const at = queued.findIndex((d) => d.id === body.id);
|
|
212
|
+
const item = queued[at];
|
|
213
|
+
const active = (0, store_1.readActive)(paths);
|
|
214
|
+
const was = active.findIndex((d) => d.id === body.supersedes);
|
|
215
|
+
const result = (0, record_1.judge)(body.id, { as: body.as, to: body.to, supersedes: body.supersedes, keepBoth: body.keepBoth, unverified: body.unverified === true, cwd });
|
|
216
|
+
if (result.exitCode === 0 && item) {
|
|
125
217
|
changed = true;
|
|
218
|
+
const id = body.as === 'skip' ? undefined : String(result.output.id);
|
|
219
|
+
judgedAs({ item, at, id, ...(was !== -1 && result.output.supersedes === body.supersedes && { replaced: { rule: active[was], at: was } }) }, { kind: 'decision', pending: body.id, as: body.as, id, change: item.change ?? item.decision, to: body.to, follows: body.follows });
|
|
220
|
+
}
|
|
126
221
|
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
127
222
|
return;
|
|
128
223
|
}
|
|
129
224
|
if (req.method === 'POST' && url.pathname === '/api/judge-all') {
|
|
130
225
|
const body = JSON.parse(await readBody(req));
|
|
226
|
+
const queued = (0, store_1.readPending)(paths);
|
|
131
227
|
const result = (0, record_1.judgeAll)({ as: body.as, level: body.level, trigger: body.trigger, cwd });
|
|
132
228
|
if (result.exitCode === 0)
|
|
133
229
|
changed = true;
|
|
230
|
+
// A partial run still recorded some: each one that went through is an answer. They went in
|
|
231
|
+
// queue order, so the ones that didn't fail line up with what they were recorded as.
|
|
232
|
+
const failed = new Set((result.output.failed ?? []).map((f) => f.id));
|
|
233
|
+
const through = queued.filter((d) => (!body.level || d.level === body.level) && (!body.trigger || d.trigger === body.trigger) && !failed.has(d.id));
|
|
234
|
+
(result.output.judged ?? []).forEach((judgedId, k) => {
|
|
235
|
+
const item = through[k];
|
|
236
|
+
if (!item)
|
|
237
|
+
return;
|
|
238
|
+
const id = body.as === 'skip' ? undefined : judgedId;
|
|
239
|
+
judgedAs({ item, at: queued.indexOf(item), id }, { kind: 'decision', pending: item.id, as: body.as, id, change: item.change ?? item.decision });
|
|
240
|
+
});
|
|
134
241
|
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
135
242
|
return;
|
|
136
243
|
}
|
|
137
|
-
if (req.method === 'POST' && url.pathname === '/api/
|
|
244
|
+
if (req.method === 'POST' && url.pathname === '/api/undo') {
|
|
138
245
|
const body = JSON.parse(await readBody(req));
|
|
139
|
-
const
|
|
140
|
-
if (
|
|
246
|
+
const was = judged.get(body.id);
|
|
247
|
+
if (!was) {
|
|
248
|
+
json(res, 200, { exitCode: 1, status: 'invalid', errors: [`Only what was judged on this board since it started can be undone`] });
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
const result = (0, record_1.unjudge)(was.j, { cwd });
|
|
252
|
+
if (result.exitCode === 0) {
|
|
141
253
|
changed = true;
|
|
254
|
+
judged.delete(body.id);
|
|
255
|
+
answers.splice(answers.indexOf(was.a), 1);
|
|
256
|
+
// A fix the agent has taken may be in the code already: the record is back, the code may not be.
|
|
257
|
+
if (!(0, handoff_1.withdrawAnswer)(paths, was.a) && was.a.as === 'fix')
|
|
258
|
+
result.output.taken = true;
|
|
259
|
+
}
|
|
142
260
|
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
143
261
|
return;
|
|
144
262
|
}
|
|
263
|
+
if (req.method === 'POST' && url.pathname === '/api/reword') {
|
|
264
|
+
const body = JSON.parse(await readBody(req));
|
|
265
|
+
const result = (0, record_1.reword)(body.id, body.to, { cwd });
|
|
266
|
+
// The agent may have read the old words already: it hears the new ones as it hears a fix.
|
|
267
|
+
if (result.exitCode === 0) {
|
|
268
|
+
changed = true;
|
|
269
|
+
answer({ kind: 'reword', id: body.id, to: String(result.output.decision) });
|
|
270
|
+
}
|
|
271
|
+
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if (req.method === 'POST' && url.pathname === '/api/retire') {
|
|
275
|
+
const body = JSON.parse(await readBody(req));
|
|
276
|
+
const rule = (0, store_1.readActive)(paths).find((d) => d.id === body.id);
|
|
277
|
+
const result = (0, record_1.retire)(body.id, body.why, { cwd });
|
|
278
|
+
if (result.exitCode === 0 && rule) {
|
|
279
|
+
changed = true;
|
|
280
|
+
const a = { kind: 'retire', id: body.id, rule: rule.decision, why: body.why.trim() };
|
|
281
|
+
retired.set(body.id, { rule, id: String(result.output.id), a });
|
|
282
|
+
answer(a);
|
|
283
|
+
}
|
|
284
|
+
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (req.method === 'POST' && url.pathname === '/api/unretire') {
|
|
288
|
+
const body = JSON.parse(await readBody(req));
|
|
289
|
+
const was = retired.get(body.id);
|
|
290
|
+
if (!was) {
|
|
291
|
+
json(res, 200, { exitCode: 1, status: 'invalid', errors: [`Only a rule removed on this board since it started can be put back`] });
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
const result = (0, record_1.unretire)(was, { cwd });
|
|
295
|
+
if (result.exitCode === 0) {
|
|
296
|
+
changed = true;
|
|
297
|
+
retired.delete(body.id);
|
|
298
|
+
// Not heard of yet: the agent never hears of it. Heard of: it hears the rule is back.
|
|
299
|
+
if ((0, handoff_1.withdrawRemoval)(paths, body.id))
|
|
300
|
+
answers.splice(answers.indexOf(was.a), 1);
|
|
301
|
+
else
|
|
302
|
+
answer({ kind: 'restore', id: body.id });
|
|
303
|
+
}
|
|
304
|
+
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
// An earlier picture of a piece of work, by its place under .arbiter/work/history/.
|
|
308
|
+
if (req.method === 'GET' && url.pathname.startsWith('/snapshots/history/')) {
|
|
309
|
+
const abs = (0, work_1.historyFile)(paths, decodeURIComponent(url.pathname.slice('/snapshots/'.length)));
|
|
310
|
+
if (!abs) {
|
|
311
|
+
res.writeHead(404);
|
|
312
|
+
res.end();
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
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(abs)] ?? 'application/octet-stream';
|
|
316
|
+
res.writeHead(200, { 'content-type': type, 'cache-control': 'max-age=86400', ...PICTURE });
|
|
317
|
+
node_fs_1.default.createReadStream(abs).pipe(res);
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
145
320
|
if (req.method === 'GET' && url.pathname.startsWith('/snapshots/')) {
|
|
146
321
|
const file = node_path_1.default.basename(url.pathname);
|
|
147
322
|
const abs = node_path_1.default.join(file.startsWith('W-') ? (0, work_1.workDir)(paths) : (0, candidates_1.candidatesDir)(paths), file);
|
|
@@ -151,7 +326,7 @@ function review(opts = {}) {
|
|
|
151
326
|
return;
|
|
152
327
|
}
|
|
153
328
|
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';
|
|
154
|
-
res.writeHead(200, { 'content-type': type, 'cache-control': 'no-cache' });
|
|
329
|
+
res.writeHead(200, { 'content-type': type, 'cache-control': 'no-cache', ...PICTURE });
|
|
155
330
|
node_fs_1.default.createReadStream(abs).pipe(res);
|
|
156
331
|
return;
|
|
157
332
|
}
|
|
@@ -169,50 +344,202 @@ function review(opts = {}) {
|
|
|
169
344
|
if (req.method === 'POST' && url.pathname === '/api/candidate') {
|
|
170
345
|
const body = JSON.parse(await readBody(req));
|
|
171
346
|
const result = (0, candidate_1.updateCandidate)(body.id, { state: body.state, why: body.why, keepOthers: body.keepOthers, cwd });
|
|
347
|
+
if (result.exitCode === 0) {
|
|
348
|
+
changed = true;
|
|
349
|
+
const c = (0, candidates_1.readCandidates)(paths).find((x) => x.id === body.id);
|
|
350
|
+
answer({ kind: 'option', id: body.id, name: c?.name ?? body.id, state: body.state });
|
|
351
|
+
}
|
|
352
|
+
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
// Folders in the side nav's Areas. They only arrange the board, so the agent isn't told and the
|
|
356
|
+
// team board isn't republished: the file is saved, and every open tab redraws from it.
|
|
357
|
+
if (req.method === 'POST' && url.pathname === '/api/areas') {
|
|
358
|
+
const r = (0, areas_1.cleanArrangement)(JSON.parse(await readBody(req)));
|
|
359
|
+
if (!r.ok) {
|
|
360
|
+
json(res, 200, { exitCode: 1, status: 'invalid', errors: r.errors });
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
(0, areas_1.writeArrangement)(paths, r.value);
|
|
364
|
+
json(res, 200, { exitCode: 0, arrangement: r.value });
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
// Rename on a change's page: the work and everything filed under it move to the new name, as
|
|
368
|
+
// `arbiter rename` does. The agent hears the new name, so its next decisions don't start a second card.
|
|
369
|
+
if (req.method === 'POST' && url.pathname === '/api/rename') {
|
|
370
|
+
const body = JSON.parse(await readBody(req));
|
|
371
|
+
const result = (0, rename_1.renameWork)(String(body.from ?? ''), String(body.to ?? ''), { cwd, merge: body.merge === true });
|
|
372
|
+
if (result.exitCode === 0) {
|
|
373
|
+
changed = true;
|
|
374
|
+
answer({ kind: 'rename', from: String(result.output.from), to: String(result.output.to) });
|
|
375
|
+
}
|
|
376
|
+
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
// A change's page, on one of its decisions: new words for its Change line; remove it, with Undo while
|
|
380
|
+
// the board is open; or make it a rule. A rule's own words and its removal go through reword and retire.
|
|
381
|
+
if (req.method === 'POST' && url.pathname === '/api/edit-change') {
|
|
382
|
+
const body = JSON.parse(await readBody(req));
|
|
383
|
+
const result = (0, decision_1.editChange)(String(body.id ?? ''), String(body.to ?? ''), { cwd });
|
|
172
384
|
if (result.exitCode === 0)
|
|
173
385
|
changed = true;
|
|
174
386
|
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
175
387
|
return;
|
|
176
388
|
}
|
|
177
|
-
if (req.method === 'POST' && url.pathname === '/api/
|
|
389
|
+
if (req.method === 'POST' && url.pathname === '/api/remove-decision') {
|
|
390
|
+
const body = JSON.parse(await readBody(req));
|
|
391
|
+
const before = (0, store_1.readArchive)(paths);
|
|
392
|
+
const index = before.findIndex((d) => d.id === body.id);
|
|
393
|
+
const result = (0, remove_1.remove)(String(body.id ?? ''), { cwd });
|
|
394
|
+
if (result.exitCode === 0 && index >= 0) {
|
|
395
|
+
changed = true;
|
|
396
|
+
removedDecisions.set(body.id, { entry: before[index], index });
|
|
397
|
+
}
|
|
398
|
+
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
if (req.method === 'POST' && url.pathname === '/api/restore-decision') {
|
|
402
|
+
const body = JSON.parse(await readBody(req));
|
|
403
|
+
const was = removedDecisions.get(body.id);
|
|
404
|
+
if (!was) {
|
|
405
|
+
json(res, 200, { exitCode: 1, status: 'invalid', errors: ['Only a decision removed on this board since it started can be put back'] });
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
const result = (0, decision_1.restoreDecision)(was, { cwd });
|
|
409
|
+
if (result.exitCode === 0) {
|
|
410
|
+
changed = true;
|
|
411
|
+
removedDecisions.delete(body.id);
|
|
412
|
+
}
|
|
413
|
+
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (req.method === 'POST' && url.pathname === '/api/promote') {
|
|
417
|
+
const body = JSON.parse(await readBody(req));
|
|
418
|
+
const replacing = body.supersedes ? (0, store_1.readActive)(paths).find((d) => d.id === body.supersedes)?.decision ?? '' : '';
|
|
419
|
+
const result = (0, decision_1.promote)(String(body.id ?? ''), { cwd, keepBoth: body.keepBoth === true, supersedes: body.supersedes || undefined });
|
|
420
|
+
if (result.exitCode === 0) {
|
|
421
|
+
changed = true;
|
|
422
|
+
answer({ kind: 'rule', id: String(result.output.id) });
|
|
423
|
+
// A rule it replaced is out of force: the agent hears that as it hears a rule removed.
|
|
424
|
+
if (result.output.replaced)
|
|
425
|
+
answer({ kind: 'retire', id: String(result.output.replaced), rule: replacing, why: `Replaced by ${result.output.id}` });
|
|
426
|
+
}
|
|
427
|
+
json(res, 200, { exitCode: result.exitCode, ...result.output });
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
// Preferences: written to arbiter.json, which the CLI reads each time it acts, so nothing needs
|
|
431
|
+
// telling. Every open tab redraws from the file.
|
|
432
|
+
if (req.method === 'POST' && url.pathname === '/api/settings') {
|
|
433
|
+
const r = (0, settings_1.saveSettings)(paths, JSON.parse(await readBody(req)));
|
|
434
|
+
json(res, 200, r.ok ? { exitCode: 0, settings: r.settings } : { exitCode: 1, status: 'invalid', errors: r.errors });
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (req.method === 'GET' && url.pathname === '/api/stamp') {
|
|
438
|
+
const page = url.searchParams.get('page');
|
|
439
|
+
if (page)
|
|
440
|
+
seen(page);
|
|
441
|
+
json(res, 200, { stamp: stamp() });
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
// Which project's board this is: a second `arbiter review` asks, to open this one instead.
|
|
445
|
+
if (req.method === 'GET' && url.pathname === '/api/who') {
|
|
446
|
+
json(res, 200, { board: 'arbiter', root });
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (req.method === 'POST' && url.pathname === '/api/bye') {
|
|
450
|
+
const body = JSON.parse(await readBody(req));
|
|
451
|
+
if (body.page)
|
|
452
|
+
gone(body.page);
|
|
178
453
|
json(res, 200, { ok: true });
|
|
179
|
-
stop();
|
|
180
454
|
return;
|
|
181
455
|
}
|
|
182
456
|
res.writeHead(404);
|
|
183
457
|
res.end();
|
|
184
458
|
};
|
|
185
|
-
|
|
459
|
+
// This project's own port, or the next free one after it. Already running there: open that one,
|
|
460
|
+
// unless an agent is waiting on this sitting's handoff, which only a board of its own can give it.
|
|
461
|
+
const place = async () => {
|
|
462
|
+
const steady = steadyPort(root);
|
|
463
|
+
const ports = opts.port !== undefined ? [opts.port] : Array.from({ length: STEADY_TRIES }, (_, i) => steady + i);
|
|
464
|
+
for (const port of ports) {
|
|
465
|
+
if (await listen(server, port))
|
|
466
|
+
return started();
|
|
467
|
+
const there = await whoIsAt(port);
|
|
468
|
+
if (there && sameFolder(there) === sameFolder(root) && !opts.json) {
|
|
469
|
+
const url = `http://127.0.0.1:${port}/`;
|
|
470
|
+
say(`Arbiter review is already open for this project.\n${url}#/review\n`);
|
|
471
|
+
if (opts.open !== false)
|
|
472
|
+
openBrowser(`${url}#/review`);
|
|
473
|
+
return stop('closed');
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (opts.port === undefined && (await listen(server, 0)))
|
|
477
|
+
return started();
|
|
478
|
+
process.stderr.write(`arbiter: port ${opts.port} is in use.\n`);
|
|
479
|
+
process.exitCode = 1;
|
|
480
|
+
stop('closed');
|
|
481
|
+
};
|
|
482
|
+
const started = () => {
|
|
186
483
|
const addr = server.address();
|
|
187
484
|
const url = `http://127.0.0.1:${addr.port}/`;
|
|
188
485
|
let s;
|
|
486
|
+
let candidates;
|
|
189
487
|
try {
|
|
190
488
|
s = state();
|
|
489
|
+
candidates = (0, candidates_1.readCandidates)(paths).length;
|
|
191
490
|
}
|
|
192
491
|
catch (e) {
|
|
193
492
|
process.stderr.write(`arbiter: ${e.message}\n`);
|
|
194
493
|
process.exitCode = 1;
|
|
195
|
-
stop();
|
|
494
|
+
stop('closed');
|
|
196
495
|
return;
|
|
197
496
|
}
|
|
198
|
-
|
|
199
|
-
|
|
497
|
+
// With --json, stdout is the handoff and nothing else; the agent reads it whole.
|
|
498
|
+
say(`Arbiter review — ${s.pending.length} pending · ${candidates} candidate${candidates === 1 ? '' : 's'} · ${s.active} of ${s.cap} rules\n${url}#/review\nClose the tab, or press Ctrl-C here, to stop.\n`);
|
|
499
|
+
// Straight to what's waiting: that's what `review` is opened for.
|
|
200
500
|
if (opts.open !== false)
|
|
201
|
-
openBrowser(url);
|
|
501
|
+
openBrowser(`${url}#/review`);
|
|
502
|
+
};
|
|
503
|
+
place().catch((e) => {
|
|
504
|
+
process.stderr.write(`arbiter: ${e.message}\n`);
|
|
505
|
+
process.exitCode = 1;
|
|
506
|
+
stop('closed');
|
|
202
507
|
});
|
|
508
|
+
const say = (text) => (opts.json ? process.stderr : process.stdout).write(text);
|
|
203
509
|
// Browsers keep connections alive; close() alone would wait on them forever.
|
|
204
|
-
|
|
510
|
+
let stopping = false;
|
|
511
|
+
const stop = (how) => {
|
|
512
|
+
if (stopping)
|
|
513
|
+
return;
|
|
514
|
+
stopping = true;
|
|
515
|
+
if (closing)
|
|
516
|
+
clearTimeout(closing);
|
|
517
|
+
process.removeListener('SIGINT', onSigint);
|
|
205
518
|
server.close(async () => {
|
|
206
519
|
if (changed) {
|
|
207
520
|
const line = await (0, autopublish_1.autoPublish)(cwd);
|
|
208
521
|
if (line)
|
|
209
|
-
|
|
522
|
+
say(line + '\n');
|
|
210
523
|
}
|
|
211
|
-
|
|
524
|
+
// An agent waiting on the page gets everything waiting for it, this sitting's and any before,
|
|
525
|
+
// so it's not kept for later as well. Stopped any other way, it may not have been there to
|
|
526
|
+
// read it: it stays. Nobody waiting: the terminal hears about this sitting, and it all stays.
|
|
527
|
+
const waiting = (0, lock_1.withLock)(paths, () => {
|
|
528
|
+
const h = (0, handoff_1.readHandoff)(paths);
|
|
529
|
+
if (opts.json && h && how === 'done')
|
|
530
|
+
(0, handoff_1.clearHandoff)(paths);
|
|
531
|
+
return h;
|
|
532
|
+
});
|
|
533
|
+
if (opts.json && waiting)
|
|
534
|
+
resolve({ ...waiting, finished: how });
|
|
535
|
+
else
|
|
536
|
+
resolve((0, handoff_1.summarize)(answers, how));
|
|
212
537
|
});
|
|
213
538
|
server.closeAllConnections();
|
|
214
539
|
};
|
|
215
|
-
|
|
540
|
+
finish = stop;
|
|
541
|
+
const onSigint = () => stop('closed');
|
|
542
|
+
process.on('SIGINT', onSigint);
|
|
216
543
|
});
|
|
217
544
|
}
|
|
218
545
|
const LOCAL = /^(127\.0\.0\.1|localhost|\[::1\])(:\d+)?$/i;
|
|
@@ -230,17 +557,70 @@ function fromHere(req) {
|
|
|
230
557
|
return false;
|
|
231
558
|
}
|
|
232
559
|
}
|
|
560
|
+
/** Listens on the port if it's free: false when something else has it, or the system keeps it back. */
|
|
561
|
+
function listen(server, port) {
|
|
562
|
+
return new Promise((resolve, reject) => {
|
|
563
|
+
const failed = (e) => {
|
|
564
|
+
server.removeListener('listening', ok);
|
|
565
|
+
if (e.code === 'EADDRINUSE' || e.code === 'EACCES')
|
|
566
|
+
resolve(false);
|
|
567
|
+
else
|
|
568
|
+
reject(e);
|
|
569
|
+
};
|
|
570
|
+
const ok = () => {
|
|
571
|
+
server.removeListener('error', failed);
|
|
572
|
+
resolve(true);
|
|
573
|
+
};
|
|
574
|
+
server.once('error', failed);
|
|
575
|
+
server.once('listening', ok);
|
|
576
|
+
server.listen(port, '127.0.0.1');
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
/** The project folder of the Arbiter board on this port, or null if it's something else or nothing answers. */
|
|
580
|
+
function whoIsAt(port) {
|
|
581
|
+
return new Promise((resolve) => {
|
|
582
|
+
const req = node_http_1.default.get({ host: '127.0.0.1', port, path: '/api/who', timeout: 1000 }, (res) => {
|
|
583
|
+
let data = '';
|
|
584
|
+
res.setEncoding('utf8');
|
|
585
|
+
res.on('data', (c) => (data += c));
|
|
586
|
+
res.on('end', () => {
|
|
587
|
+
try {
|
|
588
|
+
const body = JSON.parse(data);
|
|
589
|
+
resolve(res.statusCode === 200 && body.board === 'arbiter' && body.root ? body.root : null);
|
|
590
|
+
}
|
|
591
|
+
catch {
|
|
592
|
+
resolve(null);
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
req.on('timeout', () => req.destroy());
|
|
597
|
+
req.on('error', () => resolve(null));
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* A picture is only ever a picture. An SVG opened in a tab of its own would otherwise run its scripts as
|
|
602
|
+
* the board, and a planted one could reach the API (reword a rule, which the hook then hands the agent).
|
|
603
|
+
* Sandboxed, it runs nothing and has no origin; in the board's <img> it draws as before.
|
|
604
|
+
*/
|
|
605
|
+
const PICTURE = { 'content-security-policy': "sandbox; default-src 'none'; img-src data:; style-src 'unsafe-inline'", 'x-content-type-options': 'nosniff' };
|
|
233
606
|
function json(res, status, body) {
|
|
234
607
|
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' });
|
|
235
608
|
res.end(JSON.stringify(body));
|
|
236
609
|
}
|
|
610
|
+
/** A request's body, read once: the write queue reads it before its turn, the handler after. */
|
|
611
|
+
const bodies = new WeakMap();
|
|
237
612
|
function readBody(req) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
613
|
+
let body = bodies.get(req);
|
|
614
|
+
if (!body) {
|
|
615
|
+
body = new Promise((resolve) => {
|
|
616
|
+
let data = '';
|
|
617
|
+
req.setEncoding('utf8');
|
|
618
|
+
req.on('data', (c) => (data += c));
|
|
619
|
+
req.on('end', () => resolve(data));
|
|
620
|
+
});
|
|
621
|
+
bodies.set(req, body);
|
|
622
|
+
}
|
|
623
|
+
return body;
|
|
244
624
|
}
|
|
245
625
|
function openBrowser(url) {
|
|
246
626
|
const cmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'cmd' : 'xdg-open';
|