hud-gateway 0.1.0 → 0.1.3
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 +5 -1
- package/bin/hud-gateway.mjs +22 -11
- package/core.js +24 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,6 +10,10 @@ npx hud-gateway --days 90 # 30 days by default; it widens to 90 and 180
|
|
|
10
10
|
npx hud-gateway --sample # an invented repo, no token: for demos and recordings
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
+
In a pnpm workspace (dependencies on `catalog:` or `workspace:`), `npx` can fail with
|
|
14
|
+
`Unsupported URL Type "catalog:"`: npm tries to read the pnpm tree. Use `pnpm dlx hud-gateway`, or run
|
|
15
|
+
`npx hud-gateway owner/repo` from outside the repo.
|
|
16
|
+
|
|
13
17
|
Read-only and zero dependencies. It uses your `gh` login (its token, read once) or `GITHUB_TOKEN` with
|
|
14
18
|
read access to Contents, Pull requests and Metadata. It lists merged PRs 100 a request, then reads reviews and files
|
|
15
19
|
only for the PRs that need them (agent-written, and fix PRs), 25 a request with four in flight, so a
|
|
@@ -17,7 +21,7 @@ only for the PRs that need them (agent-written, and fix PRs), 25 a request with
|
|
|
17
21
|
to `hud-gateway.yml` next to you, and the result opens in the Hud editor at play.hudx.io/gateway, carried
|
|
18
22
|
inside the link itself: nothing is uploaded.
|
|
19
23
|
|
|
20
|
-
Flags: `--days 30`, `--max
|
|
24
|
+
Flags: `--days 30`, `--max 500` (it stops at the newest 500 merged PRs even when that covers less than 30 days, and reports the days it covered), `--out hud-gateway.yml`, `--json out.json`, `--payload out.json`, `--no-open`, `--quiet`, `--rest`.
|
|
21
25
|
|
|
22
26
|
The three beats before the policy:
|
|
23
27
|
|
package/bin/hud-gateway.mjs
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
//
|
|
5
5
|
// npx hud-gateway # the repo behind `origin` in this folder
|
|
6
6
|
// npx hud-gateway owner/repo
|
|
7
|
-
// flags: --days 30 (widens to 90 and 180 on its own when 30 days hold too few agent PRs) --max
|
|
7
|
+
// flags: --days 30 (widens to 90 and 180 on its own when 30 days hold too few agent PRs) --max 500 (stops at the newest 500 merged PRs, even inside the window) --out hud-gateway.yml --json out.json --no-open --quiet --rest --sample (an invented repo, no token)
|
|
8
8
|
//
|
|
9
9
|
// Uses your `gh` login (its token, read once) or GITHUB_TOKEN. One GraphQL request per 50 PRs, read-only;
|
|
10
10
|
// --rest uses the REST API instead (3 to 4 requests per PR, slower).
|
|
@@ -14,7 +14,7 @@ import { execFile, execFileSync, spawn } from 'node:child_process';
|
|
|
14
14
|
import { promisify } from 'node:util';
|
|
15
15
|
import { writeFileSync } from 'node:fs';
|
|
16
16
|
import { gzipSync } from 'node:zlib';
|
|
17
|
-
import { runBacktest, runBacktestGraphQL, draftPolicy, evaluatePolicy, ruleText, ruleVerdict, toPayload, toYaml, funTime, SAFE_DAYS, REVIEW_MIN } from '../core.js';
|
|
17
|
+
import { runBacktest, runBacktestGraphQL, draftPolicy, evaluatePolicy, ruleText, ruleVerdict, toPayload, toYaml, funTime, bigTime, SAFE_DAYS, REVIEW_MIN } from '../core.js';
|
|
18
18
|
import { sampleApi, SAMPLE_SLUG } from '../sample.js';
|
|
19
19
|
|
|
20
20
|
const run = promisify(execFile);
|
|
@@ -54,13 +54,13 @@ class Line {
|
|
|
54
54
|
const bar = (done, total, w = 22) => { const f = total ? Math.round((w * done) / total) : 0; return `${C.teal}${'█'.repeat(f)}${C.dim}${'░'.repeat(w - f)}${C.off}`; };
|
|
55
55
|
|
|
56
56
|
// a number that counts up to its value (the reveal), then stays
|
|
57
|
-
async function countUp(value, unit, tail) {
|
|
57
|
+
async function countUp(value, unit, tail, digits = 0) {
|
|
58
58
|
const steps = 28;
|
|
59
|
-
const big = (v) => ` ${C.bold}${C.white}${(`${fmt(v)} ${unit}`).padEnd(18)}${C.off}`;
|
|
59
|
+
const big = (v) => ` ${C.bold}${C.white}${(`${digits ? v.toFixed(digits) : fmt(v)} ${unit}`).padEnd(18)}${C.off}`;
|
|
60
60
|
if (!ANIM) { out(`${big(value)}${tail}`); return; }
|
|
61
61
|
for (let i = 1; i <= steps; i++) {
|
|
62
62
|
const e = 1 - Math.pow(1 - i / steps, 3);
|
|
63
|
-
process.stdout.write(`\r${big(Math.round(value * e))}\x1b[K`);
|
|
63
|
+
process.stdout.write(`\r${big(digits ? value * e : Math.round(value * e))}\x1b[K`);
|
|
64
64
|
await sleep(32);
|
|
65
65
|
}
|
|
66
66
|
process.stdout.write(`\r${big(value)}${tail}\x1b[K\n`);
|
|
@@ -173,6 +173,7 @@ const shortRule = (r) => {
|
|
|
173
173
|
};
|
|
174
174
|
|
|
175
175
|
// ---------- the run ----------
|
|
176
|
+
const MAX = Number(flag('max', 500));
|
|
176
177
|
const askedDays = flag('days', null);
|
|
177
178
|
let days = Number(askedDays || 30);
|
|
178
179
|
try {
|
|
@@ -192,7 +193,7 @@ try {
|
|
|
192
193
|
};
|
|
193
194
|
const tally = () => (live.stamps ? ` ${C.dim}·${C.off} ${C.white}${fmt(Math.round(live.hours))} h${C.off} ${C.dim}waited so far, ${live.stamps} rubber stamps${C.off}` : live.agent ? ` ${C.dim}· ${live.agent} agent PRs so far${C.off}` : '');
|
|
194
195
|
const opts = {
|
|
195
|
-
days, maxPrs:
|
|
196
|
+
days, maxPrs: MAX, concurrency: 16, files: true, onRow,
|
|
196
197
|
onProgress: ({ phase, done, total }) => {
|
|
197
198
|
const el = `${C.dim}${Math.round((Date.now() - t0) / 1000)} s${C.off}`;
|
|
198
199
|
if (phase === 'list') line.set(`Listing merged PRs ${C.dim}· ${fmt(done)} found${C.off} ${el}${tally()}`);
|
|
@@ -201,6 +202,7 @@ try {
|
|
|
201
202
|
};
|
|
202
203
|
// Read 30 days first so the first number shows fast; widen only when that window holds too few agent PRs.
|
|
203
204
|
const ENOUGH = 25;
|
|
205
|
+
const capped = () => s.merged_prs >= MAX;
|
|
204
206
|
let s, how = 'GraphQL';
|
|
205
207
|
const read = async (d) => {
|
|
206
208
|
live.stamps = 0; live.hours = 0; live.agent = 0;
|
|
@@ -216,12 +218,20 @@ try {
|
|
|
216
218
|
};
|
|
217
219
|
s = await read(days);
|
|
218
220
|
for (const wider of [90, 180]) {
|
|
219
|
-
if (askedDays || s.agent_prs >= ENOUGH || days >= wider) break;
|
|
221
|
+
if (askedDays || capped() || s.agent_prs >= ENOUGH || days >= wider) break;
|
|
220
222
|
line.set(`${plural(s.agent_prs, 'agent-written PR')} in ${days} days is thin; reading ${wider} days`);
|
|
221
223
|
days = wider;
|
|
222
224
|
s = await read(days);
|
|
223
225
|
}
|
|
224
|
-
|
|
226
|
+
// the PR cap was hit before the window ended: the window is as far back as those PRs go
|
|
227
|
+
let cappedNote = '';
|
|
228
|
+
if (capped()) {
|
|
229
|
+
const oldest = Math.min(...s.rows.map((r) => new Date(r.merged_at).getTime()));
|
|
230
|
+
days = Math.max(1, Math.ceil((Date.now() - oldest) / 864e5));
|
|
231
|
+
s.days = days;
|
|
232
|
+
cappedNote = ` ${C.dim}(newest ${fmt(MAX)} PRs; --max to read more)${C.off}`;
|
|
233
|
+
}
|
|
234
|
+
line.done(`${fmt(s.merged_prs)} merged PRs · ${C.white}${fmt(s.agent_prs)} by agents${C.off} (${s.agent_share_pct}%) · last ${plural(days, 'day')}${cappedNote} ${C.dim}· ${Math.round((Date.now() - t0) / 1000)} s · ${how} · ${REQS} requests${C.off}`);
|
|
225
235
|
|
|
226
236
|
if (!s.agent_prs) {
|
|
227
237
|
out();
|
|
@@ -238,10 +248,11 @@ try {
|
|
|
238
248
|
if (stamps) {
|
|
239
249
|
rule('Approvals that changed nothing');
|
|
240
250
|
out();
|
|
241
|
-
const fun = funTime(s.wait_hours_total);
|
|
242
|
-
await countUp(
|
|
251
|
+
const fun = funTime(s.wait_hours_total), bt = bigTime(s.wait_hours_total);
|
|
252
|
+
await countUp(bt.value, bt.unit, ` ${C.dim}waited in the last ${plural(days, 'day')} for approvals that changed nothing${C.off}`, Number.isInteger(bt.value) ? 0 : 1);
|
|
253
|
+
await sleep(500);
|
|
254
|
+
out(` ${C.dim}${' '.repeat(18)} ${fmt(s.wait_hours_total)} hours · ${plural(fun.days, 'working day')}${fun.line ? ` · ${fun.line}` : ''}${C.off}`);
|
|
243
255
|
await sleep(500);
|
|
244
|
-
if (fun.line) { out(` ${C.dim}${' '.repeat(18)} ${fun.line}${C.off}`); await sleep(500); }
|
|
245
256
|
fact(plural(stamps, 'PR'), 'approved by a person with no comment, no change request and no commit after it');
|
|
246
257
|
fact(`${hrs(s.wait_hours_median)} median`, `longest #${s.longest_wait.number}, ${hrs(s.longest_wait.hours)}${noHuman ? ` · another ${plural(noHuman, 'PR')} merged with no human review at all` : ''}`);
|
|
247
258
|
} else {
|
package/core.js
CHANGED
|
@@ -495,7 +495,13 @@ const yq = (v) => (typeof v === 'string' && /^[\w./*?@ -]*$/.test(v) && !/^(true
|
|
|
495
495
|
export function toYaml(policy, autonomy = 'dry-run') {
|
|
496
496
|
const L = [];
|
|
497
497
|
L.push(`# Hud Merge Gateway policy for ${policy.repo}`, `# Rules run top to bottom; the first that matches decides where the PR goes.`, `# Edit here or at https://play.hudx.io/gateway`, '');
|
|
498
|
-
L.push(`version: ${policy.version || 1}`, `repo: ${yq(policy.repo)}`, `base: ${yq(policy.base)}`, `autonomy: ${autonomy}`, `drafted_at: ${yq(policy.drafted_at)}`, ''
|
|
498
|
+
L.push(`version: ${policy.version || 1}`, `repo: ${yq(policy.repo)}`, `base: ${yq(policy.base)}`, `autonomy: ${autonomy}`, `drafted_at: ${yq(policy.drafted_at)}`, '');
|
|
499
|
+
if (policy.production && policy.production.provider) {
|
|
500
|
+
L.push('# Production reviewer: an open GitHub Action that asks your telemetry about the services a PR touches.',
|
|
501
|
+
'# runs: your-runner (your model key and telemetry key, as repo secrets) or hud (sign in; 100 reviews a month free, then $1 per PR).',
|
|
502
|
+
'production:', ` provider: ${policy.production.provider}`, ` runs: ${policy.production.runs || 'your-runner'}`, '');
|
|
503
|
+
}
|
|
504
|
+
L.push('rules:');
|
|
499
505
|
for (const r of policy.rules) {
|
|
500
506
|
L.push(` # ${ruleText(r)}`);
|
|
501
507
|
L.push(` - kind: ${r.kind}`);
|
|
@@ -522,12 +528,13 @@ const yv = (t) => {
|
|
|
522
528
|
};
|
|
523
529
|
export function fromYaml(text) {
|
|
524
530
|
const out = { rules: [] };
|
|
525
|
-
let cur = null, id = 1;
|
|
531
|
+
let cur = null, sect = null, id = 1;
|
|
526
532
|
for (const raw of text.split('\n')) {
|
|
527
533
|
const line = raw.replace(/\s+#.*$|^\s*#.*$/, '').replace(/\s+$/, '');
|
|
528
534
|
if (!line.trim()) continue;
|
|
529
535
|
let m;
|
|
530
|
-
if ((m = line.match(/^(\w+):\s*(.*)$/))) { if (m[1] === 'rules')
|
|
536
|
+
if ((m = line.match(/^(\w+):\s*(.*)$/))) { cur = null; sect = null; if (m[1] === 'rules') continue; if (m[2] === '') { sect = m[1]; out[sect] = {}; } else out[m[1]] = yv(m[2]); continue; }
|
|
537
|
+
if (sect && (m = line.match(/^\s+(\w+):\s*(.*)$/))) { out[sect][m[1]] = yv(m[2]); continue; }
|
|
531
538
|
if ((m = line.match(/^\s+-\s+(\w+):\s*(.*)$/))) { cur = { id: `r${id++}`, on: true }; cur[m[1]] = yv(m[2]); out.rules.push(cur); continue; }
|
|
532
539
|
if (cur && (m = line.match(/^\s+(\w+):\s*(.*)$/))) { cur[m[1]] = yv(m[2]); continue; }
|
|
533
540
|
throw new Error(`Cannot read line: ${raw.trim()}`);
|
|
@@ -539,7 +546,9 @@ export function fromYaml(text) {
|
|
|
539
546
|
if (r.kind === 'checks' && !Array.isArray(r.checks)) throw new Error('A checks rule needs checks: [...]');
|
|
540
547
|
}
|
|
541
548
|
if (!out.rules.some((r) => r.kind === 'default')) out.rules.push({ id: `r${id++}`, on: true, kind: 'default', value: 'human', source: 'added: no default rule' });
|
|
542
|
-
|
|
549
|
+
const PROV = ['datadog', 'clickhouse', 'dynatrace'];
|
|
550
|
+
const production = out.production && PROV.includes(out.production.provider) ? { provider: out.production.provider, runs: out.production.runs === 'hud' ? 'hud' : 'your-runner' } : null;
|
|
551
|
+
return { version: out.version || 1, repo: out.repo, base: out.base, drafted_at: out.drafted_at, rules: out.rules, autonomy: out.autonomy, production };
|
|
543
552
|
}
|
|
544
553
|
|
|
545
554
|
|
|
@@ -564,3 +573,14 @@ export function funTime(hours) {
|
|
|
564
573
|
const n = Math.floor(hours / pick.hours);
|
|
565
574
|
return { days, line: `Enough to watch ${pick.name} ${n.toLocaleString('en-US')} times.`, times: n, series: pick.name };
|
|
566
575
|
}
|
|
576
|
+
|
|
577
|
+
// Wall-clock waiting in the largest unit that reads naturally: hours, days, months, years.
|
|
578
|
+
export function bigTime(hours) {
|
|
579
|
+
const d = hours / 24;
|
|
580
|
+
const one = (x) => (x >= 10 ? String(Math.round(x)) : String(Math.round(x * 10) / 10));
|
|
581
|
+
const mk = (x, u) => { const v = one(x); return { value: Number(v), unit: v === '1' ? u : `${u}s`, text: `${v} ${v === '1' ? u : `${u}s`}` }; };
|
|
582
|
+
if (hours < 48) return mk(hours, 'hour');
|
|
583
|
+
if (d < 30) return mk(d, 'day');
|
|
584
|
+
if (d < 365) return mk(d / 30.4, 'month');
|
|
585
|
+
return mk(d / 365, 'year');
|
|
586
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hud-gateway",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "How many hours your team spent approving agent-written PRs that were already fine, and the approvals policy that hands them back. Read-only, runs locally.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|