@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/diskwarn.mjs
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Before the disk runs out: under twice the floor, dibs clears caches that
|
|
2
|
+
// rebuild themselves, and tells the sessions with something of their own to
|
|
3
|
+
// free (a big scratch folder, or running jobs that wrote a lot) how much is
|
|
4
|
+
// free and what to do. Each hears once per band: under twice the floor, then
|
|
5
|
+
// under the floor. On 2026-10-20 the Mac hit 210 MB free: writes failed, Bash
|
|
6
|
+
// died for every session, and jobs sat silently on "free disk". On 2026-11-03
|
|
7
|
+
// the warnings had gone to every session, 13 of them as the disk fell from 8.2
|
|
8
|
+
// to 4.7 GB, mostly to agents with nothing to free; the person sees the whole
|
|
9
|
+
// machine in dibs alerts.
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import os from 'node:os';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { execFile } from 'node:child_process';
|
|
14
|
+
import { HOME, paths } from './config.mjs';
|
|
15
|
+
import { freeDisk, OS } from './platform.mjs';
|
|
16
|
+
import { event } from './events.mjs';
|
|
17
|
+
import { gb, bytes, now } from './util.mjs';
|
|
18
|
+
import { makeAlive } from './proc.mjs';
|
|
19
|
+
|
|
20
|
+
const GB = b => gb(b).replace(/G$/, ' GB');
|
|
21
|
+
const SAY_FREED = 0.1 * 1024 ** 3; // less than shows as 0.0 GB: not worth a line
|
|
22
|
+
const LOG = path.join(HOME, 'cool.log');
|
|
23
|
+
// Running jobs that wrote this much (src/measure.mjs, a run's share of the disk's fall) have something to free.
|
|
24
|
+
const ATE_MIN = 1024 ** 3;
|
|
25
|
+
// The bands a session hears about once each, worst last.
|
|
26
|
+
const BANDS = ['low', 'critical'];
|
|
27
|
+
// Clearing caches again sooner than this frees little: they refill slowly.
|
|
28
|
+
const CLEAN_EVERY = 30 * 6e4;
|
|
29
|
+
const STEP_TIMEOUT = 2 * 6e4;
|
|
30
|
+
const DAY = 24 * 36e5;
|
|
31
|
+
|
|
32
|
+
// The caches dibs may clear on its own: each rebuilds itself when next needed.
|
|
33
|
+
// Never Claude Code's temp folders, a person's own folders, or a booted simulator
|
|
34
|
+
// (simctl's "unavailable" ones have no runtime, so nothing can boot them).
|
|
35
|
+
// DIBS_CLEAN ({label: argv}) and DIBS_DERIVED stand in for them in tests.
|
|
36
|
+
function steps() {
|
|
37
|
+
// A test's own DIBS_HOME never clears the real caches.
|
|
38
|
+
if (!process.env.DIBS_CLEAN && HOME !== path.join(os.homedir(), '.dibs')) return [];
|
|
39
|
+
if (process.env.DIBS_CLEAN) {
|
|
40
|
+
// {label: argv}, or {label: {argv, dir}} to measure a folder.
|
|
41
|
+
return [...Object.entries(JSON.parse(process.env.DIBS_CLEAN)).map(([label, x]) => (Array.isArray(x) ? { label, argv: x } : { label, ...x })),
|
|
42
|
+
...(process.env.DIBS_DERIVED ? [{ label: 'Xcode build output unused for a day', derived: process.env.DIBS_DERIVED }] : [])];
|
|
43
|
+
}
|
|
44
|
+
const h = os.homedir();
|
|
45
|
+
const out = [{ label: 'npm cache', argv: ['npm', 'cache', 'clean', '--force'], dir: path.join(h, '.npm/_cacache') }];
|
|
46
|
+
if (OS === 'macos') {
|
|
47
|
+
out.push({ label: 'Homebrew cache', argv: ['brew', 'cleanup', '-s'], dir: path.join(h, 'Library/Caches/Homebrew') },
|
|
48
|
+
{ label: 'unavailable simulators', argv: ['xcrun', 'simctl', 'delete', 'unavailable'], dir: path.join(h, 'Library/Developer/CoreSimulator/Devices') },
|
|
49
|
+
{ label: 'Xcode build output unused for a day', derived: path.join(os.homedir(), 'Library/Developer/Xcode/DerivedData') });
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
const run = argv => new Promise(resolve => execFile(argv[0], argv.slice(1), { timeout: STEP_TIMEOUT }, () => resolve()));
|
|
54
|
+
// A folder's size in bytes, or null. What a step freed is its folders' shrink,
|
|
55
|
+
// not free disk before and after: on 2026-10-22 swap gave back 3 GB while
|
|
56
|
+
// Homebrew's cleanup ran, and the note said the cleanup freed it.
|
|
57
|
+
const size = dirs => new Promise(resolve => execFile('du', ['-sk', ...dirs], { timeout: STEP_TIMEOUT }, (e, out) => {
|
|
58
|
+
const kb = String(out || '').split('\n').map(l => parseInt(l, 10)).filter(n => n >= 0);
|
|
59
|
+
resolve(kb.length ? kb.reduce((a, n) => a + n, 0) * 1024 : null);
|
|
60
|
+
}));
|
|
61
|
+
// A DerivedData folder no build has touched in a day: Xcode makes it again on the next build.
|
|
62
|
+
// Returns what it removed, in bytes.
|
|
63
|
+
async function staleDerived(dir, t) {
|
|
64
|
+
let list = [];
|
|
65
|
+
try { list = fs.readdirSync(dir, { withFileTypes: true }); } catch { return 0; }
|
|
66
|
+
const stale = list.filter(e => e.isDirectory()).map(e => path.join(dir, e.name)).filter(p => { try { return t - fs.statSync(p).mtimeMs > DAY; } catch { return false; } });
|
|
67
|
+
if (!stale.length) return 0;
|
|
68
|
+
const was = await size(stale);
|
|
69
|
+
for (const p of stale) try { fs.rmSync(p, { recursive: true, force: true }); } catch {}
|
|
70
|
+
return was || 0;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// One clearing: each step in turn, what it freed by free disk before and after, logged under dibs's name.
|
|
74
|
+
export async function clean(t = now(), where = HOME) {
|
|
75
|
+
const done = [];
|
|
76
|
+
for (const st of steps()) {
|
|
77
|
+
if (st.derived) { done.push({ label: st.label, freed: await staleDerived(st.derived, t) }); continue; }
|
|
78
|
+
// A step without a folder to measure (DIBS_CLEAN in tests) falls back to free disk.
|
|
79
|
+
const measure = () => (st.dir ? size([st.dir]) : freeDisk(where));
|
|
80
|
+
const before = await measure();
|
|
81
|
+
await run(st.argv);
|
|
82
|
+
const after = await measure();
|
|
83
|
+
const by = before != null && after != null ? (st.dir ? before - after : after - before) : 0;
|
|
84
|
+
done.push({ label: st.label, freed: Math.max(0, by) });
|
|
85
|
+
}
|
|
86
|
+
const freed = done.reduce((a, d) => a + d.freed, 0);
|
|
87
|
+
try {
|
|
88
|
+
fs.appendFileSync(LOG, `${new Date(t).toISOString()} disk low: dibs cleared ${done.map(d => d.label).join(', ')}, freeing ${GB(freed)}\n`);
|
|
89
|
+
event({ kind: 'clean', freed, steps: done });
|
|
90
|
+
} catch {}
|
|
91
|
+
return { at: t, freed, steps: done };
|
|
92
|
+
}
|
|
93
|
+
export const cleanDue = (was, t, critical) => !(t - (was?.cleaned?.at || 0) < (critical && !was?.cleanedCritical ? 0 : CLEAN_EVERY));
|
|
94
|
+
|
|
95
|
+
// The sessions to tell: those with something of their own to free, a scratch
|
|
96
|
+
// folder hogs.mjs found over its line (was.folders) or running jobs that ate
|
|
97
|
+
// ATE_MIN of disk. Their jobs, for the note.
|
|
98
|
+
function sessions(s, was) {
|
|
99
|
+
const out = new Map();
|
|
100
|
+
const add = (session, who, agentId) => { if (session && !out.has(session)) out.set(session, { session, who, agentId: agentId || null, running: [], waiting: [], growing: false }); return out.get(session); };
|
|
101
|
+
// Growing: its folder grew since the last reading (hogs.mjs), or its jobs are writing.
|
|
102
|
+
for (const h of was?.folders || []) add(h.session, h.who, h.agentId).growing ||= !!was.grew?.includes(`session ${h.session}'s scratch folder`);
|
|
103
|
+
for (const l of s.leases || []) if (l.kind === 'run' && l.sessionId && (l.ate || 0) >= ATE_MIN) add(l.sessionId, l.who, l.agentId).growing = true;
|
|
104
|
+
for (const l of s.leases || []) if (l.kind === 'run' && out.has(l.sessionId)) out.get(l.sessionId).running.push(l);
|
|
105
|
+
for (const q of s.queue || []) if (q.pid && out.has(q.sessionId)) out.get(q.sessionId).waiting.push(q);
|
|
106
|
+
return [...out.values()];
|
|
107
|
+
}
|
|
108
|
+
const job = x => `#${x.id}${x.purpose ? ` ("${x.purpose}")` : ''}`;
|
|
109
|
+
|
|
110
|
+
// The note itself. hint: what's growing and the biggest folder (hogs.mjs hogHint), cleaned: clean()'s result.
|
|
111
|
+
export function noteText(x, m, floor, hint, cleaned) {
|
|
112
|
+
const critical = m.diskFree < floor;
|
|
113
|
+
const lines = [critical
|
|
114
|
+
? `free disk is critically low: ${GB(m.diskFree)} left (dibs starts nothing new under ${GB(floor)}). Writes, builds and even Bash will start failing. Save your work now: commit or copy anything in progress.`
|
|
115
|
+
: `free disk is getting low: ${GB(m.diskFree)} left (dibs starts nothing new under ${GB(floor)})`];
|
|
116
|
+
if (hint) lines.push(hint.replace(/^\. /, ''));
|
|
117
|
+
if (cleaned?.freed >= SAY_FREED) lines.push(`dibs cleared caches that rebuild themselves and freed ${GB(cleaned.freed)} (${cleaned.steps.filter(d => d.freed >= SAY_FREED).map(d => `${d.label} ${GB(d.freed)}`).join(', ')})`);
|
|
118
|
+
if (critical && x.running.length) lines.push(`Your running dibs ${x.running.length === 1 ? 'job' : 'jobs'} ${x.running.map(job).join(', ')} may fail writing: save what it has done if you can`);
|
|
119
|
+
// Over the floor a waiter is held by something else, not the disk.
|
|
120
|
+
if (critical && x.waiting.length) lines.push(`Your waiting ${x.waiting.length === 1 ? 'job' : 'jobs'} ${x.waiting.map(job).join(', ')} won't start until there's room`);
|
|
121
|
+
// Files holding still were there before the disk got low: telling their session to free them each time taught agents to skip the note (e4, 2026-11-03).
|
|
122
|
+
if (x.growing) lines.push('Free what you can of your own: build output, scratch folders and merged worktrees your session made (only exact paths you made; others of your session may still use the rest)');
|
|
123
|
+
return lines.join('. ') + '.';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Whether the throttle watcher (src/cool.mjs) is down: its pidfile names no live process.
|
|
127
|
+
export function watcherDown() {
|
|
128
|
+
let pid = 0;
|
|
129
|
+
try { pid = Number(fs.readFileSync(path.join(HOME, 'cool.pid'), 'utf8')); } catch {}
|
|
130
|
+
return !(pid && makeAlive([pid])(pid));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// One pass, from hogs.mjs under twice the floor: clear caches when due, then tell
|
|
134
|
+
// each session with something to free, once per band. Returns what to keep in hogs.json.
|
|
135
|
+
export async function warnOnce(cfg, m, was, hint, t = now()) {
|
|
136
|
+
const floor = bytes(cfg.minDisk);
|
|
137
|
+
if (m.diskFree == null || m.diskFree >= 2 * floor) return {};
|
|
138
|
+
const critical = m.diskFree < floor;
|
|
139
|
+
const keep = {};
|
|
140
|
+
let cleaned = was?.cleaned && t - was.cleaned.at < CLEAN_EVERY ? was.cleaned : null;
|
|
141
|
+
if (cleanDue(was, t, critical)) { cleaned = await clean(t); Object.assign(keep, { cleaned, cleanedCritical: critical }); }
|
|
142
|
+
let s = {};
|
|
143
|
+
try { s = JSON.parse(fs.readFileSync(paths.state, 'utf8')); } catch {}
|
|
144
|
+
const { notify } = await import('./talk.mjs');
|
|
145
|
+
const level = critical ? 'critical' : 'low';
|
|
146
|
+
// The worst band each session has heard about; hogs.mjs clears it once there's room again.
|
|
147
|
+
const warned = { ...(was?.warned || {}) };
|
|
148
|
+
for (const x of sessions(s, was)) {
|
|
149
|
+
if (BANDS.indexOf(warned[x.session]?.level) >= BANDS.indexOf(level)) continue;
|
|
150
|
+
if (notify({ sessionId: x.session, who: x.who, agentId: x.agentId }, `disk:${level}:${t}`, noteText(x, m, floor, hint, cleaned))) warned[x.session] = { at: t, level, free: m.diskFree };
|
|
151
|
+
}
|
|
152
|
+
// Kept only when it changed: on a nearly full disk, every write counts.
|
|
153
|
+
return JSON.stringify(warned) === JSON.stringify(was?.warned || {}) ? keep : { ...keep, warned };
|
|
154
|
+
}
|
package/src/events.mjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// What happened, one JSON line per event, for dibs history and the Mac app.
|
|
2
|
+
// Lease starts, ends, pauses and resumes are found by comparing the state
|
|
3
|
+
// before and after each change (logChanges, called by withState), so every
|
|
4
|
+
// way a lease ends is caught: exit, crash, release, a hold running out.
|
|
5
|
+
// Waits and timeouts come from acquire, load samples from the throttle
|
|
6
|
+
// watcher, owner overrides from whoever overrides. Appending never fails a
|
|
7
|
+
// run: an error is written once per process to dibs.log and dropped.
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { HOME } from './config.mjs';
|
|
11
|
+
import { now } from './util.mjs';
|
|
12
|
+
|
|
13
|
+
export const EVENTS = path.join(HOME, 'events.jsonl');
|
|
14
|
+
export const OLDER = path.join(HOME, 'events.1.jsonl');
|
|
15
|
+
const LOG = path.join(HOME, 'dibs.log');
|
|
16
|
+
// Two files, each rotated out at half the limits, keep at most 30 days and 20 MB.
|
|
17
|
+
// A rename never loses an append racing it: the line lands in one file or the other.
|
|
18
|
+
export const KEEP_MS = 30 * 864e5;
|
|
19
|
+
export const KEEP_BYTES = 20 * 1024 ** 2;
|
|
20
|
+
|
|
21
|
+
let complained = false;
|
|
22
|
+
function complain(e) {
|
|
23
|
+
if (complained) return;
|
|
24
|
+
complained = true;
|
|
25
|
+
try { fs.appendFileSync(LOG, `${new Date().toISOString()} couldn't write ${EVENTS}: ${e.message}. History misses events from pid ${process.pid}.\n`); } catch {}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Append one event: { kind, res, who, ... }. `at` is set when missing.
|
|
29
|
+
// Overrides go through here too: event({ kind: 'override', res, who, ... }).
|
|
30
|
+
export function event(e) {
|
|
31
|
+
try { fs.appendFileSync(EVENTS, JSON.stringify({ at: now(), ...e }) + '\n'); } catch (err) { complain(err); }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Move the current file aside once it's past half of either limit. Called
|
|
35
|
+
// under the state lock, so two processes never rotate at once.
|
|
36
|
+
export function rotate(t = now()) {
|
|
37
|
+
try {
|
|
38
|
+
const st = fs.statSync(EVENTS);
|
|
39
|
+
if (st.size < KEEP_BYTES / 2 && t - firstAt() < KEEP_MS / 2) return;
|
|
40
|
+
fs.renameSync(EVENTS, OLDER);
|
|
41
|
+
} catch (e) { if (e.code !== 'ENOENT') complain(e); }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// When the current file's first event happened. Only its first bytes are read.
|
|
45
|
+
function firstAt() {
|
|
46
|
+
const fd = fs.openSync(EVENTS, 'r');
|
|
47
|
+
try {
|
|
48
|
+
const buf = Buffer.alloc(64);
|
|
49
|
+
fs.readSync(fd, buf, 0, 64, 0);
|
|
50
|
+
return Number(buf.toString().match(/^\{"at":(\d+)/)?.[1]) || now();
|
|
51
|
+
} finally { fs.closeSync(fd); }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Every event kept, oldest first. A line cut short by a crash is skipped.
|
|
55
|
+
export function readEvents(t = now()) {
|
|
56
|
+
const out = [];
|
|
57
|
+
for (const f of [OLDER, EVENTS]) {
|
|
58
|
+
let text = '';
|
|
59
|
+
try { text = fs.readFileSync(f, 'utf8'); } catch {}
|
|
60
|
+
for (const l of text.split('\n')) {
|
|
61
|
+
if (!l) continue;
|
|
62
|
+
try { const e = JSON.parse(l); if (e.at >= t - KEEP_MS) out.push(e); } catch {}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return out.sort((a, b) => a.at - b.at);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const fields = l => ({ id: l.id, res: l.res, who: l.who, prio: l.prio || 'normal', type: l.kind });
|
|
69
|
+
|
|
70
|
+
// before: the leases as read, before dead ones were reaped. paused: their
|
|
71
|
+
// pausedAt at that point (the lease objects themselves change in place).
|
|
72
|
+
export function logChanges(before, paused, s) {
|
|
73
|
+
try {
|
|
74
|
+
const t = now();
|
|
75
|
+
const after = new Map(s.leases.map(l => [l.id, l]));
|
|
76
|
+
const was = new Set(before.map(l => l.id));
|
|
77
|
+
const out = [];
|
|
78
|
+
for (const l of s.leases) {
|
|
79
|
+
if (!was.has(l.id)) out.push({ at: t, kind: 'start', ...fields(l), ms: l.waited ?? 0, ...(l.held && { held: l.held }) });
|
|
80
|
+
else if (!paused.get(l.id) && l.pausedAt) out.push({ at: t, kind: 'pause', ...fields(l) });
|
|
81
|
+
else if (paused.get(l.id) && !l.pausedAt) out.push({ at: t, kind: 'resume', ...fields(l), ms: t - paused.get(l.id) });
|
|
82
|
+
}
|
|
83
|
+
for (const l of before) {
|
|
84
|
+
if (after.has(l.id)) continue;
|
|
85
|
+
const pausedMs = (l.pausedMs || 0) + (paused.get(l.id) ? t - paused.get(l.id) : 0);
|
|
86
|
+
// What it was, so a failure can be named and a later success of the same command can close it (src/alerts.mjs).
|
|
87
|
+
out.push({ at: t, kind: 'end', ...fields(l), code: l.code ?? null, ms: t - l.start, paused: pausedMs,
|
|
88
|
+
...(l.kind === 'run' && { key: l.key, cmd: String(l.cmd || '').slice(0, 120), ...(l.purpose && { purpose: String(l.purpose).slice(0, 120) }),
|
|
89
|
+
// A deadline (--by) makes its failure the person's business; an override's by is a name, not that.
|
|
90
|
+
...(typeof l.by === 'number' && { due: l.by }) }) });
|
|
91
|
+
}
|
|
92
|
+
if (!out.length) return;
|
|
93
|
+
rotate(t);
|
|
94
|
+
for (const e of out) event(e);
|
|
95
|
+
} catch (e) { complain(e); }
|
|
96
|
+
}
|