@dibssoftware/dibs 0.0.180 → 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 +111 -1
- package/README.md +10 -8
- package/bin/dibs.mjs +379 -74
- 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 +135 -11
- 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/docs/windows.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 +113 -28
- package/src/after.mjs +127 -0
- package/src/agents.mjs +2 -1
- package/src/alerts.mjs +109 -48
- 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 +254 -86
- package/src/config.mjs +85 -14
- package/src/connect.mjs +4 -2
- package/src/cool.mjs +28 -12
- 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 +121 -7
- package/src/guard.mjs +288 -105
- 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 +51 -11
- package/src/measure.mjs +60 -14
- 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 +29 -7
- package/src/preempt.mjs +123 -15
- package/src/primer.mjs +91 -20
- package/src/prints.mjs +99 -0
- package/src/proc.mjs +11 -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 +262 -26
- 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/service.mjs +5 -1
- 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 +1082 -134
- 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 +354 -60
- package/src/work.mjs +487 -0
- package/src/worker.mjs +7 -1
package/src/remote.mjs
CHANGED
|
@@ -8,7 +8,7 @@ import path from 'node:path';
|
|
|
8
8
|
import crypto from 'node:crypto';
|
|
9
9
|
import dgram from 'node:dgram';
|
|
10
10
|
import { execFileSync } from 'node:child_process';
|
|
11
|
-
import { HOME, resCfg } from './config.mjs';
|
|
11
|
+
import { HOME, resCfg, isEnv } from './config.mjs';
|
|
12
12
|
import { identity } from './proc.mjs';
|
|
13
13
|
import { OS } from './platform.mjs';
|
|
14
14
|
import { run } from './commands.mjs';
|
|
@@ -19,8 +19,9 @@ import { rulesAbove, readRules, ruleAllows, isRuleFile, isCode, normCode, codeKe
|
|
|
19
19
|
import { projectKey } from './files.mjs';
|
|
20
20
|
import { jobSize } from './simpool.mjs';
|
|
21
21
|
import { defaultMaxWait } from './acquire.mjs';
|
|
22
|
-
import { logChange } from './owner.mjs';
|
|
22
|
+
import { logChange, AGENT_ENV } from './owner.mjs';
|
|
23
23
|
import { ondemandOf } from './ondemand.mjs';
|
|
24
|
+
import { described } from './declare.mjs';
|
|
24
25
|
|
|
25
26
|
export const REMOTE = path.join(HOME, 'remote.json');
|
|
26
27
|
export const DEFAULT_PORT = 7420;
|
|
@@ -167,7 +168,8 @@ export function macOnly(cmd, cwd) {
|
|
|
167
168
|
// machine that isn't the coordinator doesn't list them, so only the
|
|
168
169
|
// coordinator's own waiters ship on their own.
|
|
169
170
|
export function mayShip(me, cfg, cwd) {
|
|
170
|
-
|
|
171
|
+
// An environment's lease and memory live on this machine: run anywhere else, two agents could hold it at once.
|
|
172
|
+
if (me.kind !== 'run' || !me.cmd || me.res.some(r => r.startsWith('files:') || isEnv(r))) return false;
|
|
171
173
|
try { if (!ondemandOf(cfg).length) return false; } catch { return false; }
|
|
172
174
|
try { if (needsOs(cfg, me.res) === 'macos') return false; } catch { return false; }
|
|
173
175
|
if (macOnly(me.cmd, cwd) || !remoteConf(() => null) || namesPaths(me.cmd)) return false;
|
|
@@ -281,9 +283,19 @@ export async function remoteRun(o, cfg) {
|
|
|
281
283
|
say('not connected to a coordinator (dibs join), so running here');
|
|
282
284
|
return run(o, cfg);
|
|
283
285
|
}
|
|
284
|
-
if (!o.cmd || !o.cmd.length) die('nothing to run. Use: dibs run <resource> --anywhere -- <command>');
|
|
286
|
+
if (!o.cmd || !o.cmd.length) die('nothing to run. Use: dibs run <resource> --anywhere -m "..." -- <command>');
|
|
287
|
+
// A credential never leaves this machine: not in the command's text, and not in the $NAME
|
|
288
|
+
// dibs secret run set for it (a job elsewhere runs without this process's environment).
|
|
289
|
+
const inCmd = (await import('./prints.mjs')).find(o.cmd.join(' '));
|
|
290
|
+
if (inCmd.length) die(`this command has the value of the secret ${inCmd[0].name} in it, and dibs won't send that to another machine. Nothing was sent.`);
|
|
291
|
+
if (process.env.DIBS_SECRET_NAMES) {
|
|
292
|
+
const msg = `it runs with the secret ${process.env.DIBS_SECRET_NAMES} (dibs secret run), which never leaves this machine`;
|
|
293
|
+
if (o.on && o.on !== rc.name) die(`${msg}, so it can't go to ${o.on}. Nothing was sent.`);
|
|
294
|
+
say(`${msg}; running here`);
|
|
295
|
+
return run(o, cfg);
|
|
296
|
+
}
|
|
285
297
|
const res = [...new Set(o.res)];
|
|
286
|
-
if (!res.length) die('name a resource, e.g. dibs run test --anywhere -- npm test');
|
|
298
|
+
if (!res.length) die('name a resource, e.g. dibs run test --anywhere -m "run the unit tests" -- npm test');
|
|
287
299
|
const id = identity(o);
|
|
288
300
|
// The project's .dibs.json decides before anything is sent: --on a machine it
|
|
289
301
|
// doesn't allow is refused, and --anywhere stays here rather than go there.
|
|
@@ -340,7 +352,8 @@ export async function remoteRun(o, cfg) {
|
|
|
340
352
|
|
|
341
353
|
const job = await call(rc, 'POST', '/jobs', {
|
|
342
354
|
...need, machine: place.machine, cmd: o.cmd, cwdRel: wc.cwdRel, files: wc.files, bring,
|
|
343
|
-
who: id.who, agent: id.agent, label: id.label, purpose: o.purpose || '', prio: o.prio, by: o.by, est: o.est,
|
|
355
|
+
who: id.who, agent: id.agent, label: id.label, purpose: o.purpose || '', prio: o.prio, by: o.by, est: o.est, timeLimit: o.timeLimit,
|
|
356
|
+
...((AGENT_ENV.some(k => process.env[k]) || id.spid) && { byAgent: true }), // the worker runs it as an agent's, never the person's
|
|
344
357
|
disk: o.disk, ports: o.ports, repo: projectKey(process.cwd()), // its project, which one-per-project resources go by there
|
|
345
358
|
// Decided here: the worker isn't in a Claude Code session, but this end, which waits for it, may be.
|
|
346
359
|
...((o.maxWait ?? defaultMaxWait()) === Infinity ? { wait: true } : { maxWait: o.maxWait }),
|
|
@@ -503,7 +516,7 @@ async function joinWithCode(o, code) {
|
|
|
503
516
|
logChange('join', { url: found.url, code: true }); // never the code or the token
|
|
504
517
|
console.log(`joined ${found.name || 'the coordinator'} at ${found.url} as ${r.name} (coordinator dibs ${r.version})`);
|
|
505
518
|
console.log(` take jobs on this machine: dibs service install worker (or run dibs worker in a terminal)`);
|
|
506
|
-
console.log(` send a job: dibs run test --anywhere -- npm test`);
|
|
519
|
+
console.log(` send a job: dibs run test --anywhere -m "run the unit tests" -- npm test`);
|
|
507
520
|
}
|
|
508
521
|
|
|
509
522
|
// dibs join <url> <token>, or dibs join <code>: connect this machine to a coordinator.
|
|
@@ -523,7 +536,7 @@ export async function join(o) {
|
|
|
523
536
|
saveRemote(rc);
|
|
524
537
|
console.log(`joined ${rc.url} as ${rc.name} (coordinator dibs ${hello.version}, ${hello.machines.length} machine${hello.machines.length === 1 ? '' : 's'} online${hello.machines.length ? `: ${hello.machines.join(', ')}` : ''})`);
|
|
525
538
|
console.log(` take jobs on this machine: dibs service install worker (or run dibs worker in a terminal)`);
|
|
526
|
-
console.log(` send a job: dibs run test --anywhere -- npm test`);
|
|
539
|
+
console.log(` send a job: dibs run test --anywhere -m "run the unit tests" -- npm test`);
|
|
527
540
|
}
|
|
528
541
|
|
|
529
542
|
const sendsNote = (r, name) => { const s = r.sends?.[name]; return !s ? '' : s.length ? `sends only ${s.join(',')}` : 'sends nothing'; };
|
|
@@ -582,7 +595,7 @@ export async function machines(o) {
|
|
|
582
595
|
console.log(`\n${bold('Jobs sent between machines')}`);
|
|
583
596
|
for (const j of jobs)
|
|
584
597
|
console.log(` #${String(j.id).padEnd(3)} ${(j.agent || j.who).padEnd(20)} ${`${j.from} -> ${j.machine}`.padEnd(22)} ${(j.state === 'done' ? `done, exit ${j.code}` : j.state).padEnd(12)}`
|
|
585
|
-
+ ` ${dur(t - j.created).padStart(6)} ${j.res.join(',')}
|
|
598
|
+
+ ` ${dur(t - j.created).padStart(6)} ${j.res.join(',')} "${described(j)}" ${dim(j.cmd.join(' ').slice(0, 40))}`);
|
|
586
599
|
}
|
|
587
|
-
console.log(`\n${dim(`Coordinator: ${rc.url}, dibs ${r.version}. Send a job: dibs run <resource> --anywhere -- <command>`)}`);
|
|
600
|
+
console.log(`\n${dim(`Coordinator: ${rc.url}, dibs ${r.version}. Send a job: dibs run <resource> --anywhere -m "..." -- <command>`)}`);
|
|
588
601
|
}
|
|
@@ -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
|
+
}
|