@dibssoftware/dibs 0.0.181 → 0.0.195
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +103 -1
- package/README.md +10 -8
- package/bin/dibs.mjs +370 -73
- package/docs/agents.md +303 -0
- package/docs/council.md +88 -0
- package/docs/files.md +3 -3
- package/docs/guard.md +86 -11
- package/docs/json.md +211 -0
- package/docs/later.md +5 -5
- package/docs/machines.md +6 -6
- package/docs/questions.md +104 -3
- package/docs/reference.md +124 -30
- package/docs/relay.md +120 -9
- package/docs/releasing.md +7 -1
- package/docs/scheduling.md +28 -6
- package/docs/secrets.md +53 -0
- package/docs/simulators.md +3 -3
- package/package.json +1 -1
- package/skills/dibs/SKILL.md +79 -149
- package/src/about.mjs +341 -0
- package/src/acquire.mjs +111 -27
- package/src/after.mjs +127 -0
- package/src/agents.mjs +2 -1
- package/src/alerts.mjs +95 -42
- package/src/answers.mjs +2 -2
- package/src/board.mjs +21 -16
- package/src/broker.mjs +23 -7
- package/src/carry.mjs +287 -0
- package/src/codex.mjs +132 -0
- package/src/commands.mjs +243 -82
- package/src/config.mjs +85 -14
- package/src/connect.mjs +4 -2
- package/src/cool.mjs +27 -11
- package/src/council.mjs +482 -0
- package/src/cwd.mjs +9 -0
- package/src/decisions.mjs +250 -0
- package/src/declare.mjs +96 -0
- package/src/diskreport.mjs +306 -0
- package/src/diskuse.mjs +420 -0
- package/src/diskwarn.mjs +68 -13
- package/src/doctor.mjs +149 -0
- package/src/doors.mjs +547 -0
- package/src/durable.mjs +143 -0
- package/src/envs.mjs +372 -0
- package/src/events.mjs +7 -2
- package/src/files.mjs +118 -6
- package/src/guard.mjs +239 -86
- package/src/guardd.mjs +8 -1
- package/src/halt.mjs +63 -0
- package/src/handoffs.mjs +362 -0
- package/src/history.mjs +34 -4
- package/src/hogs.mjs +36 -8
- package/src/jobprogress.mjs +283 -0
- package/src/later.mjs +21 -10
- package/src/lock.mjs +33 -9
- package/src/measure.mjs +42 -11
- package/src/media.mjs +240 -0
- package/src/news.mjs +31 -0
- package/src/notes.mjs +49 -8
- package/src/notices.mjs +126 -43
- package/src/ondemand.mjs +25 -2
- package/src/outcome.mjs +163 -0
- package/src/overlap.mjs +2 -2
- package/src/override.mjs +149 -79
- package/src/owner.mjs +15 -2
- package/src/people.mjs +236 -0
- package/src/pickup.mjs +72 -0
- package/src/picture.mjs +321 -0
- package/src/plain.mjs +84 -18
- package/src/platform.mjs +19 -0
- package/src/preempt.mjs +123 -15
- package/src/primer.mjs +91 -20
- package/src/prints.mjs +99 -0
- package/src/proc.mjs +1 -1
- package/src/progress.mjs +49 -3
- package/src/proof.mjs +102 -0
- package/src/push.mjs +271 -0
- package/src/questions.mjs +328 -57
- package/src/quota.mjs +1 -1
- package/src/recap.mjs +205 -0
- package/src/relay.mjs +139 -20
- package/src/remote.mjs +23 -10
- package/src/reviewgate.mjs +268 -0
- package/src/reviews.mjs +377 -0
- package/src/route.mjs +263 -0
- package/src/rules.mjs +216 -0
- package/src/schedule.mjs +238 -44
- package/src/schema.mjs +62 -0
- package/src/scratch.mjs +2 -2
- package/src/serve.mjs +3 -2
- package/src/signed.mjs +97 -0
- package/src/simpool.mjs +46 -1
- package/src/spend.mjs +375 -0
- package/src/state.mjs +43 -17
- package/src/talk.mjs +1078 -133
- package/src/thin.sh +2 -0
- package/src/trail.mjs +290 -0
- package/src/trailnote.mjs +209 -0
- package/src/util.mjs +7 -0
- package/src/vault.mjs +435 -0
- package/src/vendors.mjs +327 -0
- package/src/version.mjs +1 -1
- package/src/web.mjs +353 -60
- package/src/work.mjs +487 -0
- package/src/worker.mjs +7 -1
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
// The review gate: the guard hook's check that a merge into main, or a push to it, has another
|
|
2
|
+
// agent's approving review (dibs review, src/reviews.mjs), in projects whose .dibs.json asks for one:
|
|
3
|
+
// { "reviewBeforeMerge": true } merges and pushes into main or master
|
|
4
|
+
// { "reviewBeforeMerge": ["main", "release"] } into these
|
|
5
|
+
// The hook loads this only for a Bash command that mentions git merge, git push or gh pr merge
|
|
6
|
+
// (src/guard.mjs), so every other call never pays for it. A project that asks for nothing costs a
|
|
7
|
+
// few file reads and no git; one that does costs one git rev-parse. Anything it can't make out
|
|
8
|
+
// lets the command through: the hook never breaks a session.
|
|
9
|
+
//
|
|
10
|
+
// src/reviews.mjs reads the same files through here, so `dibs review check` and the hook agree.
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import os from 'node:os';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { execFileSync } from 'node:child_process';
|
|
15
|
+
import { commands, expand } from './files.mjs';
|
|
16
|
+
|
|
17
|
+
// The same folder as config.mjs's HOME, worked out here because the hook loads no other module.
|
|
18
|
+
const DIBS_HOME = process.env.DIBS_HOME || path.join(os.homedir(), '.dibs');
|
|
19
|
+
export const REVIEWS = path.join(DIBS_HOME, 'reviews.json');
|
|
20
|
+
const RULE_FILE = '.dibs.json';
|
|
21
|
+
export const DEFAULT_INTO = ['main', 'master'];
|
|
22
|
+
// git answers in milliseconds; past this it's stuck on a lock or a slow disk, and the merge goes through.
|
|
23
|
+
const GIT_WAIT = 2000;
|
|
24
|
+
// A ref as a word, never one git would read as a flag.
|
|
25
|
+
const REF = /^[\w./@{}~^-]{1,200}$/;
|
|
26
|
+
|
|
27
|
+
export function git(cwd, args, wait = GIT_WAIT) {
|
|
28
|
+
try { return execFileSync('git', ['-C', cwd, ...args], { encoding: 'utf8', timeout: wait, stdio: ['ignore', 'pipe', 'ignore'] }).trim(); }
|
|
29
|
+
catch { return null; }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// The branches a merge into needs an approving review, from every .dibs.json in dir and the
|
|
33
|
+
// folders above it; null when none asks. A file that can't be read asks for nothing.
|
|
34
|
+
export function guarded(dir) {
|
|
35
|
+
let into = null;
|
|
36
|
+
for (let d = path.resolve(dir); ; d = path.dirname(d)) {
|
|
37
|
+
let r = null;
|
|
38
|
+
try { r = JSON.parse(fs.readFileSync(path.join(d, RULE_FILE), 'utf8')); } catch {}
|
|
39
|
+
const v = r?.reviewBeforeMerge;
|
|
40
|
+
const list = v === true ? DEFAULT_INTO : Array.isArray(v) ? v.filter(x => typeof x === 'string' && x) : [];
|
|
41
|
+
if (list.length) into = [...new Set([...(into || []), ...list])];
|
|
42
|
+
if (path.dirname(d) === d) return into;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Writes are renames, so reading without the lock always sees a whole file.
|
|
47
|
+
export function readReviews() {
|
|
48
|
+
try { return JSON.parse(fs.readFileSync(REVIEWS, 'utf8')).reviews || []; } catch { return []; }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Where a review of this commit stands, newest first: { approved, open, changes, older } for the
|
|
52
|
+
// sha in the repo (its git common dir), with pr standing in for the sha when only a pull request
|
|
53
|
+
// number is known. older: an approval of the same branch at another commit.
|
|
54
|
+
export function standing(list, { repo, sha = null, pr = null, branch = null }) {
|
|
55
|
+
const mine = list.filter(r => r.repo === repo).sort((a, b) => (b.answeredAt || b.at) - (a.answeredAt || a.at));
|
|
56
|
+
const hit = r => (sha ? r.head === sha || (sha.length < 40 && r.head.startsWith(sha)) : pr != null && r.pr === pr);
|
|
57
|
+
return {
|
|
58
|
+
approved: mine.find(r => hit(r) && r.state === 'approved') || null,
|
|
59
|
+
open: mine.find(r => hit(r) && r.state === 'open') || null,
|
|
60
|
+
changes: mine.find(r => hit(r) && r.state === 'changes') || null,
|
|
61
|
+
older: branch ? mine.find(r => !hit(r) && r.branch === branch && r.state === 'approved') || null : null,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const short = sha => String(sha || '').slice(0, 7);
|
|
66
|
+
const rid = r => `r${r.id}`;
|
|
67
|
+
const word = s => (/^[\w.,/@:=+-]+$/.test(s) ? s : `'${String(s).replace(/'/g, "'\\''")}'`);
|
|
68
|
+
|
|
69
|
+
// What one git or gh command merges or pushes: { dir, kind: 'git' | 'gh', refs: [...], pr, matchHead },
|
|
70
|
+
// { dir, kind: 'push', remote, specs, all, tags }, or null when it merges or pushes nothing new
|
|
71
|
+
// (--abort, --continue, --dry-run, --delete) or is neither.
|
|
72
|
+
const GIT_VALUED = new Set(['-C', '-c', '--git-dir', '--work-tree', '--namespace', '--exec-path', '--config-env']);
|
|
73
|
+
const MERGE_VALUED = new Set(['-m', '-F', '-s', '-X', '--message', '--file', '--strategy', '--strategy-option', '--cleanup', '--into-name']);
|
|
74
|
+
const PUSH_VALUED = new Set(['-o', '--push-option', '--repo', '--receive-pack', '--exec']);
|
|
75
|
+
const GH_VALUED = new Set(['-b', '--body', '-F', '--body-file', '-t', '--subject', '-A', '--author-email', '--match-head-commit', '-R', '--repo']);
|
|
76
|
+
function mergeOf(name, args, dir) {
|
|
77
|
+
const t = args.map(w => (w.opaque ? null : w.text));
|
|
78
|
+
if (name === 'git') {
|
|
79
|
+
let i = 0;
|
|
80
|
+
for (; i < t.length; i++) {
|
|
81
|
+
const a = t[i];
|
|
82
|
+
if (a === '-C') { const p = t[++i]; if (p == null) return { unknown: true }; dir = dir && path.resolve(dir, expand(p)); continue; }
|
|
83
|
+
if (a == null) return null;
|
|
84
|
+
if (GIT_VALUED.has(a)) { i++; continue; }
|
|
85
|
+
if (a.startsWith('-')) continue;
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
if (t[i] === 'push') return pushOf(t.slice(i + 1), dir);
|
|
89
|
+
if (t[i] !== 'merge') return null;
|
|
90
|
+
const rest = t.slice(i + 1);
|
|
91
|
+
if (rest.some(a => ['--abort', '--continue', '--quit', '--skip'].includes(a))) return null;
|
|
92
|
+
const refs = [];
|
|
93
|
+
for (let k = 0; k < rest.length; k++) {
|
|
94
|
+
const a = rest[k];
|
|
95
|
+
if (a == null) return { unknown: true, dir };
|
|
96
|
+
if (a === '--') { refs.push(...rest.slice(k + 1)); break; }
|
|
97
|
+
if (MERGE_VALUED.has(a)) { k++; continue; }
|
|
98
|
+
if (a.startsWith('-') && a !== '-') continue;
|
|
99
|
+
refs.push(a === '-' ? '@{-1}' : a);
|
|
100
|
+
}
|
|
101
|
+
if (refs.some(r => r == null)) return { unknown: true, dir };
|
|
102
|
+
return { dir, kind: 'git', refs: refs.length ? refs : ['@{upstream}'] };
|
|
103
|
+
}
|
|
104
|
+
if (name === 'gh') {
|
|
105
|
+
if (t[0] !== 'pr' || t[1] !== 'merge') return null;
|
|
106
|
+
const rest = t.slice(2);
|
|
107
|
+
let target = null, matchHead = null;
|
|
108
|
+
for (let k = 0; k < rest.length; k++) {
|
|
109
|
+
const a = rest[k];
|
|
110
|
+
if (a == null) return { unknown: true, dir };
|
|
111
|
+
if (a === '--match-head-commit') { matchHead = rest[++k]; continue; }
|
|
112
|
+
if (a.startsWith('--match-head-commit=')) { matchHead = a.slice(20); continue; }
|
|
113
|
+
if (GH_VALUED.has(a)) { k++; continue; }
|
|
114
|
+
if (a.startsWith('-')) continue;
|
|
115
|
+
target ??= a;
|
|
116
|
+
}
|
|
117
|
+
const pr = target && (/^#?(\d+)$/.exec(target)?.[1] || /\/pull\/(\d+)/.exec(target)?.[1]);
|
|
118
|
+
return { dir, kind: 'gh', refs: matchHead ? [] : pr ? [] : [target || 'HEAD'], pr: pr ? Number(pr) : null, matchHead };
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// git push's words after push: the remote, the refspecs, and --all or --mirror (every branch).
|
|
124
|
+
function pushOf(rest, dir) {
|
|
125
|
+
const pos = [];
|
|
126
|
+
let all = false, tags = false;
|
|
127
|
+
for (let k = 0; k < rest.length; k++) {
|
|
128
|
+
const a = rest[k];
|
|
129
|
+
if (a == null) return { unknown: true, dir };
|
|
130
|
+
if (a === '--') { pos.push(...rest.slice(k + 1)); break; }
|
|
131
|
+
if (['-d', '--delete', '-n', '--dry-run'].includes(a)) return null;
|
|
132
|
+
if (['--all', '--mirror', '--branches'].includes(a)) { all = true; continue; }
|
|
133
|
+
if (a === '--tags') { tags = true; continue; }
|
|
134
|
+
if (PUSH_VALUED.has(a)) { k++; continue; }
|
|
135
|
+
if (a.startsWith('-')) continue;
|
|
136
|
+
pos.push(a);
|
|
137
|
+
}
|
|
138
|
+
if (pos.some(x => x == null)) return { unknown: true, dir };
|
|
139
|
+
return { dir, kind: 'push', remote: pos[0] || null, specs: pos.slice(1), all, tags };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const OFF = /^DIBS_GUARD=(0|off|false|no)$/;
|
|
143
|
+
|
|
144
|
+
// The hook's answer for a Bash call: a deny naming the command that asks for a review, or null.
|
|
145
|
+
// split: src/guard.mjs's shell reader, passed in so this module loads nothing more of the guard.
|
|
146
|
+
export function gate(input, split) {
|
|
147
|
+
const cmd = input?.tool_input?.command;
|
|
148
|
+
const segs = typeof cmd === 'string' && split(cmd);
|
|
149
|
+
if (!segs) return null;
|
|
150
|
+
let off = false;
|
|
151
|
+
for (const { seg, name, args, dir } of commands(segs, input.cwd || null)) {
|
|
152
|
+
// DIBS_GUARD=off in front of the command (or env DIBS_GUARD=off), or exported earlier on the line, skips it, as it does the guard.
|
|
153
|
+
if (name === 'export' && args.some(w => !w.opaque && OFF.test(w.text))) off = true;
|
|
154
|
+
if (off || (name !== 'git' && name !== 'gh')) continue;
|
|
155
|
+
const words = seg.filter(w => !w.redir);
|
|
156
|
+
const at = words.findIndex(w => !w.opaque && path.basename(w.text) === name);
|
|
157
|
+
if (words.slice(0, Math.max(0, at)).some(w => !w.opaque && OFF.test(w.text))) continue;
|
|
158
|
+
const m = mergeOf(name, args, dir);
|
|
159
|
+
if (!m?.dir) continue;
|
|
160
|
+
const into = guarded(m.dir);
|
|
161
|
+
if (!into) continue;
|
|
162
|
+
const why = refusal(m, into, input.cwd);
|
|
163
|
+
if (why) return { hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny', permissionDecisionReason: why } };
|
|
164
|
+
}
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const RULE = '(.dibs.json "reviewBeforeMerge")';
|
|
169
|
+
|
|
170
|
+
// The refusal for one commit: what's waiting, what was asked for, or the command that asks.
|
|
171
|
+
// act: "merges into main" or "pushes to main"; verb: what to do once it's approved.
|
|
172
|
+
function denier(act, verb, dir, cwd) {
|
|
173
|
+
const head = `dibs: ${act} here need another agent's approving review first ${RULE}`;
|
|
174
|
+
const repoFlag = cwd && path.resolve(dir) === path.resolve(cwd) ? '' : ` --repo ${word(dir)}`;
|
|
175
|
+
return (what, ref, s, extra = '') => {
|
|
176
|
+
if (s.open) return `${head}, and ${what} is waiting for one: ${rid(s.open)}, asked of ${s.open.reviewer?.name || 'any agent'}. `
|
|
177
|
+
+ `Its answer reaches you like any message; ${verb} once it approves. Where it stands: dibs review list`;
|
|
178
|
+
const was = s.changes ? `${s.changes.verdictBy} asked for changes in ${rid(s.changes)}${s.changes.verdictNote ? ` ("${String(s.changes.verdictNote).slice(0, 200)}")` : ''}. Make them, commit, and ask again`
|
|
179
|
+
: s.older ? `${rid(s.older)} approved it at ${short(s.older.head)}, and it has moved since. Ask again for what it is now` : 'Ask for one';
|
|
180
|
+
return `${head}, and ${what} has none. ${was}:\n dibs review request ${ref.startsWith('<') ? ref : word(ref)}${repoFlag}${extra} -m "what to look at"\n`
|
|
181
|
+
+ `An agent that knows the area reviews it; its answer reaches you like any message. ${verb[0].toUpperCase()}${verb.slice(1)} once it approves.`;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// A push that updates a guarded branch: each commit it puts there needs an approval, or has to be
|
|
186
|
+
// what the remote already has, or a merge whose merged-in commits were approved (git merge of an
|
|
187
|
+
// approved branch makes one). Costs a git rev-parse, plus a look at the commit's parents and the
|
|
188
|
+
// remote's copy only when the commit itself isn't approved.
|
|
189
|
+
function pushRefusal(m, into, cwd) {
|
|
190
|
+
const top = git(m.dir, ['rev-parse', '--path-format=absolute', '--git-common-dir', '--symbolic-full-name', 'HEAD']);
|
|
191
|
+
if (!top) return null;
|
|
192
|
+
const [repo, headRef = ''] = top.split('\n');
|
|
193
|
+
const onto = headRef.replace(/^refs\/heads\//, '');
|
|
194
|
+
const remote = m.remote && REF.test(m.remote) && !m.remote.includes('/') ? m.remote : 'origin';
|
|
195
|
+
let targets = [];
|
|
196
|
+
if (m.all) targets = into.map(b => ({ src: `refs/heads/${b}`, dst: b }));
|
|
197
|
+
else if (!m.specs.length) { if (!m.tags && into.includes(onto)) targets = [{ src: 'HEAD', dst: onto }]; }
|
|
198
|
+
else {
|
|
199
|
+
for (const spec of m.specs) {
|
|
200
|
+
const x = spec.replace(/^\+/, '');
|
|
201
|
+
const [src, dst = src] = x.includes(':') ? x.split(':') : [x, x === 'HEAD' ? onto : x];
|
|
202
|
+
if (!src) continue; // :main deletes
|
|
203
|
+
targets.push({ src, dst: (dst === 'HEAD' ? onto : dst).replace(/^refs\/heads\//, '') });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
targets = targets.filter(x => into.includes(x.dst) && REF.test(x.src));
|
|
207
|
+
if (!targets.length) return null;
|
|
208
|
+
// --all names branches that may not exist here: only the ones that do are pushed.
|
|
209
|
+
const found = m.all ? (git(m.dir, ['for-each-ref', '--format=%(refname)', ...targets.map(x => x.src)]) || '').split('\n') : null;
|
|
210
|
+
if (found) targets = targets.filter(x => found.includes(x.src));
|
|
211
|
+
if (!targets.length) return null;
|
|
212
|
+
const out = git(m.dir, ['rev-parse', ...targets.map(x => `${x.src}^{commit}`)]);
|
|
213
|
+
if (!out) return null;
|
|
214
|
+
const shas = out.split('\n');
|
|
215
|
+
const list = readReviews();
|
|
216
|
+
const approved = sha => !!standing(list, { repo, sha }).approved;
|
|
217
|
+
for (let k = 0; k < targets.length; k++) {
|
|
218
|
+
const { src, dst } = targets[k], sha = shas[k];
|
|
219
|
+
if (approved(sha)) continue;
|
|
220
|
+
const theirs = git(m.dir, ['for-each-ref', '--format=%(objectname)', `refs/remotes/${remote}/${dst}`]);
|
|
221
|
+
if (theirs === sha) continue;
|
|
222
|
+
const parents = (git(m.dir, ['rev-list', '--parents', '-n', '1', sha]) || '').split(' ').slice(1);
|
|
223
|
+
if (parents.length > 1 && parents.slice(1).every(approved)) continue;
|
|
224
|
+
const name = src === 'HEAD' ? onto : src.replace(/^refs\/heads\//, '');
|
|
225
|
+
const branch = /^[\w./-]+$/.test(name) && !/^[0-9a-f]{7,40}$/.test(name) ? name : null;
|
|
226
|
+
const s = standing(list, { repo, sha, branch });
|
|
227
|
+
// Commits made on main itself are reviewed as the range the remote doesn't have yet.
|
|
228
|
+
const ref = branch && !into.includes(branch) ? branch : theirs ? `${remote}/${dst}..${name}` : `<from>..${name}`;
|
|
229
|
+
return denier(`pushes to ${dst}`, 'push', m.dir, cwd)(`${dst} at ${short(sha)}`, ref, s);
|
|
230
|
+
}
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Why this merge may not go ahead yet, or null when it may. cwd: where the agent's shell starts.
|
|
235
|
+
function refusal(m, into, cwd) {
|
|
236
|
+
if (m.unknown) return `dibs: merges and pushes into ${into.join(' or ')} here need another agent's approving review first ${RULE}, `
|
|
237
|
+
+ 'and this one names what it merges with a $variable or $(...), so dibs can\'t tell whether it has one. Name the branch or commit as plain words and run it again.';
|
|
238
|
+
if (m.kind === 'push') return pushRefusal(m, into, cwd);
|
|
239
|
+
const refs = m.refs.filter(r => REF.test(r));
|
|
240
|
+
if (refs.length !== m.refs.length) return null;
|
|
241
|
+
const out = git(m.dir, ['rev-parse', '--path-format=absolute', '--git-common-dir', ...refs.map(r => `${r}^{commit}`), '--symbolic-full-name', 'HEAD']);
|
|
242
|
+
if (!out) return null;
|
|
243
|
+
const [repo, ...rest] = out.split('\n');
|
|
244
|
+
const shas = rest.slice(0, refs.length);
|
|
245
|
+
const onto = (rest[refs.length] || '').replace(/^refs\/heads\//, '');
|
|
246
|
+
// git merge goes into the branch checked out; gh pr merge into the pull request's base, which only
|
|
247
|
+
// GitHub knows, so it's taken to be one that needs a review.
|
|
248
|
+
if (m.kind === 'git' && !into.includes(onto)) return null;
|
|
249
|
+
const target = into.includes(onto) ? onto : into[0];
|
|
250
|
+
const list = readReviews();
|
|
251
|
+
const deny = denier(`merges into ${target}`, 'merge', m.dir, cwd);
|
|
252
|
+
if (m.kind === 'gh' && m.pr != null) {
|
|
253
|
+
const s = standing(list, { repo, pr: m.pr });
|
|
254
|
+
return s.approved ? null : deny(`pull request #${m.pr}`, onto && !into.includes(onto) ? onto : '<its branch>', s, ` --pr ${m.pr}`);
|
|
255
|
+
}
|
|
256
|
+
if (m.kind === 'gh' && m.matchHead) {
|
|
257
|
+
if (!/^[0-9a-f]{7,40}$/i.test(m.matchHead)) return null;
|
|
258
|
+
const s = standing(list, { repo, sha: m.matchHead.toLowerCase() });
|
|
259
|
+
return s.approved ? null : deny(short(m.matchHead), m.matchHead, s);
|
|
260
|
+
}
|
|
261
|
+
for (let k = 0; k < refs.length; k++) {
|
|
262
|
+
const name = refs[k] === 'HEAD' ? onto : refs[k];
|
|
263
|
+
const branch = /^[\w./-]+$/.test(name || '') && !/^[0-9a-f]{7,40}$/.test(name) ? name : null;
|
|
264
|
+
const s = standing(list, { repo, sha: shas[k], branch });
|
|
265
|
+
if (!s.approved) return deny(`${branch || refs[k]} at ${short(shas[k])}`, name || refs[k], s);
|
|
266
|
+
}
|
|
267
|
+
return null;
|
|
268
|
+
}
|
package/src/reviews.mjs
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
// dibs review: one agent asks another to look over a change before it lands, so the person isn't
|
|
2
|
+
// the only check. The author names a branch or a commit range in a repo and what to look at; dibs
|
|
3
|
+
// picks a reviewer among the agents running now (never the author's own session): one that knows
|
|
4
|
+
// the area first (dibs who knows, src/route.mjs, when this dibs has it), then one working in the
|
|
5
|
+
// same repo, then one listening, so it hears the request within seconds. The request reaches it as
|
|
6
|
+
// any message from another agent does, signed by the author, never as the person's. The reviewer
|
|
7
|
+
// answers with approve or changes and notes; the author hears the answer the same way.
|
|
8
|
+
//
|
|
9
|
+
// An approval covers the commit it was asked about. A project whose .dibs.json says
|
|
10
|
+
// "reviewBeforeMerge" gets the guard hook's check (src/reviewgate.mjs): a merge into main, or a push to it, of a
|
|
11
|
+
// commit nobody approved is turned away with the command that asks for a review.
|
|
12
|
+
//
|
|
13
|
+
// Reviews keep their own file and lock (reviews.json), like handoffs: nothing here decides who
|
|
14
|
+
// runs next. The hook reads the file without the lock; writes are renames.
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import { HOME } from './config.mjs';
|
|
18
|
+
import { logChange } from './owner.mjs';
|
|
19
|
+
import { makeAlive, sessionFiles } from './proc.mjs';
|
|
20
|
+
import { whoAmI, send, messageTo, othersNow, delivery, unreachable } from './talk.mjs';
|
|
21
|
+
import { cwdOf } from './about.mjs';
|
|
22
|
+
import { underLock, commit } from './lock.mjs';
|
|
23
|
+
import { REVIEWS as FILE, git, guarded, standing, DEFAULT_INTO } from './reviewgate.mjs';
|
|
24
|
+
import { expand } from './files.mjs';
|
|
25
|
+
import { minutes } from './plain.mjs';
|
|
26
|
+
import { bold, dim, columns } from './style.mjs';
|
|
27
|
+
import { die, now, say, fakeFull } from './util.mjs';
|
|
28
|
+
import { readJSON } from './durable.mjs';
|
|
29
|
+
|
|
30
|
+
const LOCK = path.join(HOME, 'reviews.lock');
|
|
31
|
+
// Writes take milliseconds: a live holder past this is stuck on something else, and the caller hears so.
|
|
32
|
+
const LOCK_WAIT = 15e3;
|
|
33
|
+
// An open request nobody answered in a week is stale.
|
|
34
|
+
const OPEN_FOR = 7 * 864e5;
|
|
35
|
+
// Two weeks of answers: an approval has to outlast the wait for a merge, and the list stays short.
|
|
36
|
+
const KEEP_FOR = 14 * 864e5;
|
|
37
|
+
const KEEP_DONE = 300;
|
|
38
|
+
// What to look at is a few sentences, as a message is.
|
|
39
|
+
const MAX_NOTE = 4000;
|
|
40
|
+
// The changed files kept with a request, for the list and for picking who knows them.
|
|
41
|
+
const FILES_KEPT = 100;
|
|
42
|
+
// The list shows this many of the most recently answered.
|
|
43
|
+
const RECENT = 5;
|
|
44
|
+
// The board and the page show answers this recent beside the open requests.
|
|
45
|
+
const SHOWN_FOR = 864e5;
|
|
46
|
+
// A commit as a request names it: long enough to stay unique in any repo an agent works in.
|
|
47
|
+
const SHA_SHOWN = 12;
|
|
48
|
+
|
|
49
|
+
const USAGE = `dibs review request [<branch>|<from>..<to>] [-m "what to look at"] [--to <agent>] [--repo <dir>] [--pr <n>]
|
|
50
|
+
ask another agent to review a change (no branch: the one checked out, against main);
|
|
51
|
+
dibs picks one that knows the area and is running, and you hear its answer
|
|
52
|
+
dibs review approve <id> ["notes"] it's good to merge; its author hears your notes
|
|
53
|
+
dibs review changes <id> "what to change" not yet; its author hears what to change
|
|
54
|
+
dibs review cancel <id> drop a request
|
|
55
|
+
dibs review list [--all] [--json] open requests and recent answers (--all: every answer kept)
|
|
56
|
+
dibs review check [<branch>] [--repo <dir>] exit 0 if that commit has an approving review, 1 if not`;
|
|
57
|
+
|
|
58
|
+
const OPEN = 'open', APPROVED = 'approved', CHANGES = 'changes';
|
|
59
|
+
|
|
60
|
+
function locked(fn) {
|
|
61
|
+
let lost = [];
|
|
62
|
+
const r = underLock(LOCK, h => {
|
|
63
|
+
const s = load();
|
|
64
|
+
const before = JSON.stringify(s);
|
|
65
|
+
lost = reap(s);
|
|
66
|
+
const out = fn(s);
|
|
67
|
+
if (JSON.stringify(s) !== before) commit(h, FILE, JSON.stringify(s, null, 1), { fake: () => fakeFull('state') });
|
|
68
|
+
return out;
|
|
69
|
+
}, { wait: LOCK_WAIT, files: [FILE] });
|
|
70
|
+
if (!r) die(`${LOCK} is held by a dibs that's still running, and stayed held for ${LOCK_WAIT / 1000}s. Nothing was changed: run the same command again.`, 75);
|
|
71
|
+
// Told after the lock: a message takes the messages lock.
|
|
72
|
+
for (const x of lost) if (x.from) tryTell(x.from, `${x.gone} was reviewing ${x.id} (${what(x)}), and its session ended before it answered. `
|
|
73
|
+
+ `Ask again and dibs picks someone else: dibs review request ${x.ref}${x.pr != null ? ` --pr ${x.pr}` : ''}`);
|
|
74
|
+
return r.out;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function load() {
|
|
78
|
+
let s = {};
|
|
79
|
+
try { s = readJSON(FILE); } catch (e) {
|
|
80
|
+
if (e.code !== 'ENOENT') say(`${FILE} was unreadable (${e.message}); starting with no reviews.`);
|
|
81
|
+
}
|
|
82
|
+
s.seq ||= 0; s.reviews ||= [];
|
|
83
|
+
return s;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// An open request whose reviewer's Claude Code session is gone has no reviewer any more, and its
|
|
87
|
+
// author is told. Open ones expire after OPEN_FOR; answered ones stay KEEP_FOR. Returns the ones it
|
|
88
|
+
// took a reviewer from.
|
|
89
|
+
function reap(s, t = now()) {
|
|
90
|
+
const waiting = s.reviews.filter(x => x.state === OPEN && x.reviewer?.session && x.reviewer.agentId === 'main');
|
|
91
|
+
const lost = [];
|
|
92
|
+
if (waiting.length) {
|
|
93
|
+
const files = sessionFiles();
|
|
94
|
+
const up = makeAlive(files.map(f => f.pid));
|
|
95
|
+
const live = new Set(files.filter(f => up(f.pid)).map(f => f.sessionId));
|
|
96
|
+
for (const x of waiting) {
|
|
97
|
+
if (live.has(x.reviewer.session)) continue;
|
|
98
|
+
lost.push({ ...x, gone: x.reviewer.name });
|
|
99
|
+
(x.history ||= []).push({ state: 'unassigned', by: x.reviewer.name, why: 'its session ended', at: t });
|
|
100
|
+
x.reviewer = null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
for (const x of s.reviews) if (x.state === OPEN && t - x.at > OPEN_FOR) Object.assign(x, { state: 'expired', answeredAt: t });
|
|
104
|
+
const closed = s.reviews.filter(x => x.state !== OPEN && t - (x.answeredAt || x.at) < KEEP_FOR).slice(-KEEP_DONE);
|
|
105
|
+
s.reviews = s.reviews.filter(x => x.state === OPEN || closed.includes(x));
|
|
106
|
+
return lost;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const short = sha => String(sha || '').slice(0, SHA_SHOWN);
|
|
110
|
+
const what = x => (x.branch ? `${x.branch} at ${short(x.head).slice(0, 7)}` : `${short(x.base).slice(0, 7)}..${short(x.head).slice(0, 7)}`);
|
|
111
|
+
const word = s => (/^[\w.,/@:=+~^-]+$/.test(s) ? s : `'${String(s).replace(/'/g, "'\\''")}'`);
|
|
112
|
+
const nameOf = me => me?.name || 'the person';
|
|
113
|
+
function tryTell(to, text) { try { messageTo(to, text); } catch {} }
|
|
114
|
+
const sameAgent = (a, b) => !!a?.session && a.session === b?.session;
|
|
115
|
+
|
|
116
|
+
const idOf = arg => {
|
|
117
|
+
const n = Number(String(arg ?? '').replace(/^r/i, ''));
|
|
118
|
+
return arg && Number.isInteger(n) && n > 0 ? n : die('which review? Its id is the r number: dibs review approve r3 "looks right". Open ones: dibs review list');
|
|
119
|
+
};
|
|
120
|
+
const rid = x => `r${x.id}`;
|
|
121
|
+
const refuse = (err, code = 1) => ({ err, code });
|
|
122
|
+
const said = r => (r.err ? die(r.err, r.code) : r);
|
|
123
|
+
|
|
124
|
+
export async function review(args) {
|
|
125
|
+
const sub = args[0];
|
|
126
|
+
if (sub === 'request' || sub === 'ask') return request(args.slice(1));
|
|
127
|
+
if (sub === 'approve') return answer(args, APPROVED);
|
|
128
|
+
if (sub === 'changes') return answer(args, CHANGES);
|
|
129
|
+
if (sub === 'cancel') return cancel(args);
|
|
130
|
+
if (sub === 'check') return check(args.slice(1));
|
|
131
|
+
if (sub === 'help' || sub === '-h' || sub === '--help') return console.log(USAGE);
|
|
132
|
+
if (!sub || sub === 'list' || sub.startsWith('--')) return list(args.filter(a => a !== 'list'));
|
|
133
|
+
die(`unknown dibs review ${sub}.\n${USAGE}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function flags(args, takes) {
|
|
137
|
+
const o = { words: [] };
|
|
138
|
+
for (let i = 0; i < args.length; i++) {
|
|
139
|
+
const a = args[i], next = () => args[++i] ?? die(`${a} needs a value.\n${USAGE}`);
|
|
140
|
+
if (a === '-m' || a === '--msg') { if (!takes.includes('m')) die(`unknown flag ${a}.\n${USAGE}`); o.note = next().trim(); }
|
|
141
|
+
else if (a === '--to' && takes.includes('to')) o.to = next();
|
|
142
|
+
else if (a === '--repo') o.repo = path.resolve(expand(next()));
|
|
143
|
+
else if (a === '--pr' && takes.includes('pr')) { const n = Number(String(next()).replace(/^#/, '')); o.pr = Number.isInteger(n) && n > 0 ? n : die('--pr takes the pull request\'s number: --pr 42'); }
|
|
144
|
+
else if (a === '--json') o.json = true;
|
|
145
|
+
else if (a === '--all' && takes.includes('all')) o.all = true;
|
|
146
|
+
else if (a.startsWith('-') && a.length > 1 && !a.includes('..')) die(`unknown flag ${a}.\n${USAGE}`);
|
|
147
|
+
else o.words.push(a);
|
|
148
|
+
}
|
|
149
|
+
return o;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// What a request names, read from git in dir: { repo (the git common dir, the same for every
|
|
153
|
+
// worktree), root, branch, base, head, ref, files, stat }. Dies saying why when it can't.
|
|
154
|
+
function resolve(dir, ref) {
|
|
155
|
+
const top = git(dir, ['rev-parse', '--path-format=absolute', '--git-common-dir', '--show-toplevel']);
|
|
156
|
+
if (!top) die(`${dir} isn't in a git repo. Run this in the repo, or say which: --repo <dir>`);
|
|
157
|
+
const [repo, root] = top.split('\n');
|
|
158
|
+
const sha = r => (/^[\w./@{}~^-]{1,200}$/.test(r) ? git(dir, ['rev-parse', '--verify', '--quiet', `${r}^{commit}`]) : null)
|
|
159
|
+
|| die(`no commit ${r} in ${root}. Name a branch, a commit or a range: dibs review request fix-login, or main..fix-login`);
|
|
160
|
+
const isBranch = r => !!git(dir, ['rev-parse', '--verify', '--quiet', `refs/heads/${r}`]);
|
|
161
|
+
let base, head, branch, into = null;
|
|
162
|
+
if (ref && ref.includes('..')) {
|
|
163
|
+
const three = ref.includes('...');
|
|
164
|
+
const [a, b = ''] = ref.split(three ? '...' : '..');
|
|
165
|
+
head = sha(b || 'HEAD');
|
|
166
|
+
base = three ? git(dir, ['merge-base', sha(a || 'HEAD'), head]) : sha(a || 'HEAD');
|
|
167
|
+
branch = b && isBranch(b) ? b : null;
|
|
168
|
+
} else {
|
|
169
|
+
if (!ref) ref = git(dir, ['symbolic-ref', '--quiet', '--short', 'HEAD']) || 'HEAD';
|
|
170
|
+
head = sha(ref);
|
|
171
|
+
branch = isBranch(ref) ? ref : null;
|
|
172
|
+
into = (guarded(root) || DEFAULT_INTO).find(isBranch) || null;
|
|
173
|
+
if (!into) die(`there's no main or master here to compare ${ref} with. Name what to review as a range: dibs review request <from>..${word(ref)}`);
|
|
174
|
+
base = git(dir, ['merge-base', into, head]);
|
|
175
|
+
if (base === head) die(`${ref} has nothing ${into} doesn't, so there's nothing to review against it. Name a range instead: dibs review request ${into}~3..${into}`);
|
|
176
|
+
}
|
|
177
|
+
if (!base) die(`${ref} and its base share no history, so there's no change to review. Name a range: dibs review request <from>..<to>`);
|
|
178
|
+
const files = (git(dir, ['diff', '--name-only', base, head]) || '').split('\n').filter(Boolean);
|
|
179
|
+
if (!files.length) die(`${ref || 'that'} changes no files against ${short(base).slice(0, 7)}, so there's nothing to review.`);
|
|
180
|
+
const stat = (git(dir, ['diff', '--shortstat', base, head]) || '').trim();
|
|
181
|
+
return { repo, root, branch, base, head, ref: ref || (branch ?? `${short(base)}..${short(head)}`), files, stat, into };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// The folder the changed files share, as an absolute path: what "who knows" is asked about.
|
|
185
|
+
function area(info) {
|
|
186
|
+
const dirs = info.files.map(f => path.dirname(f).split('/'));
|
|
187
|
+
let common = dirs[0] || [];
|
|
188
|
+
for (const d of dirs) { let k = 0; while (k < common.length && d[k] === common[k]) k++; common = common.slice(0, k); }
|
|
189
|
+
return path.join(info.root, ...common.filter(x => x && x !== '.'));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Who should review: an agent running now that isn't the author, best first. skip: names that
|
|
193
|
+
// already had it. Returns { name, why } or null when nobody can.
|
|
194
|
+
async function pick(info, args, skip = new Set()) {
|
|
195
|
+
const others = othersNow('review', args).filter(a => a.name && !skip.has(a.name) && (a.reach === 'listening' || a.reach === 'working'));
|
|
196
|
+
if (!others.length) return null;
|
|
197
|
+
// dibs who knows, when this dibs has it: the agents that edited, held, ran or posted about the area.
|
|
198
|
+
let routed = [];
|
|
199
|
+
try { routed = (await import('./route.mjs')).lookup(area(info), { cwd: info.root }).found || []; } catch {}
|
|
200
|
+
const routeAt = new Map(routed.map((m, i) => [m.name, i]));
|
|
201
|
+
const repos = new Map();
|
|
202
|
+
const sameRepo = a => {
|
|
203
|
+
const cwd = a.session && cwdOf(a.session);
|
|
204
|
+
if (!cwd) return false;
|
|
205
|
+
if (!repos.has(cwd)) repos.set(cwd, git(cwd, ['rev-parse', '--path-format=absolute', '--git-common-dir']));
|
|
206
|
+
return repos.get(cwd) === info.repo;
|
|
207
|
+
};
|
|
208
|
+
const key = a => [routeAt.get(a.name) ?? Infinity, sameRepo(a) ? 0 : 1, a.reach === 'listening' ? 0 : 1, a.agentId === 'main' ? 0 : 1];
|
|
209
|
+
const ranked = others.map(a => ({ a, k: key(a) })).sort((x, y) => { for (let i = 0; i < x.k.length; i++) if (x.k[i] !== y.k[i]) return x.k[i] - y.k[i]; return 0; });
|
|
210
|
+
const { a, k } = ranked[0];
|
|
211
|
+
const r = routed[k[0]];
|
|
212
|
+
const why = [Number.isFinite(k[0]) ? `knows ${path.relative(info.root, area(info)) || 'this repo'}${r?.why?.[0]?.text ? ` (${r.why[0].text})` : ''}` : '',
|
|
213
|
+
k[1] === 0 ? 'works in the same repo' : '', a.reach === 'listening' ? 'is listening' : 'is working now'].filter(Boolean).join(', ');
|
|
214
|
+
return { name: a.name, why };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async function request(args) {
|
|
218
|
+
const o = flags(args, ['m', 'to', 'pr']);
|
|
219
|
+
if (o.words.length > 1) die(`review one branch or range at a time: dibs review request fix-login -m "what to look at"\n${USAGE}`);
|
|
220
|
+
if ((o.note || '').length > MAX_NOTE) die(`what to look at is ${o.note.length} characters; keep it under ${MAX_NOTE}, or point to a file. Nothing was asked.`);
|
|
221
|
+
const info = resolve(o.repo || process.cwd(), o.words[0]);
|
|
222
|
+
const me = whoAmI('review', args);
|
|
223
|
+
if (o.to) {
|
|
224
|
+
const no = unreachable(o.to);
|
|
225
|
+
if (no) die(no, 1);
|
|
226
|
+
if (me && o.to === me.name) die("that's you. Another agent has to review it: leave out --to and dibs picks one.");
|
|
227
|
+
}
|
|
228
|
+
// Asked again for the same commit: the same request, to someone new when there's anyone (the last one never answered).
|
|
229
|
+
const was = locked(s => { const x = s.reviews.find(r => r.state === OPEN && r.repo === info.repo && r.head === info.head); return x ? { ...x } : null; });
|
|
230
|
+
const skip = new Set(was?.reviewer ? [was.reviewer.name] : []);
|
|
231
|
+
// With nobody else about, the one that had it hears it again, as a reminder.
|
|
232
|
+
const chosen = o.to ? { name: o.to, why: 'was named by the author' } : (await pick(info, args, skip)) || (skip.size ? await pick(info, args) : null);
|
|
233
|
+
const x = locked(s => {
|
|
234
|
+
let x = s.reviews.find(r => r.state === OPEN && r.repo === info.repo && r.head === info.head);
|
|
235
|
+
if (!x) {
|
|
236
|
+
x = { id: ++s.seq, repo: info.repo, root: info.root, branch: info.branch, base: info.base, head: info.head, ref: info.ref,
|
|
237
|
+
files: info.files.slice(0, FILES_KEPT), changed: info.files.length, stat: info.stat, from: me, state: OPEN, at: now(), history: [] };
|
|
238
|
+
s.reviews.push(x);
|
|
239
|
+
}
|
|
240
|
+
if (o.note) x.note = o.note;
|
|
241
|
+
if (o.pr != null) x.pr = o.pr;
|
|
242
|
+
x.reviewer = chosen ? { name: chosen.name } : null;
|
|
243
|
+
x.picked = chosen?.why || null;
|
|
244
|
+
x.history.push({ state: 'asked', by: nameOf(me), ...(chosen && { of: chosen.name }), at: now() });
|
|
245
|
+
return { ...x };
|
|
246
|
+
});
|
|
247
|
+
logChange('review request', { id: rid(x), ...(chosen && { to: chosen.name }) });
|
|
248
|
+
if (!chosen) {
|
|
249
|
+
if (o.json) return console.log(JSON.stringify({ review: shown(x), to: null }));
|
|
250
|
+
return console.log(`${rid(x)} is asked for ${what(x)}, but no other agent is working or listening here now, so nobody has it yet. `
|
|
251
|
+
+ `It waits on the board; any agent can answer it (dibs review approve ${rid(x)} "notes"), and asking again later picks one: dibs review request ${word(x.ref)}`);
|
|
252
|
+
}
|
|
253
|
+
const files = `${x.changed} file${x.changed === 1 ? '' : 's'}${x.stat ? `, ${x.stat.replace(/^\d+ files? changed,?\s*/, '')}` : ''}`;
|
|
254
|
+
const text = `Review request ${rid(x)}: ${nameOf(me)} asks you to review ${what(x)} in ${x.root} (${files}).`
|
|
255
|
+
+ `${x.note ? ` What to look at: ${x.note}` : ''}${x.pr != null ? ` It's pull request #${x.pr}.` : ''}`
|
|
256
|
+
+ `\nSee it: git -C ${word(x.root)} diff ${short(x.base)}..${short(x.head)}`
|
|
257
|
+
+ `\nAnswer: dibs review approve ${rid(x)} "what you checked", or dibs review changes ${rid(x)} "what to change". ${me ? `${me.name} hears` : 'The person sees'} your answer.`
|
|
258
|
+
+ ` dibs picked you for this (${chosen.why}). If you can't review it, say so: ${me ? `dibs say --to ${word(me.name)} "..."` : 'dibs say "..."'}`;
|
|
259
|
+
const r = send(chosen.name, text, 'review', args);
|
|
260
|
+
// The record keeps how to reach the reviewer, for its session's end and the cancel notice.
|
|
261
|
+
locked(s => { const y = s.reviews.find(z => z.id === x.id); if (y?.reviewer?.name === chosen.name) Object.assign(y.reviewer, { session: r.th.session, agentId: r.th.agentId, who: r.th.who }); });
|
|
262
|
+
if (o.json) return console.log(JSON.stringify({ review: shown(x), to: r.to }));
|
|
263
|
+
const d = delivery(r);
|
|
264
|
+
console.log(`${rid(x)} (${what(x)}) is with ${r.to}, who ${chosen.why}${d.startsWith('but ') ? ', ' : ': '}${d}`
|
|
265
|
+
+ ` Its answer reaches you like any message. Where it stands: dibs review list`);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// approve and changes: anyone but the author's own session may answer, the person included, and
|
|
269
|
+
// the author hears who did.
|
|
270
|
+
async function answer(args, verdict) {
|
|
271
|
+
const me = whoAmI('review', args);
|
|
272
|
+
const name = nameOf(me);
|
|
273
|
+
const n = idOf(args[1]);
|
|
274
|
+
const note = args.slice(2).join(' ').trim().slice(0, MAX_NOTE);
|
|
275
|
+
if (verdict === CHANGES && !note) die(`say what to change: dibs review changes r${n} "the retry loop never gives up". Nothing was answered.`);
|
|
276
|
+
const x = said(locked(s => {
|
|
277
|
+
const x = s.reviews.find(r => r.id === n);
|
|
278
|
+
if (!x) return refuse(`no review r${n}. Open ones: dibs review list`);
|
|
279
|
+
if (x.state === 'cancelled' || x.state === 'expired') return refuse(`${rid(x)} is ${x.state}. Its author can ask again: dibs review request ${word(x.ref)}`);
|
|
280
|
+
if (sameAgent(me, x.from)) return refuse(`you asked for ${rid(x)}, so another agent has to answer it. It's with ${x.reviewer?.name || 'nobody yet'}: dibs review list`);
|
|
281
|
+
Object.assign(x, { state: verdict, answeredAt: now(), verdictBy: name, verdictNote: note || null });
|
|
282
|
+
x.history.push({ state: verdict, by: name, ...(note && { note }), at: now() });
|
|
283
|
+
return { ...x };
|
|
284
|
+
}));
|
|
285
|
+
logChange(verdict === APPROVED ? 'review approve' : 'review changes', { id: rid(x) });
|
|
286
|
+
// The branch may have moved on since the request: the answer covers the commit that was asked about.
|
|
287
|
+
const tip = x.branch ? git(x.root, ['rev-parse', '--verify', '--quiet', `refs/heads/${x.branch}`]) : null;
|
|
288
|
+
const moved = tip && tip !== x.head ? ` ${x.branch} has moved to ${short(tip).slice(0, 7)} since, so this answer covers ${short(x.head).slice(0, 7)} only.` : '';
|
|
289
|
+
if (x.from && !sameAgent(me, x.from)) {
|
|
290
|
+
tryTell(x.from, verdict === APPROVED
|
|
291
|
+
? `${name} approved ${rid(x)} (${what(x)})${note ? `: ${note}${/[.!?]$/.test(note) ? '' : '.'}` : '.'}${moved} Merge when you're ready.`
|
|
292
|
+
: `${name} asked for changes in ${rid(x)} (${what(x)}): ${note}\nMake them, commit, and ask again: dibs review request ${word(x.ref)}${x.pr != null ? ` --pr ${x.pr}` : ''}`);
|
|
293
|
+
}
|
|
294
|
+
console.log(`${rid(x)} ${verdict === APPROVED ? 'approved' : 'needs changes'}.${x.from ? ` ${x.from.name} is told.` : ''}${moved}`);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function cancel(args) {
|
|
298
|
+
const me = whoAmI('review', args);
|
|
299
|
+
const n = idOf(args[1]);
|
|
300
|
+
const x = said(locked(s => {
|
|
301
|
+
const x = s.reviews.find(r => r.id === n);
|
|
302
|
+
if (!x) return refuse(`no review r${n}. Open ones: dibs review list`);
|
|
303
|
+
if (x.state !== OPEN) return refuse(`${rid(x)} is already ${x.state}.`);
|
|
304
|
+
Object.assign(x, { state: 'cancelled', answeredAt: now(), verdictBy: nameOf(me) });
|
|
305
|
+
x.history.push({ state: 'cancelled', by: nameOf(me), at: now() });
|
|
306
|
+
return { ...x };
|
|
307
|
+
}));
|
|
308
|
+
logChange('review cancel', { id: rid(x) });
|
|
309
|
+
if (x.reviewer?.session && !sameAgent(me, x.reviewer)) tryTell(x.reviewer, `${rid(x)} (${what(x)}) was cancelled by ${nameOf(me)}: no need to review it.`);
|
|
310
|
+
console.log(`${rid(x)} is cancelled.`);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// dibs review check [<branch>] [--repo dir]: what the guard hook would find, for scripts and agents.
|
|
314
|
+
function check(args) {
|
|
315
|
+
const o = flags(args, []);
|
|
316
|
+
const dir = o.repo || process.cwd();
|
|
317
|
+
const top = git(dir, ['rev-parse', '--path-format=absolute', '--git-common-dir']);
|
|
318
|
+
if (!top) die(`${dir} isn't in a git repo. Run this in the repo, or say which: --repo <dir>`);
|
|
319
|
+
const ref = o.words[0] || 'HEAD';
|
|
320
|
+
const sha = /^[\w./@{}~^-]{1,200}$/.test(ref) ? git(dir, ['rev-parse', '--verify', '--quiet', `${ref}^{commit}`]) : null;
|
|
321
|
+
if (!sha) die(`no commit ${ref} here.`);
|
|
322
|
+
const s = standing(load().reviews, { repo: top, sha, branch: ref === 'HEAD' ? null : ref });
|
|
323
|
+
const into = guarded(dir);
|
|
324
|
+
const rule = into ? `This repo asks for one before merging into ${into.join(' or ')}.` : 'This repo doesn\'t ask for one before merging.';
|
|
325
|
+
if (o.json) { console.log(JSON.stringify({ sha, approved: s.approved ? shown(s.approved) : null, open: s.open ? shown(s.open) : null, required: into })); process.exitCode = s.approved ? 0 : 1; return; }
|
|
326
|
+
if (s.approved) return console.log(`${short(sha).slice(0, 7)} is approved: ${rid(s.approved)} by ${s.approved.verdictBy}${s.approved.verdictNote ? ` ("${s.approved.verdictNote}")` : ''}. ${rule}`);
|
|
327
|
+
const why = s.open ? `${rid(s.open)} is waiting on ${s.open.reviewer?.name || 'any agent'}` : s.changes ? `${s.changes.verdictBy} asked for changes in ${rid(s.changes)}`
|
|
328
|
+
: s.older ? `${rid(s.older)} approved ${short(s.older.head).slice(0, 7)}, before it moved` : 'nobody has been asked';
|
|
329
|
+
die(`${short(sha).slice(0, 7)} has no approving review: ${why}. ${rule} Ask: dibs review request${ref === 'HEAD' ? '' : ` ${word(ref)}`}`, 1);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// What the board, the page, the phone and --json show of one: names and commits, never sessions or paths.
|
|
333
|
+
export const shown = x => ({ id: rid(x), state: x.state, project: path.basename(x.root || ''), branch: x.branch || null, base: short(x.base), head: short(x.head),
|
|
334
|
+
changed: x.changed ?? (x.files || []).length, stat: x.stat || null, note: x.note || null, pr: x.pr ?? null, from: x.from?.name || 'the person',
|
|
335
|
+
reviewer: x.reviewer?.name || null, picked: x.picked || null, at: x.at, answeredAt: x.answeredAt || null, by: x.verdictBy || null, verdict: x.verdictNote || null });
|
|
336
|
+
|
|
337
|
+
// Open requests and the answers of the last day, newest first, for the board and the page. Read
|
|
338
|
+
// without the lock; the reaping isn't saved.
|
|
339
|
+
export function reviewsNow(t = now()) {
|
|
340
|
+
if (!fs.existsSync(FILE)) return [];
|
|
341
|
+
const s = load();
|
|
342
|
+
reap(s, t);
|
|
343
|
+
return s.reviews.filter(x => x.state === OPEN || ((x.state === APPROVED || x.state === CHANGES) && t - x.answeredAt < SHOWN_FOR))
|
|
344
|
+
.sort((a, b) => (b.answeredAt || b.at) - (a.answeredAt || a.at)).map(shown);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Answers given between since and t, for dibs recap.
|
|
348
|
+
export function answeredSince(since, t = now()) {
|
|
349
|
+
if (!fs.existsSync(FILE)) return [];
|
|
350
|
+
return load().reviews.filter(x => (x.state === APPROVED || x.state === CHANGES) && x.answeredAt > since && x.answeredAt <= t).map(shown);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// One line for the board.
|
|
354
|
+
export const reviewLine = (x, t = now()) => `${x.id} ${x.branch || `${x.base.slice(0, 7)}..${x.head.slice(0, 7)}`} in ${x.project} from ${x.from}, `
|
|
355
|
+
+ (x.state === OPEN ? (x.reviewer ? `with ${x.reviewer} ${t - x.at < 6e4 ? 'since just now' : `for ${minutes(t - x.at)}`}` : 'nobody has it yet')
|
|
356
|
+
: x.state === APPROVED ? `approved by ${x.by}` : x.state === CHANGES ? `changes asked by ${x.by}` : x.state);
|
|
357
|
+
|
|
358
|
+
function list(args) {
|
|
359
|
+
const o = flags(args, ['all']);
|
|
360
|
+
const all = !!o.all;
|
|
361
|
+
if (o.words.length) die(`unknown ${o.words[0]}.\n${USAGE}`);
|
|
362
|
+
const t = now();
|
|
363
|
+
const s = locked(x => x);
|
|
364
|
+
const open = s.reviews.filter(x => x.state === OPEN).sort((a, b) => a.at - b.at);
|
|
365
|
+
const done = s.reviews.filter(x => x.state !== OPEN).sort((a, b) => (b.answeredAt || 0) - (a.answeredAt || 0));
|
|
366
|
+
if (o.json) return console.log(JSON.stringify({ reviews: [...open, ...(all ? done : done.slice(0, RECENT))].map(shown) }, null, 1));
|
|
367
|
+
if (!open.length && !done.length) return console.log('No reviews. Ask another agent to look over a change: dibs review request <branch> -m "what to look at"');
|
|
368
|
+
const out = [bold(open.length ? `${open.length} waiting for an answer:` : 'Nothing waiting for an answer.')];
|
|
369
|
+
out.push(...columns(open.map(x => [rid(x), reviewLine(shown(x), t).replace(/^r\d+ /, '') + (x.note ? dim(` (${x.note.slice(0, 80)})`) : '')])));
|
|
370
|
+
const old = all ? done : done.slice(0, RECENT);
|
|
371
|
+
if (old.length) {
|
|
372
|
+
out.push('', dim(all ? 'Answered:' : 'Answered lately:'));
|
|
373
|
+
out.push(...columns(old.map(x => [rid(x), dim(`${reviewLine(shown(x), t).replace(/^r\d+ /, '')}${x.verdictNote ? `: ${x.verdictNote.slice(0, 120)}` : ''}`)])));
|
|
374
|
+
}
|
|
375
|
+
out.push('', dim('Answer one: dibs review approve <id> "notes", or dibs review changes <id> "what to change". Everything: dibs review help'));
|
|
376
|
+
console.log(out.join('\n'));
|
|
377
|
+
}
|