@dibssoftware/dibs 0.0.0-stage → 0.0.179
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 +594 -0
- package/LICENSE +670 -0
- package/README.md +85 -2
- package/bin/dibs-guard.mjs +7 -0
- package/bin/dibs.mjs +548 -0
- package/docs/benchmarks.md +193 -0
- package/docs/files.md +30 -0
- package/docs/guard.md +91 -0
- package/docs/later.md +43 -0
- package/docs/machines.md +147 -0
- package/docs/questions.md +78 -0
- package/docs/reference.md +157 -0
- package/docs/relay.md +196 -0
- package/docs/releasing.md +27 -0
- package/docs/scheduling.md +163 -0
- package/docs/simulators.md +17 -0
- package/package.json +45 -4
- package/skills/dibs/SKILL.md +183 -0
- package/src/acquire.mjs +700 -0
- package/src/admin.mjs +145 -0
- package/src/agents.mjs +145 -0
- package/src/alerts.mjs +499 -0
- package/src/answers.mjs +65 -0
- package/src/board.mjs +202 -0
- package/src/broker.mjs +369 -0
- package/src/commands.mjs +810 -0
- package/src/config.mjs +324 -0
- package/src/connect.mjs +106 -0
- package/src/cool.mjs +160 -0
- package/src/diskwarn.mjs +154 -0
- package/src/events.mjs +96 -0
- package/src/files.mjs +1123 -0
- package/src/guard.mjs +613 -0
- package/src/history.mjs +247 -0
- package/src/hogs.mjs +227 -0
- package/src/later.mjs +487 -0
- package/src/measure.mjs +262 -0
- package/src/news.mjs +101 -0
- package/src/nodecheck.mjs +17 -0
- package/src/notes.mjs +171 -0
- package/src/notices.mjs +160 -0
- package/src/ondemand.mjs +572 -0
- package/src/overlap.mjs +151 -0
- package/src/override.mjs +360 -0
- package/src/owner.mjs +30 -0
- package/src/plain.mjs +470 -0
- package/src/platform.mjs +357 -0
- package/src/preempt.mjs +438 -0
- package/src/primer.mjs +89 -0
- package/src/proc.mjs +99 -0
- package/src/progress.mjs +187 -0
- package/src/qr.mjs +204 -0
- package/src/questions.mjs +389 -0
- package/src/quota.mjs +81 -0
- package/src/reads.mjs +54 -0
- package/src/relay.mjs +779 -0
- package/src/remote.mjs +584 -0
- package/src/reset.mjs +127 -0
- package/src/schedule.mjs +1020 -0
- package/src/scratch.mjs +61 -0
- package/src/serve.mjs +742 -0
- package/src/service.mjs +221 -0
- package/src/shard.mjs +59 -0
- package/src/simctl.mjs +42 -0
- package/src/simpool.mjs +258 -0
- package/src/state.mjs +201 -0
- package/src/stats.mjs +328 -0
- package/src/style.mjs +44 -0
- package/src/talk.mjs +702 -0
- package/src/thin.sh +130 -0
- package/src/trust.mjs +152 -0
- package/src/util.mjs +184 -0
- package/src/version.mjs +2 -0
- package/src/web.mjs +1119 -0
- package/src/worker.mjs +318 -0
package/src/measure.mjs
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
const GB = b => gb(b).replace(/G$/, ' GB');
|
|
2
|
+
// What jobs really use. A job's memory (--mem, or its resource's default) is
|
|
3
|
+
// a guess, and macOS can't cap a process tree without root, so instead of a
|
|
4
|
+
// cage this keeps the numbers honest: each running lease's tree is measured,
|
|
5
|
+
// its peak goes into history with its run time, and the next run of the same
|
|
6
|
+
// command is planned at what it really took.
|
|
7
|
+
//
|
|
8
|
+
// Memory is physical footprint (src/platform.mjs footprints), CPU is the
|
|
9
|
+
// process table's % of a core, both summed over every process group the lease
|
|
10
|
+
// spans plus, for a pool simulator, the simulator's own launchd_sim tree,
|
|
11
|
+
// which runs outside the job's. Reading footprints costs a top call, so
|
|
12
|
+
// it's taken at most once per SAMPLE_EVERY across the machine: whichever
|
|
13
|
+
// `dibs run` claims the slot measures every lease at once.
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import { HOME, paths, hardware, machine } from './config.mjs';
|
|
17
|
+
import { processes, footprints, OS } from './platform.mjs';
|
|
18
|
+
import { withStateIfRoom } from './state.mjs';
|
|
19
|
+
import { gb, now, bytes, runsOf, say, KEY_SPLIT } from './util.mjs';
|
|
20
|
+
import { event } from './events.mjs';
|
|
21
|
+
|
|
22
|
+
// top costs about 0.3s of CPU per call, so 10s keeps it near 3% of one core
|
|
23
|
+
// while anything runs, and nothing while nothing does. A build's peak lasts
|
|
24
|
+
// longer than that; a spike shorter than it can be missed.
|
|
25
|
+
export const SAMPLE_EVERY = Number(process.env.DIBS_SAMPLE_MS) || 10e3;
|
|
26
|
+
// The learned size is the most any of the last few measured runs used: one
|
|
27
|
+
// light run doesn't undo a heavy one, and a command that got lighter is
|
|
28
|
+
// believed after this many runs.
|
|
29
|
+
const LEARN_RUNS = 5;
|
|
30
|
+
|
|
31
|
+
// One process per slot wins the right to measure. The file name is the slot,
|
|
32
|
+
// and creating it fails for everyone else.
|
|
33
|
+
function claim(t) {
|
|
34
|
+
const slot = Math.floor(t / SAMPLE_EVERY);
|
|
35
|
+
try { fs.writeFileSync(path.join(HOME, `sample-${slot}`), '', { flag: 'wx' }); } catch { return false; }
|
|
36
|
+
try { for (const f of fs.readdirSync(HOME)) if (/^sample-\d+$/.test(f) && f !== `sample-${slot}`) fs.rmSync(path.join(HOME, f), { force: true }); } catch {}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Every pid that belongs to a lease: its process groups, less those of runs
|
|
41
|
+
// nested in it, and the launchd_sim tree of any pool simulator it's using (its
|
|
42
|
+
// own, or its session's hold's).
|
|
43
|
+
export function treeOf(l, s, procs) {
|
|
44
|
+
const groups = new Set([l.pgid, ...(l.groups || [])].filter(Boolean));
|
|
45
|
+
// A dibs run nested in this one (a ship calling dibs run xcode-build) shows
|
|
46
|
+
// up among its groups, but it's measured as its own lease: counting it here
|
|
47
|
+
// too would plan and flag the outer job at the inner one's size.
|
|
48
|
+
for (const x of s.leases || [])
|
|
49
|
+
if (x.id !== l.id && x.kind === 'run' && x.pgid && x.pgid !== l.pgid && groups.has(x.pgid))
|
|
50
|
+
for (const g of [x.pgid, ...(x.groups || [])]) groups.delete(g);
|
|
51
|
+
const pids = new Set(procs.filter(p => groups.has(p.pgid)).map(p => p.pid));
|
|
52
|
+
const holds = new Set(s.leases.filter(h => h.kind === 'hold' && h.pid && h.pid === l.spid).map(h => h.id));
|
|
53
|
+
// A hold's simulator counts in one of the runs under it, the first: two test
|
|
54
|
+
// runs on one held simulator each counting it charged it twice.
|
|
55
|
+
const first = d => !(s.leases || []).some(x => x.kind === 'run' && x.id < l.id && x.spid === l.spid && x.covered?.includes(d.res));
|
|
56
|
+
const udids = (s.pool || []).filter(d => d.udid && (d.lease === l.id || (holds.has(d.lease) && l.covered?.includes(d.res) && first(d)))).map(d => d.udid);
|
|
57
|
+
// The shared simulator (sim) is whichever booted one the pool didn't make. A
|
|
58
|
+
// job holding it does its work there, outside its own groups: on 2026-11-02 a
|
|
59
|
+
// TestFlight ship measured 0.1 GB and no CPU for 40 minutes while it ran in
|
|
60
|
+
// the simulator, so dibs took it for idle and planned it at nothing.
|
|
61
|
+
const pooled = (s.pool || []).map(d => d.udid).filter(Boolean);
|
|
62
|
+
const shared = l.res?.includes('sim') || (l.covered?.includes('sim') && first({ res: 'sim' }));
|
|
63
|
+
if (!udids.length && !shared) return pids;
|
|
64
|
+
const kids = new Map();
|
|
65
|
+
for (const p of procs) { if (!kids.has(p.ppid)) kids.set(p.ppid, []); kids.get(p.ppid).push(p.pid); }
|
|
66
|
+
const mine = p => (udids.some(u => p.args.includes(`/${u}/`)) || (shared && !pooled.some(u => p.args.includes(`/${u}/`))));
|
|
67
|
+
const stack = procs.filter(p => /\blaunchd_sim\b/.test(p.args) && mine(p)).map(p => p.pid);
|
|
68
|
+
while (stack.length) {
|
|
69
|
+
const pid = stack.pop();
|
|
70
|
+
if (pids.has(pid)) continue;
|
|
71
|
+
pids.add(pid);
|
|
72
|
+
stack.push(...(kids.get(pid) || []));
|
|
73
|
+
}
|
|
74
|
+
return pids;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Memory in bytes and CPU in cores for a set of pids. A process top didn't
|
|
78
|
+
// list (it started since) counts at its RSS.
|
|
79
|
+
export function usage(pids, procs, foot) {
|
|
80
|
+
let mem = 0, cpu = 0;
|
|
81
|
+
for (const p of procs) {
|
|
82
|
+
if (!pids.has(p.pid)) continue;
|
|
83
|
+
mem += foot?.get(p.pid) ?? p.rss ?? 0;
|
|
84
|
+
cpu += p.cpu / 100;
|
|
85
|
+
}
|
|
86
|
+
return { mem, cpu: Math.round(cpu * 10) / 10 };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Jobs can't use more memory than the machine has in use. Footprints count
|
|
90
|
+
// memory processes share (a browser's GPU and IPC buffers) once per process:
|
|
91
|
+
// on 2026-10-20 Playwright runs read 11 to 13 GB while the 16 GB Mac had more
|
|
92
|
+
// than half its memory free, and every later browser run was planned at the
|
|
93
|
+
// whole budget. When the jobs' sum is more than what's in use, each is scaled
|
|
94
|
+
// down by the same share.
|
|
95
|
+
export function fit(got, m = machine()) {
|
|
96
|
+
if (m.memFree == null) return;
|
|
97
|
+
const inUse = (m.memTotal || hardware().mem) * (1 - m.memFree / 100);
|
|
98
|
+
const total = [...got.values()].reduce((a, u) => a + u.mem, 0);
|
|
99
|
+
if (!(total > inUse)) return;
|
|
100
|
+
for (const u of got.values()) u.mem = Math.round(u.mem * inUse / total);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// What each run ate of free disk: every fall between two measurements, less
|
|
104
|
+
// what swap grew by (that's memory, not anyone's files: the swap files' size where
|
|
105
|
+
// the OS says it, since macOS adds a gigabyte file before it's used), shared among the runs
|
|
106
|
+
// measured then, by the disk their resources declare (DISK_WEIGHT at least).
|
|
107
|
+
// Crediting each run with the whole fall, as 0.0.153 did, gave a deploy 3 GB
|
|
108
|
+
// for a simulator's writes and held it on the disk. A rise frees nobody's share.
|
|
109
|
+
const DISK_WEIGHT = 0.25 * 1024 ** 3;
|
|
110
|
+
function shareDisk(s, got, m, t) {
|
|
111
|
+
const last = s.diskSample;
|
|
112
|
+
s.diskSample = { free: m.diskFree ?? null, swap: m.swapUsed ?? null, ...(m.swapFiles != null && { files: m.swapFiles }), at: t };
|
|
113
|
+
const runs = s.leases.filter(l => got.has(l.id) && !l.pausedAt);
|
|
114
|
+
for (const l of runs) l.ate ??= 0;
|
|
115
|
+
if (!last || last.free == null || m.diskFree == null || t - last.at > 3 * SAMPLE_EVERY) return;
|
|
116
|
+
const swapGrew = Math.max(0, last.swap != null && m.swapUsed != null ? m.swapUsed - last.swap : 0,
|
|
117
|
+
last.files != null && m.swapFiles != null ? m.swapFiles - last.files : 0);
|
|
118
|
+
const fell = Math.max(0, last.free - m.diskFree - swapGrew);
|
|
119
|
+
if (!fell || !runs.length) return;
|
|
120
|
+
const w = l => Math.max(l.disk || 0, DISK_WEIGHT);
|
|
121
|
+
const all = runs.reduce((a, l) => a + w(l), 0);
|
|
122
|
+
for (const l of runs) l.ate += fell * w(l) / all;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Measure every run lease on the machine, if this process won the slot.
|
|
126
|
+
// Called from each `dibs run` on its group-tracking tick. True if it measured.
|
|
127
|
+
export function sample(t = now()) {
|
|
128
|
+
if (OS === 'windows' || !claim(t)) return false;
|
|
129
|
+
// A plain read: the state file is replaced whole, never written in place.
|
|
130
|
+
let s;
|
|
131
|
+
try { s = JSON.parse(fs.readFileSync(paths.state, 'utf8')); } catch { return false; }
|
|
132
|
+
const runs = (s.leases || []).filter(l => l.kind === 'run' && l.pgid);
|
|
133
|
+
if (!runs.length) return false;
|
|
134
|
+
const procs = processes();
|
|
135
|
+
const trees = new Map(runs.map(l => [l.id, treeOf(l, s, procs)]));
|
|
136
|
+
const foot = footprints([...new Set([...trees.values()].flatMap(x => [...x]))]);
|
|
137
|
+
const got = new Map([...trees].map(([id, pids]) => [id, usage(pids, procs, foot)]));
|
|
138
|
+
const mm = machine();
|
|
139
|
+
// Made-up footprints come with a made-up machine that doesn't match them.
|
|
140
|
+
if (!process.env.DIBS_FAKE_FOOTPRINT) fit(got);
|
|
141
|
+
// On a full disk this measurement is dropped; the next slot takes another.
|
|
142
|
+
const tell = withStateIfRoom(s => {
|
|
143
|
+
const out = [];
|
|
144
|
+
shareDisk(s, got, mm, t);
|
|
145
|
+
for (const l of s.leases) {
|
|
146
|
+
const u = got.get(l.id);
|
|
147
|
+
if (!u) continue;
|
|
148
|
+
l.use = { ...u, at: t };
|
|
149
|
+
// When its memory last rose past what it had reached: a job let in by the
|
|
150
|
+
// stall breaker counts as settled once this is a while ago (schedule.mjs).
|
|
151
|
+
if (!(u.mem <= (l.peak?.mem ?? -1) * 1.05)) l.peakAt = t;
|
|
152
|
+
l.peak = { mem: Math.max(l.peak?.mem || 0, u.mem), cpu: Math.max(l.peak?.cpu || 0, u.cpu), fit: true,
|
|
153
|
+
...(l.ate != null && { ate: l.ate }) };
|
|
154
|
+
const x = overAsk(l);
|
|
155
|
+
if (!x) continue;
|
|
156
|
+
if (!l.overAsk) {
|
|
157
|
+
event({ kind: 'overask', id: l.id, res: l.res, who: l.who, agent: l.agent, sessionId: l.sessionId, key: l.key, ...x });
|
|
158
|
+
if (l.notify && l.pid) out.push(l.pid);
|
|
159
|
+
}
|
|
160
|
+
l.overAsk = { ...x, at: l.overAsk?.at ?? t };
|
|
161
|
+
}
|
|
162
|
+
return out;
|
|
163
|
+
}) || [];
|
|
164
|
+
// The run's own dibs says it on its stderr (src/preempt.mjs noticeHandler), once the state has it.
|
|
165
|
+
for (const pid of tell) try { process.kill(pid, 'SIGUSR2'); } catch {}
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// A job that used far more memory than it asked for: what it used, what it
|
|
170
|
+
// asked, what dibs planned and the --mem to pass next time, or null. Over
|
|
171
|
+
// OVER_ASK times its plan and by OVER_ASK_MIN or more: footprints wobble, and
|
|
172
|
+
// half a gigabyte over on a small job isn't worth an agent's attention. A job
|
|
173
|
+
// dibs already plans at the whole budget (usual) has nothing better to ask for.
|
|
174
|
+
const OVER_ASK = 1.5;
|
|
175
|
+
const OVER_ASK_MIN = 1024 ** 3;
|
|
176
|
+
export function overAsk(l) {
|
|
177
|
+
const used = Math.max(l?.peak?.mem || 0, l?.use?.mem || 0);
|
|
178
|
+
if (!l || l.usual != null || !(l.mem > 0) || used < l.mem * OVER_ASK || used - l.mem < OVER_ASK_MIN) return null;
|
|
179
|
+
return { used, asked: l.asked ?? l.mem, planned: l.mem, suggest: `${Math.ceil(used / 1024 ** 3)}G` };
|
|
180
|
+
}
|
|
181
|
+
// Running leases that used far more than they asked, with overAsk on each: for the hook's notice to the holder.
|
|
182
|
+
export const overAsks = s => s.leases.filter(l => l.overAsk);
|
|
183
|
+
export const overAskLine = x => `this job used ${GB(x.used)} of memory but asked for ${GB(x.asked)}${x.planned > x.asked ? ` (dibs planned ${GB(x.planned)})` : ''}.`
|
|
184
|
+
+ ` Next time pass --mem ${x.suggest}, so dibs plans for it and doesn't start other jobs into memory this one will take.`;
|
|
185
|
+
// Said once per run, on its stderr: while it runs (the sampler's SIGUSR2) or when it ends.
|
|
186
|
+
let told = false;
|
|
187
|
+
export function tellOverAsk(l) {
|
|
188
|
+
const x = l && (l.overAsk || overAsk(l));
|
|
189
|
+
if (told || !x) return;
|
|
190
|
+
told = true;
|
|
191
|
+
say(overAskLine(x));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// The most the last LEARN_RUNS measured runs of a command used, never more
|
|
195
|
+
// than the machine has, or null when none was measured.
|
|
196
|
+
// A command never measured borrows from its family: the same resources,
|
|
197
|
+
// program and next word (util.mjs KEY_SPLIT), so npx playwright test a.spec.ts
|
|
198
|
+
// learns from b.spec.ts. On 2026-10-15 each Playwright spec ran under its own
|
|
199
|
+
// key, used 5.5 GB of the 3 GB asked, and none ever learned it. The next word
|
|
200
|
+
// matters for wrappers: npm run build and npm run lint, or wrangler d1 and
|
|
201
|
+
// wrangler deploy, are different programs, and a family of just the wrapper
|
|
202
|
+
// lent one's size to the others.
|
|
203
|
+
function family(key) {
|
|
204
|
+
const [head, more = ''] = String(key).split(KEY_SPLIT);
|
|
205
|
+
const next = more.split(' ')[0];
|
|
206
|
+
return next ? `${head}${KEY_SPLIT}${next}` : head;
|
|
207
|
+
}
|
|
208
|
+
// Only runs measured with fit() count: earlier ones counted shared memory in
|
|
209
|
+
// every process, and planned browser runs at three times what they used.
|
|
210
|
+
export function learned(hist, key, cores = hardware().cores) {
|
|
211
|
+
let runs = runsOf(hist, key).filter(h => h.mem > 0 && h.fit);
|
|
212
|
+
if (!runs.length) runs = hist.filter(h => h.mem > 0 && h.fit && family(h.key) === family(key));
|
|
213
|
+
runs = runs.slice(-LEARN_RUNS);
|
|
214
|
+
if (!runs.length) return null;
|
|
215
|
+
// The middle run, not the biggest: one run that read 15 GB (a browser's shared
|
|
216
|
+
// memory counted in every process) planned every later Playwright run at the
|
|
217
|
+
// whole budget on 2026-10-20, and twenty jobs waited on a Mac with 8.6 GB free.
|
|
218
|
+
// A run that outgrows its plan books what it uses anyway (memOf).
|
|
219
|
+
const mems = runs.map(h => h.mem).sort((a, b) => a - b);
|
|
220
|
+
const mem = mems[Math.floor(mems.length / 2)];
|
|
221
|
+
return { mem: Math.min(mem, hardware().mem), cpu: Math.min(Math.max(...runs.map(h => h.cpu || 0)), cores) };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// A job's size as dibs plans it: what it asked for, raised to what its
|
|
225
|
+
// command has really used. asked and askedCpu keep the declared numbers. A
|
|
226
|
+
// command that has used more than the whole memory budget is planned at the
|
|
227
|
+
// budget, with usual saying what it used: it waits for the budget to empty,
|
|
228
|
+
// not for a machine with no leases at all.
|
|
229
|
+
export function sized(size, hist, key, cfg) {
|
|
230
|
+
const got = learned(hist, key, cfg.cores);
|
|
231
|
+
if (!got) return size;
|
|
232
|
+
const out = { ...size };
|
|
233
|
+
const budget = bytes(cfg.budget);
|
|
234
|
+
if (got.mem > size.mem) Object.assign(out, got.mem > budget && size.mem <= budget ? { mem: budget, asked: size.mem, usual: got.mem } : { mem: got.mem, asked: size.mem });
|
|
235
|
+
// Like memory: a learned number past the CPU budget is planned at the budget, so the job can still start alone.
|
|
236
|
+
if (Math.ceil(got.cpu) > size.cpu) Object.assign(out, { cpu: size.cpu <= cfg.cores ? Math.min(Math.ceil(got.cpu), cfg.cores) : Math.ceil(got.cpu), askedCpu: size.cpu });
|
|
237
|
+
return out;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// "uses ~6.1G (asked 4.0G) and 5 cores (asked 3)", or for a job planned at
|
|
241
|
+
// the budget "usually uses ~14.1G and 8 cores (asked 4); planned at the 11.0G
|
|
242
|
+
// budget", or '' when it fits what it asked.
|
|
243
|
+
export const cappedLine = x => (x.usual != null ? `usually uses ~${gb(x.usual)}; planned at the ${gb(x.mem)} budget` : '');
|
|
244
|
+
// The same, in a sentence: "xcodebuild used up to 6.0 GB of memory in recent
|
|
245
|
+
// runs (it asks for 4.0 GB), so dibs plans for that." A job bigger than all
|
|
246
|
+
// the memory dibs shares out is planned at all of it, and waits for it to be free.
|
|
247
|
+
export function grewPlain(x, key) {
|
|
248
|
+
const what = key.replace(KEY_SPLIT, ' ');
|
|
249
|
+
const cores = x.askedCpu != null ? `${x.cpu} cores (it asks for ${x.askedCpu})` : '';
|
|
250
|
+
if (x.usual != null)
|
|
251
|
+
return `${what} usually uses up to ${GB(x.usual)} of memory${cores ? ` and ${cores}` : ''}, more than the ${GB(x.mem)} dibs shares out at once,`
|
|
252
|
+
+ ` so dibs plans it at all ${GB(x.mem)}: it starts when no other job is using that memory.`;
|
|
253
|
+
const parts = [x.asked != null ? `${GB(x.mem)} of memory (it asks for ${GB(x.asked)})` : '', cores].filter(Boolean);
|
|
254
|
+
return parts.length ? `${what} used up to ${parts.join(' and ')} in recent runs, so dibs plans for that.` : '';
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export function grewLine(x) {
|
|
258
|
+
const cores = x.askedCpu != null ? `${x.cpu} cores (asked ${x.askedCpu})` : '';
|
|
259
|
+
if (x.usual != null) return `usually uses ~${gb(x.usual)}${cores ? ` and ${cores}` : ''}; planned at the ${gb(x.mem)} budget`;
|
|
260
|
+
const parts = [x.asked != null ? `~${gb(x.mem)} (asked ${gb(x.asked)})` : '', cores].filter(Boolean);
|
|
261
|
+
return parts.length ? `uses ${parts.join(' and ')}` : '';
|
|
262
|
+
}
|
package/src/news.mjs
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// What changed in dibs, for the agents using it: the CHANGELOG's sections, which version each
|
|
2
|
+
// agent last heard about (seen.json, shared with bin/dibs.mjs's what's-new line), the version on
|
|
3
|
+
// disk now (an update lands while a long dibs listen is running), and whether dibs.cc has a
|
|
4
|
+
// newer release (asked at most every 6 hours per machine, never for a git checkout).
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import { HOME, paths, loadConfig } from './config.mjs';
|
|
9
|
+
|
|
10
|
+
export const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
11
|
+
// DIBS_VERSION_FILE stands in for it in tests, which can't bump the real one.
|
|
12
|
+
const VERSION_FILE = process.env.DIBS_VERSION_FILE || path.join(ROOT, 'src', 'version.mjs');
|
|
13
|
+
// The newest release, as the Mac app reads it. DIBS_UPDATE_URL points a test at its own.
|
|
14
|
+
const LATEST = process.env.DIBS_UPDATE_URL || 'https://dibs.cc/mac/latest.json';
|
|
15
|
+
// How often one machine asks dibs.cc, whoever asks: a release a few hours old can wait.
|
|
16
|
+
export const CHECK_EVERY = 6 * 36e5;
|
|
17
|
+
// An answer slower than this is no answer: the check never holds anything up.
|
|
18
|
+
const CHECK_TIMEOUT = 3000;
|
|
19
|
+
const CHECKED = path.join(HOME, 'update-check.json');
|
|
20
|
+
export const UPGRADE = 'curl -fsSL https://dibs.cc/install.sh | sh';
|
|
21
|
+
|
|
22
|
+
// "0.0.178" against "0.0.99": by number, part by part.
|
|
23
|
+
export function newer(a, b) {
|
|
24
|
+
const x = String(a).split('.').map(Number), y = String(b).split('.').map(Number);
|
|
25
|
+
for (let i = 0; i < Math.max(x.length, y.length); i++) if ((x[i] || 0) !== (y[i] || 0)) return (x[i] || 0) > (y[i] || 0);
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// The changelog's sections, newest first: [{ version, body }].
|
|
30
|
+
export function changelog() {
|
|
31
|
+
try {
|
|
32
|
+
return fs.readFileSync(path.join(ROOT, 'CHANGELOG.md'), 'utf8').split(/^## /m).slice(1)
|
|
33
|
+
.map(sec => ({ version: sec.split(/\s/)[0], body: sec.slice(sec.indexOf('\n') + 1).trim() }));
|
|
34
|
+
} catch { return []; }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// The sections after from, up to and including to, oldest first.
|
|
38
|
+
export const since = (from, to) => changelog().filter(c => /^\d/.test(c.version) && newer(c.version, from) && !newer(c.version, to)).reverse();
|
|
39
|
+
|
|
40
|
+
const readSeen = () => { try { return JSON.parse(fs.readFileSync(paths.seen, 'utf8')); } catch { return {}; } };
|
|
41
|
+
export const seenBy = key => readSeen()[key] || null;
|
|
42
|
+
export function markSeen(key, version) {
|
|
43
|
+
const seen = readSeen();
|
|
44
|
+
seen[key] = version;
|
|
45
|
+
try {
|
|
46
|
+
fs.writeFileSync(`${paths.seen}.${process.pid}`, JSON.stringify(seen));
|
|
47
|
+
fs.renameSync(`${paths.seen}.${process.pid}`, paths.seen);
|
|
48
|
+
} catch {}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// The version on disk, which is what the next dibs command runs: read again only when the file
|
|
52
|
+
// changed, so a listener can look every few seconds for nothing.
|
|
53
|
+
let onDisk = { mtime: 0, version: null };
|
|
54
|
+
export function diskVersion() {
|
|
55
|
+
try {
|
|
56
|
+
const m = fs.statSync(VERSION_FILE).mtimeMs;
|
|
57
|
+
if (m !== onDisk.mtime) onDisk = { mtime: m, version: /VERSION = '([^']+)'/.exec(fs.readFileSync(VERSION_FILE, 'utf8'))?.[1] || null };
|
|
58
|
+
} catch {}
|
|
59
|
+
return onDisk.version;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// A git checkout (this repo, linked from ~/bin) updates by git, not by the install script.
|
|
63
|
+
const checkout = () => !process.env.DIBS_UPDATE_URL && fs.existsSync(path.join(ROOT, '.git'));
|
|
64
|
+
const readChecked = () => { try { return JSON.parse(fs.readFileSync(CHECKED, 'utf8')); } catch { return {}; } };
|
|
65
|
+
function writeChecked(c) {
|
|
66
|
+
try {
|
|
67
|
+
fs.writeFileSync(`${CHECKED}.${process.pid}`, JSON.stringify(c));
|
|
68
|
+
fs.renameSync(`${CHECKED}.${process.pid}`, CHECKED);
|
|
69
|
+
} catch {}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The newest release dibs.cc knows of, as last asked, asking again (in the background) when
|
|
73
|
+
// the machine's last ask is older than CHECK_EVERY. Off with config updateCheck: false.
|
|
74
|
+
export function latest(t = Date.now()) {
|
|
75
|
+
let off = false;
|
|
76
|
+
try { off = loadConfig().updateCheck === false; } catch {}
|
|
77
|
+
if (off || checkout()) return null;
|
|
78
|
+
const c = readChecked();
|
|
79
|
+
if (!(t - (c.at || 0) < CHECK_EVERY)) {
|
|
80
|
+
// Claimed before asking, so a dozen listeners waking together ask once.
|
|
81
|
+
writeChecked({ ...c, at: t });
|
|
82
|
+
fetch(LATEST, { signal: AbortSignal.timeout(CHECK_TIMEOUT) }).then(r => (r.ok ? r.json() : null))
|
|
83
|
+
.then(j => { if (typeof j?.version === 'string' && /^\d+(\.\d+)*$/.test(j.version)) writeChecked({ ...readChecked(), latest: j.version }); })
|
|
84
|
+
.catch(() => {});
|
|
85
|
+
}
|
|
86
|
+
return c.latest || null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Each thread hears about a release once.
|
|
90
|
+
export const toldOf = key => readChecked().told?.[key] || null;
|
|
91
|
+
// Threads remembered: plenty for a machine's sessions over the weeks between releases.
|
|
92
|
+
const TOLD_KEEP = 500;
|
|
93
|
+
export function tell(key, version) {
|
|
94
|
+
const c = readChecked();
|
|
95
|
+
const told = { ...(c.told || {}) };
|
|
96
|
+
delete told[key];
|
|
97
|
+
told[key] = version;
|
|
98
|
+
const keys = Object.keys(told);
|
|
99
|
+
for (const k of keys.slice(0, Math.max(0, keys.length - TOLD_KEEP))) delete told[k];
|
|
100
|
+
writeChecked({ ...c, told });
|
|
101
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Node versions with a known hole dibs shouldn't run its long-lived parts on.
|
|
2
|
+
// CVE-2026-111497 ("Node2Shell", 2026-10-21): an out-of-bounds write in the
|
|
3
|
+
// Brotli decoder, reachable through any br-encoded body. Fixed in 22.23.4,
|
|
4
|
+
// 24.22.1 and 26.12.1; other lines older than 27 get no fix.
|
|
5
|
+
const FIXED = { 22: [22, 23, 4], 24: [24, 22, 1], 26: [26, 12, 1] };
|
|
6
|
+
const FIRST_SAFE_MAJOR = 27;
|
|
7
|
+
|
|
8
|
+
// null when this Node is fine, else a sentence saying why it isn't.
|
|
9
|
+
export function unpatched(version = process.versions.node) {
|
|
10
|
+
const v = String(version).replace(/^v/, '').split('.').map(Number);
|
|
11
|
+
const fix = FIXED[v[0]];
|
|
12
|
+
const why = `Node ${v.join('.')} has the Brotli hole CVE-2026-111497`;
|
|
13
|
+
if (v[0] >= FIRST_SAFE_MAJOR) return null;
|
|
14
|
+
if (!fix) return `${why}, and Node ${v[0]} gets no fix. Use Node 22.23.4, 24.22.1, 26.12.1 or newer`;
|
|
15
|
+
for (let i = 1; i < 3; i++) if (v[i] !== fix[i]) return v[i] > fix[i] ? null : `${why}. Upgrade to ${fix.join('.')} or newer`;
|
|
16
|
+
return null;
|
|
17
|
+
}
|
package/src/notes.mjs
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// dibs init: telling the coding agents on this machine that dibs is here.
|
|
2
|
+
//
|
|
3
|
+
// dibs.md the agent instructions, made from skills/dibs/SKILL.md (one source,
|
|
4
|
+
// no second copy to drift), in the folder dibs is installed in.
|
|
5
|
+
// Rewritten on every install and upgrade, so it matches the version.
|
|
6
|
+
// the block a few marked lines in the agents' own instruction files saying to
|
|
7
|
+
// read dibs.md before builds, tests, simulators or deploys:
|
|
8
|
+
// ~/.claude/CLAUDE.md when ~/.claude exists (made if missing)
|
|
9
|
+
// ~/.codex/AGENTS.md when ~/.codex exists (made if missing)
|
|
10
|
+
// CLAUDE.md and AGENTS.md at the root of the git repo dibs runs in,
|
|
11
|
+
// when that isn't the home folder: only files already there, unless
|
|
12
|
+
// asked with dibs init --repo
|
|
13
|
+
//
|
|
14
|
+
// Only what's between the markers is ever written; the rest of a file stays byte for
|
|
15
|
+
// byte. Each write goes to a temp file beside the real one, with its mode, then is
|
|
16
|
+
// renamed over it: a full disk once emptied a file written in place. A symlinked
|
|
17
|
+
// file is written where the link points, so the link stays a link.
|
|
18
|
+
// DIBS_NO_AGENT_NOTES=1 turns all of it off.
|
|
19
|
+
import fs from 'node:fs';
|
|
20
|
+
import os from 'node:os';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
import { execFileSync } from 'node:child_process';
|
|
23
|
+
import { fileURLToPath } from 'node:url';
|
|
24
|
+
import { HOME } from './config.mjs';
|
|
25
|
+
import { VERSION } from './version.mjs';
|
|
26
|
+
import { die } from './util.mjs';
|
|
27
|
+
|
|
28
|
+
export const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
29
|
+
const START = '<!-- dibs:start -->', END = '<!-- dibs:end -->';
|
|
30
|
+
const BLOCK_RE = /<!-- dibs:start -->[\s\S]*?<!-- dibs:end -->(\r?\n)?/;
|
|
31
|
+
// Which version and folder the notes were last written for (src/notes.mjs stale()).
|
|
32
|
+
const STAMP = path.join(HOME, 'notes.json');
|
|
33
|
+
export const off = () => !!process.env.DIBS_NO_AGENT_NOTES && process.env.DIBS_NO_AGENT_NOTES !== '0';
|
|
34
|
+
|
|
35
|
+
// The agent instructions: SKILL.md without its front matter and title, under a header
|
|
36
|
+
// saying where it came from.
|
|
37
|
+
export function dibsMd(root = ROOT) {
|
|
38
|
+
let skill = '';
|
|
39
|
+
try { skill = fs.readFileSync(path.join(root, 'skills', 'dibs', 'SKILL.md'), 'utf8'); } catch {}
|
|
40
|
+
const body = skill.replace(/^---\n[\s\S]*?\n---\n/, '').replace(/^\s*# dibs\s*\n/, '').replace(/<!-- dibs:rules:(start|end) -->\n/g, '').trim();
|
|
41
|
+
return [
|
|
42
|
+
'# dibs: instructions for coding agents',
|
|
43
|
+
'',
|
|
44
|
+
`Written by dibs ${VERSION} when it was installed here, and rewritten on every install and upgrade, so edits don't last.`,
|
|
45
|
+
`Docs: https://dibs.cc, and ${path.join(root, 'README.md')}. Every command: \`dibs help all\`.`,
|
|
46
|
+
'',
|
|
47
|
+
body || 'Run builds, tests, simulators and deploys through dibs: `dibs run <resource> -m "why" -- <command>`. `dibs help` lists everything.',
|
|
48
|
+
'',
|
|
49
|
+
].join('\n');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const block = file => [START, '## dibs',
|
|
53
|
+
`This machine runs dibs, the CLI that coding agents use to share simulators, builds, tests and deploys. Read ${file} before running builds, tests, simulators or deploys. Docs: https://dibs.cc`,
|
|
54
|
+
END].join('\n');
|
|
55
|
+
|
|
56
|
+
// Write whole and rename into place, keeping the old file's mode.
|
|
57
|
+
function writeAtomic(file, text) {
|
|
58
|
+
let real = file, mode = 0o644;
|
|
59
|
+
try { real = fs.realpathSync(file); mode = fs.statSync(real).mode & 0o7777; } catch {}
|
|
60
|
+
fs.mkdirSync(path.dirname(real), { recursive: true });
|
|
61
|
+
const tmp = `${real}.dibs-${process.pid}`;
|
|
62
|
+
try {
|
|
63
|
+
fs.writeFileSync(tmp, text, { mode });
|
|
64
|
+
fs.chmodSync(tmp, mode);
|
|
65
|
+
fs.renameSync(tmp, real);
|
|
66
|
+
} catch (e) { try { fs.rmSync(tmp, { force: true }); } catch {} throw e; }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// text with the block in it: replaced where the markers are, else added at the end.
|
|
70
|
+
// Line endings follow the file's own.
|
|
71
|
+
export function withBlock(text, b) {
|
|
72
|
+
const nl = /\r\n/.test(text) ? '\r\n' : '\n';
|
|
73
|
+
const mine = b.replace(/\n/g, nl);
|
|
74
|
+
if (BLOCK_RE.test(text)) return text.replace(BLOCK_RE, (m, end) => mine + (end || ''));
|
|
75
|
+
if (!text) return mine + nl;
|
|
76
|
+
const gap = text.endsWith(nl + nl) ? '' : text.endsWith(nl) ? nl : nl + nl;
|
|
77
|
+
return text + gap + mine + nl;
|
|
78
|
+
}
|
|
79
|
+
// text without the block. At the end of the file, the blank line withBlock put in front goes too.
|
|
80
|
+
export function withoutBlock(text) {
|
|
81
|
+
const m = text.match(BLOCK_RE);
|
|
82
|
+
if (!m) return text;
|
|
83
|
+
let before = text.slice(0, m.index);
|
|
84
|
+
const after = text.slice(m.index + m[0].length);
|
|
85
|
+
if (!after) before = before.replace(/(\r?\n)\r?\n$/, '$1');
|
|
86
|
+
return before + after;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// The git repo the command runs in, unless it's the home folder itself.
|
|
90
|
+
function repoRoot(cwd) {
|
|
91
|
+
try {
|
|
92
|
+
const top = execFileSync('git', ['rev-parse', '--show-toplevel'], { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 3000 }).trim();
|
|
93
|
+
return top && path.resolve(top) !== path.resolve(os.homedir()) ? top : null;
|
|
94
|
+
} catch { return null; }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// The instruction files the block goes in. made: may be created when missing.
|
|
98
|
+
function targets(cwd, repo) {
|
|
99
|
+
const home = os.homedir(), out = [];
|
|
100
|
+
if (fs.existsSync(path.join(home, '.claude'))) out.push({ file: path.join(home, '.claude', 'CLAUDE.md'), made: true });
|
|
101
|
+
if (fs.existsSync(path.join(home, '.codex'))) out.push({ file: path.join(home, '.codex', 'AGENTS.md'), made: true });
|
|
102
|
+
const top = repoRoot(cwd);
|
|
103
|
+
if (top) {
|
|
104
|
+
const there = ['CLAUDE.md', 'AGENTS.md'].map(n => path.join(top, n)).filter(f => fs.existsSync(f));
|
|
105
|
+
for (const f of there) out.push({ file: f, made: false });
|
|
106
|
+
if (repo && !there.length) out.push({ file: path.join(top, 'CLAUDE.md'), made: true });
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Where dibs.md goes: the install folder, or ~/.dibs when that can't be written
|
|
112
|
+
// (an npm install owned by root, say).
|
|
113
|
+
function mdPlace(root) {
|
|
114
|
+
const f = path.join(root, 'dibs.md');
|
|
115
|
+
try { fs.accessSync(root, fs.constants.W_OK); return f; } catch { return path.join(HOME, 'dibs.md'); }
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Write dibs.md and the blocks. Returns the files that changed.
|
|
119
|
+
export function write({ root = ROOT, cwd = process.cwd(), repo = false } = {}) {
|
|
120
|
+
const changed = [];
|
|
121
|
+
const md = mdPlace(root);
|
|
122
|
+
const text = dibsMd(root);
|
|
123
|
+
let old = null;
|
|
124
|
+
try { old = fs.readFileSync(md, 'utf8'); } catch {}
|
|
125
|
+
if (old !== text) { writeAtomic(md, text); changed.push(md); }
|
|
126
|
+
const b = block(md);
|
|
127
|
+
for (const t of targets(cwd, repo)) {
|
|
128
|
+
let cur = null;
|
|
129
|
+
try { cur = fs.readFileSync(t.file, 'utf8'); } catch (e) { if (e.code !== 'ENOENT') throw e; }
|
|
130
|
+
if (cur == null && !t.made) continue;
|
|
131
|
+
const next = withBlock(cur || '', b);
|
|
132
|
+
if (next !== cur) { writeAtomic(t.file, next); changed.push(t.file); }
|
|
133
|
+
}
|
|
134
|
+
try { fs.mkdirSync(HOME, { recursive: true }); fs.writeFileSync(STAMP, JSON.stringify({ version: VERSION, root })); } catch {}
|
|
135
|
+
return changed;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Take dibs.md and every block back out. Returns the files that changed.
|
|
139
|
+
export function remove({ root = ROOT, cwd = process.cwd() } = {}) {
|
|
140
|
+
const changed = [];
|
|
141
|
+
for (const md of new Set([path.join(root, 'dibs.md'), path.join(HOME, 'dibs.md')])) {
|
|
142
|
+
if (fs.existsSync(md)) { fs.rmSync(md); changed.push(md); }
|
|
143
|
+
}
|
|
144
|
+
for (const t of targets(cwd, false)) {
|
|
145
|
+
let cur;
|
|
146
|
+
try { cur = fs.readFileSync(t.file, 'utf8'); } catch { continue; }
|
|
147
|
+
const next = withoutBlock(cur);
|
|
148
|
+
if (next !== cur) { writeAtomic(t.file, next); changed.push(t.file); }
|
|
149
|
+
}
|
|
150
|
+
try { fs.rmSync(STAMP, { force: true }); } catch {}
|
|
151
|
+
return changed;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Whether the notes are behind this version or this folder: an npm install has no
|
|
155
|
+
// install script to write them (the package runs no scripts, by design), so its
|
|
156
|
+
// first dibs command after an install or upgrade does.
|
|
157
|
+
export function stale(root = ROOT) {
|
|
158
|
+
if (off()) return false;
|
|
159
|
+
try { const s = JSON.parse(fs.readFileSync(STAMP, 'utf8')); return s.version !== VERSION || s.root !== root; } catch { return true; }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// dibs init [--repo] [--remove] [--quiet]
|
|
163
|
+
export function init(args) {
|
|
164
|
+
const bad = args.find(a => !['--repo', '--remove', '--quiet'].includes(a));
|
|
165
|
+
if (bad) die(`unknown ${bad}. dibs init [--repo] [--remove]`);
|
|
166
|
+
if (off()) return console.log('DIBS_NO_AGENT_NOTES is set, so dibs leaves the agents\' instruction files alone.');
|
|
167
|
+
const quiet = args.includes('--quiet');
|
|
168
|
+
const changed = args.includes('--remove') ? remove() : write({ repo: args.includes('--repo') });
|
|
169
|
+
for (const f of changed) console.log(`${args.includes('--remove') ? 'cleaned' : 'wrote'} ${f}`);
|
|
170
|
+
if (!changed.length && !quiet) console.log('Nothing to change: dibs.md and the notes are up to date.');
|
|
171
|
+
}
|