@dibssoftware/dibs 0.0.178 → 0.0.180
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 +40 -2
- package/README.md +49 -196
- package/bin/dibs-guard.mjs +10 -1
- package/bin/dibs.mjs +72 -31
- package/docs/benchmarks.md +1 -1
- package/docs/files.md +1 -1
- package/docs/guard.md +6 -0
- package/docs/machines.md +1 -1
- package/docs/questions.md +7 -3
- package/docs/reference.md +157 -0
- package/docs/relay.md +9 -7
- package/docs/releasing.md +11 -8
- package/docs/scheduling.md +6 -6
- package/docs/windows.md +14 -0
- package/package.json +2 -1
- package/skills/dibs/SKILL.md +104 -83
- package/src/acquire.mjs +102 -34
- package/src/agents.mjs +7 -22
- package/src/alerts.mjs +157 -57
- package/src/answers.mjs +2 -2
- package/src/board.mjs +3 -4
- package/src/broker.mjs +26 -11
- package/src/commands.mjs +39 -14
- package/src/config.mjs +27 -6
- package/src/cool.mjs +104 -9
- package/src/diskwarn.mjs +32 -35
- package/src/events.mjs +13 -2
- package/src/files.mjs +52 -67
- package/src/guard.mjs +52 -6
- package/src/guardd.mjs +97 -0
- package/src/guardw.mjs +19 -0
- package/src/hogs.mjs +18 -5
- package/src/later.mjs +20 -25
- package/src/lock.mjs +292 -0
- package/src/measure.mjs +9 -4
- package/src/news.mjs +101 -0
- package/src/notes.mjs +171 -0
- package/src/notices.mjs +52 -13
- package/src/ondemand.mjs +6 -3
- package/src/override.mjs +60 -14
- package/src/plain.mjs +7 -4
- package/src/platform.mjs +165 -35
- package/src/preempt.mjs +54 -6
- package/src/primer.mjs +43 -15
- package/src/proc.mjs +39 -8
- package/src/progress.mjs +33 -4
- package/src/questions.mjs +8 -22
- package/src/relay.mjs +10 -22
- package/src/remote.mjs +12 -8
- package/src/schedule.mjs +182 -44
- package/src/serve.mjs +22 -3
- package/src/service.mjs +4 -3
- package/src/state.mjs +139 -47
- package/src/stats.mjs +3 -2
- package/src/talk.mjs +335 -73
- package/src/trust.mjs +7 -1
- package/src/util.mjs +28 -2
- package/src/version.mjs +1 -1
- package/src/web.mjs +19 -27
- package/src/worker.mjs +2 -1
package/src/alerts.mjs
CHANGED
|
@@ -7,13 +7,15 @@
|
|
|
7
7
|
// list; the app turns a new one into a notification, at most one per cause
|
|
8
8
|
// every 30 minutes.
|
|
9
9
|
//
|
|
10
|
-
//
|
|
10
|
+
// Seven causes:
|
|
11
11
|
// wait a job waiting far longer than usual, worst when a person waits on it
|
|
12
12
|
// memory memory pressure or swap holding new jobs back, with what to close
|
|
13
13
|
// idle a slot held by a job that has done nothing for a long time
|
|
14
14
|
// ended jobs that failed, were stopped, or were lost on another machine
|
|
15
15
|
// dibs dibs itself: a service down or restarting, a crash, the hook failing
|
|
16
16
|
// disk free disk under the floor
|
|
17
|
+
// machine what only the person can fix on the machine: cores that stopped taking
|
|
18
|
+
// work, processes left running with their script gone (src/cool.mjs)
|
|
17
19
|
//
|
|
18
20
|
// alerts.json keeps the open ones, the resolved ones for a day, and what the
|
|
19
21
|
// scan needs between runs: how far into the event log and the hook's log it
|
|
@@ -22,24 +24,31 @@
|
|
|
22
24
|
// throttle watcher, so alerts keep up even when nobody looks.
|
|
23
25
|
import fs from 'node:fs';
|
|
24
26
|
import path from 'node:path';
|
|
25
|
-
import { HOME, machine } from './config.mjs';
|
|
27
|
+
import { HOME, paths, machine } from './config.mjs';
|
|
26
28
|
import { verdict } from './schedule.mjs';
|
|
27
29
|
import { stuckFor, minutes, andList } from './plain.mjs';
|
|
28
|
-
import {
|
|
30
|
+
import { processesNow, memoryUse } from './platform.mjs';
|
|
29
31
|
import { treeOf } from './measure.mjs';
|
|
30
32
|
import { withState, history } from './state.mjs';
|
|
31
|
-
import { markIdle } from './acquire.mjs';
|
|
33
|
+
import { markIdle, tableFirst } from './acquire.mjs';
|
|
32
34
|
import { sessionFiles } from './proc.mjs';
|
|
33
35
|
import { EVENTS } from './events.mjs';
|
|
34
36
|
import { serviceHealth } from './service.mjs';
|
|
35
37
|
import { hogHint } from './hogs.mjs';
|
|
38
|
+
import { underLock, heldBy } from './lock.mjs';
|
|
36
39
|
import { now, bytes, clock, gb, die } from './util.mjs';
|
|
37
40
|
import { dim, red, amber, wrap } from './style.mjs';
|
|
38
41
|
|
|
39
42
|
export const FILE = path.join(HOME, 'alerts.json');
|
|
43
|
+
// The throttle watcher's notes on the machine (src/cool.mjs noteCpu).
|
|
44
|
+
export const CPU_FILE = path.join(HOME, 'cpu.json');
|
|
45
|
+
// Notes older than this are from a watcher that stopped: three of its passes.
|
|
46
|
+
const CPU_FRESH = 30e3;
|
|
40
47
|
const LOCK = path.join(HOME, 'alerts.lock');
|
|
41
48
|
const GUARD_LOG = path.join(HOME, 'guard.log');
|
|
42
49
|
const MIN = 6e4, HOUR = 36e5, DAY = 864e5;
|
|
50
|
+
// Stuck cores for this long, not a passing spike, before the person is told to restart.
|
|
51
|
+
const STUCK_AFTER = 5 * MIN;
|
|
43
52
|
|
|
44
53
|
// Reads are cheap but not free (a verdict per waiter, the tail of two logs);
|
|
45
54
|
// the app polls every 2 s, so a scan stands for this long.
|
|
@@ -63,14 +72,33 @@ const SERVICES_EVERY = MIN;
|
|
|
63
72
|
export const RESOLVED_SHOWN = 2 * HOUR;
|
|
64
73
|
// The first read of the event log starts this far from its end, not at its start: the log runs to 20 MB.
|
|
65
74
|
const FIRST_READ = 2 * 1024 ** 2;
|
|
66
|
-
// The rules a waiter is held by when memory, not a turn, is in the way.
|
|
67
|
-
const
|
|
75
|
+
// The rules a waiter is held by when the machine's memory, not a turn or dibs's budget, is in the way.
|
|
76
|
+
const PRESSURE = new Set(['pressure', 'swap', 'swaproom', 'ram', 'oversize']);
|
|
77
|
+
// Bumped when what makes an alert changes, so an upgrade rebuilds the open ones instead of closing them.
|
|
78
|
+
const RULES = 2;
|
|
79
|
+
// The same command failing this many times in a row, with no pass between, is worth the person's attention.
|
|
80
|
+
const REPEATS = 3;
|
|
68
81
|
// Exit codes a stopped command dies with: Ctrl-C, SIGKILL, SIGTERM.
|
|
69
82
|
const STOPPED = new Set([130, 137, 143]);
|
|
70
83
|
|
|
84
|
+
// A time as the person reads it: the clock today, with the weekday within a
|
|
85
|
+
// week either way, with the date further off. "at 6:40 PM" from yesterday
|
|
86
|
+
// read as a time still to come.
|
|
87
|
+
export function stamp(at, t = now()) {
|
|
88
|
+
const d = new Date(at);
|
|
89
|
+
if (d.toDateString() === new Date(t).toDateString()) return clock(at);
|
|
90
|
+
const day = Math.abs(t - at) < 6 * DAY ? d.toLocaleDateString([], { weekday: 'short' }) : d.toLocaleDateString([], { month: 'short', day: 'numeric' });
|
|
91
|
+
return `${day} ${clock(at)}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
71
94
|
const what = x => (x.purpose || x.cmd || (x.res || []).join(', ') || 'a job').replace(/\s+/g, ' ').slice(0, 70);
|
|
72
95
|
const job = x => `${x.who}'s #${x.id} (${what(x)})`;
|
|
73
96
|
const personWaits = q => q.prio === 'urgent' || q.prio === 'high' || typeof q.by === 'number';
|
|
97
|
+
const isDeploy = res => (res || []).some(r => r === 'deploy' || r.startsWith('deploy-') || r.startsWith('deploy:'));
|
|
98
|
+
// A failure the person cares about: a deploy, or a job they were waiting on.
|
|
99
|
+
// Held by the machine's memory: pressure or swap, or a job that fits dibs's budget but not what's really free.
|
|
100
|
+
const pressed = v => PRESSURE.has(v.rule) || (v.rule === 'memory' && (v.pressure || v.real));
|
|
101
|
+
const matters = x => isDeploy(x.res) || x.prio === 'urgent' || x.prio === 'high' || typeof x.due === 'number';
|
|
74
102
|
|
|
75
103
|
function load() {
|
|
76
104
|
try { return { seen: {}, open: {}, resolved: [], dismissed: {}, waits: {}, ended: [], svc: {}, restarts: [], hook: [], ...JSON.parse(fs.readFileSync(FILE, 'utf8')) }; }
|
|
@@ -84,13 +112,9 @@ function save(st) {
|
|
|
84
112
|
}
|
|
85
113
|
|
|
86
114
|
// One scan at a time. Whoever finds the lock taken shows what the file already
|
|
87
|
-
// says; a lock left by a process that died is
|
|
115
|
+
// says; a lock left by a process that died is taken over at once (src/lock.mjs).
|
|
88
116
|
function locked(fn) {
|
|
89
|
-
|
|
90
|
-
if (e.code !== 'EEXIST') return null;
|
|
91
|
-
try { if (now() - fs.statSync(LOCK).mtimeMs < 30e3) return null; fs.rmSync(LOCK, { recursive: true, force: true }); fs.mkdirSync(LOCK); } catch { return null; }
|
|
92
|
-
}
|
|
93
|
-
try { return fn(); } finally { fs.rmSync(LOCK, { recursive: true, force: true }); }
|
|
117
|
+
return underLock(LOCK, () => fn(), { wait: 0 })?.out ?? null;
|
|
94
118
|
}
|
|
95
119
|
|
|
96
120
|
// The lines a log gained since the offset last read. A file shorter than the
|
|
@@ -131,9 +155,11 @@ function readEvents(st, t) {
|
|
|
131
155
|
// A later success of the same command closes its failures.
|
|
132
156
|
if (e.code === 0 && e.key) for (const x of st.ended) if (x.key === e.key && !x.fixedAt) x.fixedAt = e.at;
|
|
133
157
|
const by = stops.get(e.id);
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
158
|
+
const x = { at: e.at, id: e.id, who: e.who, res: e.res, cmd: e.cmd, purpose: e.purpose, key: e.key, code: e.code, prio: e.prio, due: e.due };
|
|
159
|
+
if (by || STOPPED.has(e.code)) st.ended.push({ kind: 'stopped', ...x, by: by || null });
|
|
160
|
+
else if (e.code != null && e.code !== 0) st.ended.push({ kind: 'failed', ...x });
|
|
161
|
+
} else if (e.kind === 'lockstuck') st.ended.push({ kind: e.kind, at: e.at, key: e.key, pid: e.pid, cmd: e.cmd, held: e.held });
|
|
162
|
+
else if (e.kind === 'lost' || e.kind === 'crash') st.ended.push({ kind: e.kind, at: e.at, who: e.who, cmd: e.cmd, code: e.code, why: e.why, verb: e.verb });
|
|
137
163
|
}
|
|
138
164
|
st.ended = st.ended.filter(x => t - x.at < EVENT_KEEP);
|
|
139
165
|
}
|
|
@@ -176,28 +202,35 @@ export function memoryUsers(s, procs, use) {
|
|
|
176
202
|
const bytes = use.get(p.pid);
|
|
177
203
|
if (!bytes) continue;
|
|
178
204
|
const l = owner.get(p.pid);
|
|
179
|
-
const name = l ? `dibs job #${l.id} (${l.who})` : appOf(p, sessions);
|
|
180
|
-
const g = groups.get(name) || { name, bytes: 0, n: 0, dibs: !!l, ...(l && { id: l.id }) };
|
|
205
|
+
const [name, cmd] = l ? [`dibs job #${l.id} (${l.who})`] : appOf(p, sessions);
|
|
206
|
+
const g = groups.get(name) || { name, bytes: 0, n: 0, dibs: !!l, ...(l && { id: l.id }), ...(cmd && { cmd: true }) };
|
|
181
207
|
g.bytes += bytes; g.n++;
|
|
182
208
|
groups.set(name, g);
|
|
183
209
|
}
|
|
184
210
|
return [...groups.values()].sort((a, b) => b.bytes - a.bytes).slice(0, 5);
|
|
185
211
|
}
|
|
186
212
|
|
|
213
|
+
// [name, true when it's named by its command]: one of those may be many separate processes.
|
|
187
214
|
function appOf(p, sessions) {
|
|
188
215
|
const a = p.args || '';
|
|
189
|
-
if (/\blaunchd_sim\b|\/CoreSimulator\/Devices\//.test(a)) return 'the iOS Simulator';
|
|
190
|
-
if (/com\.apple\.WebKit\.(WebContent|GPU|Networking)/.test(a)) return 'Safari and other web pages';
|
|
216
|
+
if (/\blaunchd_sim\b|\/CoreSimulator\/Devices\/|\.simruntime\//.test(a)) return ['the iOS Simulator'];
|
|
217
|
+
if (/com\.apple\.WebKit\.(WebContent|GPU|Networking)/.test(a)) return ['Safari and other web pages'];
|
|
191
218
|
if (/(^|\/)claude(\s|$)/.test(a)) {
|
|
192
219
|
const x = sessions.get(p.pid);
|
|
193
|
-
return x?.name ? `Claude Code session ${x.name}` : `Claude Code (pid ${p.pid})
|
|
220
|
+
return [x?.name ? `Claude Code session ${x.name}` : `Claude Code (pid ${p.pid})`];
|
|
194
221
|
}
|
|
195
222
|
const app = a.match(/\/([^/]+)\.app\//);
|
|
196
|
-
if (app) return app[1];
|
|
197
|
-
|
|
223
|
+
if (app) return [app[1]];
|
|
224
|
+
// A script under an interpreter is the script: "node" alone says nothing about which.
|
|
225
|
+
const [cmd, ...args] = a.split(/\s+/);
|
|
226
|
+
const name = path.basename(cmd || `pid ${p.pid}`);
|
|
227
|
+
const script = INTERPRETERS.test(name) && args.find(x => !x.startsWith('-'));
|
|
228
|
+
return [script ? `${name} ${path.basename(script)}` : name, true];
|
|
198
229
|
}
|
|
199
230
|
|
|
200
|
-
const
|
|
231
|
+
const INTERPRETERS = /^(node|bun|deno|python3?(\.\d+)?|ruby|perl)$/;
|
|
232
|
+
// A command's count when it's more than one: 25 short-lived curls (2026-11-05) read as one big curl otherwise.
|
|
233
|
+
const sized = g => `${g.name} (${gb(g.bytes).replace(/G$/, ' GB')}${g.n > 1 && g.name.startsWith('Safari') ? `, ${g.n} pages` : g.n > 1 && g.cmd ? `, ${g.n} processes` : ''})`;
|
|
201
234
|
|
|
202
235
|
// Every alert the state calls for now: { id, cause, level, title, fix, after?, at? }.
|
|
203
236
|
// level: urgent (a person is held up or dibs is broken), warn, or note.
|
|
@@ -213,25 +246,24 @@ export function detect({ s, cfg, hist, m, t, st, users }) {
|
|
|
213
246
|
const usual = Math.max(0, ...q.res.map(r => median(st.waits[r] || [])));
|
|
214
247
|
const waited = t - q.enq;
|
|
215
248
|
if (waited < Math.max(WAIT_FLOOR, WAIT_TIMES * usual)) continue;
|
|
216
|
-
out.push({ id: `wait:#${q.id}`, cause: 'wait', level: personWaits(q) ? 'urgent' : 'warn', job: q.id, who: q.who,
|
|
217
|
-
|
|
218
|
-
|
|
249
|
+
out.push({ id: `wait:#${q.id}`, cause: 'wait', level: personWaits(q) ? 'urgent' : 'warn', job: q.id, who: q.who, at: q.enq,
|
|
250
|
+
...(pressed(v) && !users && { want: 'users' }),
|
|
251
|
+
title: `${job(q)} has waited ${minutes(waited)}${usual >= MIN ? `, where ${q.res[0]} usually takes ${minutes(usual)}` : ''}${personWaits(q) ? `, and it's ${typeof q.by === 'number' ? `due by ${stamp(q.by, t)}` : q.prio}` : ''}`,
|
|
252
|
+
why: v.why, fix: waitFix(s, q, v, t, users) });
|
|
219
253
|
}
|
|
220
254
|
|
|
221
255
|
// Memory holding new jobs back.
|
|
222
|
-
const held = waiting.filter(q =>
|
|
256
|
+
const held = waiting.filter(q => pressed(verdicts.get(q.id)));
|
|
223
257
|
if (held.length) {
|
|
224
258
|
const swap = held.some(q => /^swap/.test(verdicts.get(q.id).rule));
|
|
225
259
|
const list = users || [];
|
|
226
260
|
const close = list.filter(g => !g.dibs);
|
|
227
261
|
const ours = list.filter(g => g.dibs);
|
|
228
|
-
out.push({ id: 'memory', cause: 'memory', level: 'urgent', after: PRESSURE_AFTER,
|
|
262
|
+
out.push({ id: 'memory', cause: 'memory', level: 'urgent', after: PRESSURE_AFTER, at: Math.min(...held.map(q => q.enq)), ...(!users && { want: 'users' }),
|
|
229
263
|
title: `${swap ? 'Swap' : 'Memory pressure'} is holding back ${held.length} job${held.length > 1 ? 's' : ''} (${andList(held.slice(0, 3).map(q => `#${q.id}`))}${held.length > 3 ? ' and more' : ''})`,
|
|
230
264
|
why: verdicts.get(held[0].id).why,
|
|
231
265
|
detail: list.length ? `Using the most memory: ${list.map(sized).join(', ')}.` : '',
|
|
232
|
-
fix:
|
|
233
|
-
? `Close what you can of ${andList(close.slice(0, 3).map(sized))}. dibs won't close apps it didn't start.${ours.length ? ` Or stop a dibs job: dibs stop ${ours[0].id}.` : ''}`
|
|
234
|
-
: ours.length ? `dibs's own jobs use it: dibs stop ${ours[0].id} frees the most.` : 'Close apps you aren\'t using; dibs won\'t close what it didn\'t start.' });
|
|
266
|
+
fix: memoryFix(list) });
|
|
235
267
|
}
|
|
236
268
|
|
|
237
269
|
// A slot held by a run that has used no CPU for a long while someone waits for it.
|
|
@@ -240,38 +272,58 @@ export function detect({ s, cfg, hist, m, t, st, users }) {
|
|
|
240
272
|
if (!idle) continue;
|
|
241
273
|
const behind = waiting.filter(q => q.res.some(r => l.res.includes(r)));
|
|
242
274
|
if (!behind.length) continue;
|
|
243
|
-
out.push({ id: `idle:#${l.id}`, cause: 'idle', level: behind.some(personWaits) ? 'urgent' : 'warn', job: l.id, who: l.who,
|
|
275
|
+
out.push({ id: `idle:#${l.id}`, cause: 'idle', level: behind.some(personWaits) ? 'urgent' : 'warn', job: l.id, who: l.who, at: l.idleSince,
|
|
244
276
|
title: `${job(l)} has held ${l.res.join(', ')} for ${minutes(t - l.start)} and used no CPU for ${minutes(idle)}, while ${behind.length} job${behind.length > 1 ? 's wait' : ' waits'}`,
|
|
245
277
|
fix: `If it's stuck, stop it: dibs stop ${l.id} --owner ${l.who}. If it's working somewhere dibs can't see, leave it.` });
|
|
246
278
|
}
|
|
247
279
|
|
|
248
|
-
// Jobs that failed, were stopped, or were lost, and dibs's own crashes.
|
|
280
|
+
// Jobs that failed, were stopped, or were lost, and dibs's own crashes. An
|
|
281
|
+
// agent's test run failing while it works is normal: on 2026-11-03 one
|
|
282
|
+
// session's 53 failed test runs filled the list. What's loud is what the
|
|
283
|
+
// person cares about: a deploy, a job a person waits on (high, urgent or
|
|
284
|
+
// --by), the same command failing REPEATS times in a row, a lost job and a
|
|
285
|
+
// crash. The rest is quiet: in dibs alerts --all only.
|
|
249
286
|
const failed = new Map();
|
|
250
287
|
for (const x of st.ended) {
|
|
251
288
|
if (x.fixedAt) continue;
|
|
252
|
-
if (x.kind === 'failed') {
|
|
253
|
-
|
|
289
|
+
if (x.kind === 'failed' && matters(x)) {
|
|
290
|
+
out.push({ id: `failed:#${x.id}`, cause: 'ended', level: 'warn', at: x.at, job: x.id, who: x.who,
|
|
291
|
+
title: `${job(x)} failed at ${stamp(x.at, t)} (exit ${x.code})${x.due ? `, and it was due by ${stamp(x.due, t)}` : x.prio === 'urgent' || x.prio === 'high' ? `, and it was ${x.prio}` : ''}`,
|
|
292
|
+
fix: `${x.who} is the one to fix it; this closes when the same command passes.` });
|
|
293
|
+
} else if (x.kind === 'failed') {
|
|
294
|
+
// Failures of the exact same command, all since its last pass (a pass closes the ones before it).
|
|
295
|
+
const k = `${x.who}|${x.cmd || (x.res || []).join(',')}`;
|
|
254
296
|
const g = failed.get(k) || { ...x, n: 0, first: x.at };
|
|
255
297
|
g.n++; g.at = Math.max(g.at, x.at); g.code = x.code;
|
|
256
298
|
failed.set(k, g);
|
|
257
299
|
} else if (x.kind === 'stopped') {
|
|
258
|
-
|
|
259
|
-
|
|
300
|
+
// Stopped on purpose (exit 143 or 137, or dibs stop): on record in dibs alerts --all, nowhere louder.
|
|
301
|
+
out.push({ id: `stopped:#${x.id}`, cause: 'ended', level: 'note', at: x.at, job: x.id, who: x.who, quiet: true,
|
|
302
|
+
title: `${job(x)} was stopped${x.by ? ` by ${x.by}` : ''} at ${stamp(x.at, t)}${x.code != null ? ` (exit ${x.code})` : ''}`,
|
|
260
303
|
fix: `If it should finish, ${x.who} runs it again; nothing else waits on it.` });
|
|
261
304
|
} else if (x.kind === 'lost') {
|
|
262
305
|
out.push({ id: `lost:${x.at}`, cause: 'ended', level: 'urgent', at: x.at, who: x.who,
|
|
263
|
-
title: `${x.who}'s job was lost${x.code === 77 ? ' after it started' : ' before it started'} at ${
|
|
306
|
+
title: `${x.who}'s job was lost${x.code === 77 ? ' after it started' : ' before it started'} at ${stamp(x.at, t)}: ${x.why || x.cmd}`,
|
|
264
307
|
fix: x.code === 77 ? 'It may have run. Check what it changed before running it again.' : 'It never ran, so it\'s safe to run again.' });
|
|
308
|
+
} else if (x.kind === 'lockstuck') {
|
|
309
|
+
// Open while that same holder still has the lock; it closes the moment it lets go.
|
|
310
|
+
const now = heldBy(paths.lock);
|
|
311
|
+
if (now?.key !== x.key) continue;
|
|
312
|
+
out.push({ id: `lockstuck:${x.key}`, cause: 'dibs', level: 'urgent', at: x.at - x.held,
|
|
313
|
+
title: `dibs is stuck behind pid ${x.pid}${x.cmd ? ` (${x.cmd.slice(0, 80)})` : ''}: it has held the state lock for ${minutes(now.held)}, and every dibs command waits for it`,
|
|
314
|
+
fix: `If pid ${x.pid} is hung, end it and everyone goes on at once: kill ${x.pid}` });
|
|
265
315
|
} else if (x.kind === 'crash') {
|
|
266
316
|
out.push({ id: `crash:${x.at}`, cause: 'dibs', level: 'urgent', at: x.at, who: x.who,
|
|
267
|
-
title: `dibs crashed at ${
|
|
317
|
+
title: `dibs crashed at ${stamp(x.at, t)} running dibs ${x.verb || ''} for ${x.who}`,
|
|
268
318
|
fix: `It's a bug in dibs. Report it: dibs ask "crash in dibs ${x.verb || ''}"` });
|
|
269
319
|
}
|
|
270
320
|
}
|
|
271
|
-
for (const [k, g] of failed)
|
|
272
|
-
|
|
273
|
-
|
|
321
|
+
for (const [k, g] of failed) {
|
|
322
|
+
const loud = g.cmd && g.n >= REPEATS;
|
|
323
|
+
out.push({ id: `failed:${hash(k)}`, cause: 'ended', level: loud ? 'warn' : 'note', at: g.first, last: g.at, who: g.who, ...(!loud && { quiet: true }),
|
|
324
|
+
title: `${g.who}'s ${what(g)} failed${g.n > 1 ? ` ${g.n} times in a row, last` : ''} at ${stamp(g.at, t)} (exit ${g.code})`,
|
|
274
325
|
fix: `${g.who} is the one to fix it; this closes when the same command passes.` });
|
|
326
|
+
}
|
|
275
327
|
|
|
276
328
|
// dibs itself.
|
|
277
329
|
for (const [k, h] of Object.entries(st.svc || {}))
|
|
@@ -283,27 +335,47 @@ export function detect({ s, cfg, hist, m, t, st, users }) {
|
|
|
283
335
|
const rs = st.restarts.filter(r => r.k === k);
|
|
284
336
|
const last = rs[rs.length - 1];
|
|
285
337
|
out.push({ id: `restart:${k}`, cause: 'dibs', level: 'warn', at: rs[0].at,
|
|
286
|
-
title: `dibs's ${k} service crashed (exit ${last.exit}) and launchd started it again${rs.length > 1 ? `, ${rs.length} times in the last hour` : ''}, last at ${
|
|
338
|
+
title: `dibs's ${k} service crashed (exit ${last.exit}) and launchd started it again${rs.length > 1 ? `, ${rs.length} times in the last hour` : ''}, last at ${stamp(last.at, t)}`,
|
|
287
339
|
fix: `Look in ~/.dibs/${k}.log for why, and report it: dibs ask "the ${k} service keeps exiting with ${last.exit}"` });
|
|
288
340
|
}
|
|
289
341
|
if (st.hook?.length) {
|
|
290
342
|
const last = st.hook[st.hook.length - 1];
|
|
291
343
|
out.push({ id: 'hook', cause: 'dibs', level: 'warn', at: st.hook[0].at,
|
|
292
|
-
title: `The Claude Code hook failed ${st.hook.length} time${st.hook.length > 1 ? 's' : ''} in the last hour, last at ${
|
|
344
|
+
title: `The Claude Code hook failed ${st.hook.length} time${st.hook.length > 1 ? 's' : ''} in the last hour, last at ${stamp(last.at, t)}: ${last.msg}`,
|
|
293
345
|
fix: `Those tool calls ran without dibs's checks. Report it: dibs ask "hook: ${last.msg.slice(0, 60)}"` });
|
|
294
346
|
}
|
|
295
347
|
|
|
348
|
+
// The hard stop (src/preempt.mjs hardStop): a job paused under 1 GB free, whatever it was.
|
|
349
|
+
for (const l of s.leases.filter(x => x.pauses?.some(p => p.hard)))
|
|
350
|
+
out.push({ id: `hardstop:#${l.id}`, cause: 'disk', level: 'urgent', job: l.id, who: l.who, at: l.pausedAt,
|
|
351
|
+
title: `Free disk fell under 1 GB, so dibs paused ${job(l)}, the job writing the most. It resumes once 2 GB is free`,
|
|
352
|
+
fix: diskFix() });
|
|
353
|
+
|
|
354
|
+
// The machine itself, from the throttle watcher.
|
|
355
|
+
let cpu = null;
|
|
356
|
+
try { cpu = JSON.parse(fs.readFileSync(CPU_FILE, 'utf8')); } catch {}
|
|
357
|
+
if (cpu && t - cpu.at < CPU_FRESH) {
|
|
358
|
+
if (cpu.stuckSince && !cpu.swapping)
|
|
359
|
+
out.push({ id: 'cores', cause: 'machine', level: 'urgent', after: STUCK_AFTER, at: cpu.stuckSince,
|
|
360
|
+
title: `The load is ${cpu.load} on ${cpu.cores} cores while ${cpu.idle}% of the CPU sits idle: work is ready to run and some cores have stopped taking it`,
|
|
361
|
+
fix: 'Restart the Mac. On 2026-11-10 nothing else brought its cores back. Until then dibs moves nothing to background priority.' });
|
|
362
|
+
for (const o of cpu.orphans || [])
|
|
363
|
+
out.push({ id: `orphan:${o.pid}`, cause: 'machine', level: 'warn',
|
|
364
|
+
title: `${o.name} ${o.pid} has run ${Math.round(o.age / HOUR)} hours outside dibs, using ${Math.round(o.cpu / HOUR)} CPU-hours, and its script ${o.script} is gone`,
|
|
365
|
+
fix: `Likely left behind by a session that ended. dibs leaves it running; if nobody needs it, end it: kill ${o.pid}` });
|
|
366
|
+
}
|
|
367
|
+
|
|
296
368
|
// Disk under the floor: nothing new starts.
|
|
297
369
|
const floor = bytes(cfg.minDisk || '0');
|
|
298
370
|
if (floor && m.diskFree != null && m.diskFree < floor)
|
|
299
371
|
out.push({ id: 'disk', cause: 'disk', level: 'urgent',
|
|
300
|
-
title: `Free disk is ${gb(m.diskFree).replace(/G$/, ' GB')}, under the ${gb(floor).replace(/G$/, ' GB')} dibs keeps free, so
|
|
372
|
+
title: `Free disk is ${gb(m.diskFree).replace(/G$/, ' GB')}, under the ${gb(floor).replace(/G$/, ' GB')} dibs keeps free, so only jobs that write next to nothing start; the rest wait for room`,
|
|
301
373
|
fix: diskFix() });
|
|
302
374
|
return out;
|
|
303
375
|
}
|
|
304
376
|
|
|
305
377
|
// The one thing that lets a long-waiting job start, by what holds it.
|
|
306
|
-
function waitFix(s, q, v, t) {
|
|
378
|
+
function waitFix(s, q, v, t, users) {
|
|
307
379
|
const boost = `dibs boost ${q.id} urgent -m "why"`;
|
|
308
380
|
if (v.rule === 'slot' || v.rule === 'files' || v.rule === 'together') {
|
|
309
381
|
// With one deploy per project (the per-repo lock), the one in the way is this project's.
|
|
@@ -313,7 +385,12 @@ function waitFix(s, q, v, t) {
|
|
|
313
385
|
if (l) return `${job(l)} has held ${v.on} for ${minutes(t - l.start)}. If ${q.who}'s job can't wait, ${boost} puts it first in line; ${l.who} decides whether to stop theirs.`;
|
|
314
386
|
}
|
|
315
387
|
if (v.rule === 'ahead' && v.ahead) return `${job(v.ahead)} is ahead. If this one matters more: ${boost}.`;
|
|
316
|
-
|
|
388
|
+
// What uses the memory, said here: an alert that points at another one that isn't there is no help.
|
|
389
|
+
if (pressed(v)) return memoryFix(users || []);
|
|
390
|
+
if (v.rule === 'memory') {
|
|
391
|
+
const big = (s.leases || []).filter(l => l.mem).sort((a, b) => b.mem - a.mem).slice(0, 3);
|
|
392
|
+
return `dibs's memory budget is handed out to running jobs${big.length ? `, the biggest ${andList(big.map(l => `${job(l)} with ${gb(l.mem).replace(/G$/, ' GB')}`))}` : ''}. It starts as they end; if it can't wait: ${boost}.`;
|
|
393
|
+
}
|
|
317
394
|
if (v.rule === 'disk' || v.rule === 'drain') return diskFix();
|
|
318
395
|
if (v.rule === 'load' || v.rule === 'cpu') return `The machine is busy. If a person waits on this: dibs boost ${q.id} high -m "why".`;
|
|
319
396
|
if (v.rule === 'idle') return `It's idle-priority work, which waits for everything else: dibs boost ${q.id} normal.`;
|
|
@@ -321,6 +398,14 @@ function waitFix(s, q, v, t) {
|
|
|
321
398
|
return `${v.why}. If it can't wait: ${boost}.`;
|
|
322
399
|
}
|
|
323
400
|
|
|
401
|
+
// What to close, from the biggest memory users on the machine.
|
|
402
|
+
function memoryFix(list) {
|
|
403
|
+
const close = list.filter(g => !g.dibs), ours = list.filter(g => g.dibs);
|
|
404
|
+
if (close.length) return `Close what you can of ${andList(close.slice(0, 3).map(sized))}. dibs won't close apps it didn't start.${ours.length ? ` Or stop a dibs job: dibs stop ${ours[0].id}.` : ''}`;
|
|
405
|
+
if (ours.length) return `dibs's own jobs use it: dibs stop ${ours[0].id} frees the most.`;
|
|
406
|
+
return "Close apps you aren't using; dibs won't close what it didn't start.";
|
|
407
|
+
}
|
|
408
|
+
|
|
324
409
|
function diskFix() {
|
|
325
410
|
return `Free disk: build output, caches and scratch folders${hogHint()}.`;
|
|
326
411
|
}
|
|
@@ -332,11 +417,12 @@ const hash = k => { let h = 5381; for (const c of String(k)) h = ((h << 5) + h +
|
|
|
332
417
|
export function merge(st, found, t) {
|
|
333
418
|
const live = new Set();
|
|
334
419
|
for (const c of found) {
|
|
335
|
-
|
|
420
|
+
// Since when the problem itself began (the job queued, the first failure), not this scan.
|
|
421
|
+
if (c.after && t - (st.seen[c.id] ??= c.at ?? t) < c.after) continue;
|
|
336
422
|
if (st.dismissed[c.id]) continue;
|
|
337
423
|
live.add(c.id);
|
|
338
|
-
const { after, ...a } = c;
|
|
339
|
-
st.open[c.id] = { ...a, since: st.open[c.id]?.since ??
|
|
424
|
+
const { after, want, at, ...a } = c;
|
|
425
|
+
st.open[c.id] = { ...a, since: at ?? st.open[c.id]?.since ?? (after ? st.seen[c.id] : t), resolvedAt: null };
|
|
340
426
|
}
|
|
341
427
|
const pending = new Set(found.map(c => c.id));
|
|
342
428
|
for (const id of Object.keys(st.seen)) if (!pending.has(id)) delete st.seen[id];
|
|
@@ -358,14 +444,18 @@ export function scan({ s, cfg, hist, m = machine(), t = now(), force = false } =
|
|
|
358
444
|
if (!force && was.at && t - was.at < SCAN_EVERY) return shown(was, t);
|
|
359
445
|
const st = locked(() => {
|
|
360
446
|
const st = load();
|
|
447
|
+
// Alerts made by older rules aren't resolved, only no longer said that way: start the open list over.
|
|
448
|
+
if (st.rules !== RULES) { st.open = {}; st.seen = {}; st.rules = RULES; }
|
|
361
449
|
readEvents(st, t);
|
|
362
450
|
readHook(st, t);
|
|
363
451
|
readServices(st, t);
|
|
364
452
|
let users = st.top && t - st.top.at < TOP_EVERY ? st.top.list : null;
|
|
365
453
|
let found = detect({ s, cfg, hist, m, t, st, users });
|
|
366
454
|
// The machine-wide reading only once memory is what holds jobs back.
|
|
367
|
-
if (!users && found.some(a => a.
|
|
368
|
-
|
|
455
|
+
if (!users && found.some(a => a.want === 'users')) {
|
|
456
|
+
// Memory first, then the process table it's named from: both as they are now.
|
|
457
|
+
const use = memoryUse();
|
|
458
|
+
users = memoryUsers(s, processesNow(), use);
|
|
369
459
|
st.top = { at: t, list: users };
|
|
370
460
|
found = detect({ s, cfg, hist, m, t, st, users });
|
|
371
461
|
}
|
|
@@ -386,8 +476,8 @@ export function scan({ s, cfg, hist, m = machine(), t = now(), force = false } =
|
|
|
386
476
|
// Open alerts, worst first, then the ones resolved in the last RESOLVED_SHOWN.
|
|
387
477
|
const RANK = { urgent: 0, warn: 1, note: 2 };
|
|
388
478
|
export function shown(st, t = now(), all = false) {
|
|
389
|
-
const open = Object.values(st.open || {}).sort((a, b) => RANK[a.level] - RANK[b.level] || b.since - a.since);
|
|
390
|
-
const gone = (st.resolved || []).filter(a => all || t - a.resolvedAt < RESOLVED_SHOWN).sort((a, b) => b.resolvedAt - a.resolvedAt);
|
|
479
|
+
const open = Object.values(st.open || {}).filter(a => all || !a.quiet).sort((a, b) => RANK[a.level] - RANK[b.level] || b.since - a.since);
|
|
480
|
+
const gone = (st.resolved || []).filter(a => all || (!a.quiet && t - a.resolvedAt < RESOLVED_SHOWN)).sort((a, b) => b.resolvedAt - a.resolvedAt);
|
|
391
481
|
return [...open, ...gone];
|
|
392
482
|
}
|
|
393
483
|
|
|
@@ -403,15 +493,21 @@ export function dismiss(which, t = now()) {
|
|
|
403
493
|
}) || [];
|
|
404
494
|
}
|
|
405
495
|
|
|
496
|
+
// The plain board is what agents read between steps: a failure, a stop or a
|
|
497
|
+
// lost job is on it for this long after it happened, then only in the alerts
|
|
498
|
+
// (the page, the app, dibs alerts), which the person reads.
|
|
499
|
+
export const BOARD_KEEP = 30 * MIN;
|
|
500
|
+
export const onBoard = (a, t = now()) => a.cause !== 'ended' || t - (a.last ?? a.since) < BOARD_KEEP;
|
|
501
|
+
|
|
406
502
|
// The board's lines for the alerts: each open one with its fix, worst first,
|
|
407
503
|
// then the ones resolved lately, with when.
|
|
408
504
|
export function alertLines(list, t = now()) {
|
|
409
505
|
const out = [];
|
|
410
506
|
for (const a of list) {
|
|
411
|
-
if (a.resolvedAt) out.push(dim(`Resolved at ${
|
|
507
|
+
if (a.resolvedAt) out.push(dim(`Resolved at ${stamp(a.resolvedAt, t)}${a.dismissed ? ' (dismissed)' : ''}: ${a.title}.`));
|
|
412
508
|
else {
|
|
413
509
|
const paint = a.level === 'urgent' ? red : a.level === 'warn' ? amber : x => x;
|
|
414
|
-
out.push(paint(`${a.level === 'note' ? '' : '! '}${a.title} (since ${
|
|
510
|
+
out.push(paint(`${a.level === 'note' ? '' : '! '}${a.title} (since ${stamp(a.since, t)}).`), wrap(` Fix: ${a.fix}`, ' '));
|
|
415
511
|
}
|
|
416
512
|
}
|
|
417
513
|
return out;
|
|
@@ -426,12 +522,16 @@ export function alerts(args, cfg) {
|
|
|
426
522
|
: `No open alert matches ${which}. dibs alerts lists them.`);
|
|
427
523
|
}
|
|
428
524
|
const t = now();
|
|
525
|
+
tableFirst();
|
|
429
526
|
const s = withState(s => { markIdle(s); return s; });
|
|
430
527
|
let list = scan({ s, cfg, hist: history(), t, force: true });
|
|
431
528
|
if (args.includes('--all')) list = shown(load(), t, true);
|
|
432
529
|
if (args.includes('--json')) return console.log(JSON.stringify(list, null, 1));
|
|
433
|
-
|
|
530
|
+
const quiet = args.includes('--all') ? 0 : Object.values(load().open).filter(a => a.quiet).length;
|
|
531
|
+
const more = quiet ? dim(`${quiet} more not worth your attention (agents' own test runs failing, jobs stopped on purpose): dibs alerts --all`) : '';
|
|
532
|
+
if (!list.length) return console.log(['Nothing is wrong that dibs can see. Open problems and what fixes them show here, and on the board.', more].filter(Boolean).join('\n'));
|
|
434
533
|
console.log(alertLines(list, t).join('\n'));
|
|
534
|
+
if (more) console.log(`\n${more}`);
|
|
435
535
|
const open = list.filter(a => !a.resolvedAt);
|
|
436
536
|
if (open.length) console.log(dim(`\nIds, for dibs alerts dismiss <id>: ${open.map(a => a.id).join(', ')}`));
|
|
437
537
|
}
|
package/src/answers.mjs
CHANGED
|
@@ -7,7 +7,7 @@ import path from 'node:path';
|
|
|
7
7
|
import { fileURLToPath } from 'node:url';
|
|
8
8
|
|
|
9
9
|
const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
10
|
-
const DOCS = ['skills/dibs/SKILL.md', 'README.md', ...['guard', 'files', 'questions', 'later', 'machines', 'simulators', 'scheduling', 'relay'].map(d => `docs/${d}.md`)];
|
|
10
|
+
const DOCS = ['skills/dibs/SKILL.md', 'README.md', ...['reference', 'guard', 'files', 'questions', 'later', 'machines', 'simulators', 'scheduling', 'relay'].map(d => `docs/${d}.md`)];
|
|
11
11
|
export const ISSUES = 'https://github.com/dibssoftware/dibs/issues';
|
|
12
12
|
// Two passages answer most questions; more is reading the docs.
|
|
13
13
|
const SHOW = 2;
|
|
@@ -24,7 +24,7 @@ const words = text => text.toLowerCase().match(/[a-z0-9][a-z0-9:_-]*/g)?.filter(
|
|
|
24
24
|
function pieces(block) {
|
|
25
25
|
const lines = block.split('\n');
|
|
26
26
|
if (lines.every(l => l.startsWith('|'))) return lines.filter(l => !/^\|[\s:|-]+\|$/.test(l)).slice(1);
|
|
27
|
-
//
|
|
27
|
+
// docs/reference.md's paste-in block for agents is one fence of bullets: each bullet answers on its own.
|
|
28
28
|
if (lines[0] === '```markdown') return lines.filter(l => l.startsWith('- '));
|
|
29
29
|
if (!/^(- |\d+\. )/.test(lines[0])) return [block];
|
|
30
30
|
const items = [];
|
package/src/board.mjs
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// joined to a coordinator, on every other machine too.
|
|
3
3
|
import { resCfg } from './config.mjs';
|
|
4
4
|
import { tag, isMine } from './proc.mjs';
|
|
5
|
-
import { idleFor } from './acquire.mjs';
|
|
6
|
-
import { forecast, byRank, remaining, used, overBy, verdict, memUsed, cpuUsed, shownFree, swapGrowing, diskSqueeze, BREAKER_LINE } from './schedule.mjs';
|
|
5
|
+
import { idleFor, UNPOLLED } from './acquire.mjs';
|
|
6
|
+
import { forecast, byRank, remaining, used, overBy, verdict, memUsed, cpuUsed, shownFree, swapGrowing, diskSqueeze, BREAKER_LINE, QUICK_LINE } from './schedule.mjs';
|
|
7
7
|
import { grewLine, cappedLine } from './measure.mjs';
|
|
8
8
|
import { pauseLabel } from './preempt.mjs';
|
|
9
9
|
import { alongsideLabel } from './overlap.mjs';
|
|
@@ -29,7 +29,6 @@ const blockedBy = v => { const r = waitReason(v); return r && !/^(behind|held by
|
|
|
29
29
|
// line and the machine, then a shared rate limit), or, when nothing does, that
|
|
30
30
|
// nobody has decided for it lately. On 2026-10-20 two test runs showed no
|
|
31
31
|
// reason at all and didn't start, with free slots and memory.
|
|
32
|
-
const UNPOLLED = 3 * 6e4; // a waiter's dibs decides every few seconds, and notes it once a minute (acquire.mjs POLLED_EVERY)
|
|
33
32
|
function waitingOn(s, q, cfg, hist, t) {
|
|
34
33
|
const v = verdict(s, q, cfg, hist, t);
|
|
35
34
|
if (!v.ok) return blockedBy(v);
|
|
@@ -55,7 +54,7 @@ export function boardOf(s, cfg, hist, t = now(), me = null) {
|
|
|
55
54
|
held: [...s.leases].sort((a, b) => a.start - b.start).map(l => ({
|
|
56
55
|
res: l.res.map(r => short(r)), who: l.who, label: l.label, agentId: l.agentId, for: t - l.start, left: remaining(l, hist, t), idle: idleFor(l, t),
|
|
57
56
|
stuck: s.queue.some(q => q.pid) && stuckFor(l, t) > 0,
|
|
58
|
-
prio: l.prio, paused: pauseLabel(l) || null, alongside: alongsideLabel(l) || (l.breaker ? BREAKER_LINE : null), owner: ownerLabel(l) || null, purpose: l.purpose, cmd: l.cmd ? l.cmd.slice(0, 40) : null,
|
|
57
|
+
prio: l.prio, paused: pauseLabel(l) || null, alongside: alongsideLabel(l) || (l.breaker ? BREAKER_LINE : l.quick ? QUICK_LINE : null), owner: ownerLabel(l) || null, purpose: l.purpose, cmd: l.cmd ? l.cmd.slice(0, 40) : null,
|
|
59
58
|
mem: l.mem, use: l.use?.mem ?? null, over: overBy(l) > 0, grew: grewLine(l) || null, ...you(l) })),
|
|
60
59
|
waiting: [...s.queue].sort(byRank(hist, t)).map(q => ({
|
|
61
60
|
res: q.res.map(r => short(r)), who: q.who, label: q.label, agentId: q.agentId, waited: t - q.enq, in: fc.has(q.id) ? fc.get(q.id) - t : null,
|
package/src/broker.mjs
CHANGED
|
@@ -11,12 +11,14 @@ import net from 'node:net';
|
|
|
11
11
|
import path from 'node:path';
|
|
12
12
|
import { spawn, spawnSync } from 'node:child_process';
|
|
13
13
|
import { fileURLToPath } from 'node:url';
|
|
14
|
-
import { HOME, paths, loadConfig } from './config.mjs';
|
|
14
|
+
import { HOME, paths, loadConfig, machine } from './config.mjs';
|
|
15
15
|
import { pstart, makeAlive } from './proc.mjs';
|
|
16
16
|
import { processes } from './platform.mjs';
|
|
17
17
|
import { withState, history, kick, quiet, KEEP_PLACE } from './state.mjs';
|
|
18
|
+
import { sweepLock } from './lock.mjs';
|
|
18
19
|
import { decide, step, waiter, full, markIdle, saidText, abandon, POLL } from './acquire.mjs';
|
|
19
20
|
import { byRank } from './schedule.mjs';
|
|
21
|
+
import { inversions, lift } from './override.mjs';
|
|
20
22
|
import { noticeScan } from './notices.mjs';
|
|
21
23
|
import { event } from './events.mjs';
|
|
22
24
|
import { now, noSpace, sleepSync, signalExit, notifyLine, LEAN } from './util.mjs';
|
|
@@ -186,17 +188,19 @@ function keepFor(q, shell) {
|
|
|
186
188
|
// up on the broker (why is keep: the place stays for its in-process retry).
|
|
187
189
|
// id 0: interrupted before it knew its id; its place is the one under its pid.
|
|
188
190
|
export function leaveLine(id, shell, why) {
|
|
189
|
-
|
|
191
|
+
// A low job lifted for this waiter alone goes back down as it leaves (src/override.mjs inversions).
|
|
192
|
+
lift(withState(s => {
|
|
190
193
|
// Its place by id when it knows it, else by its pid. A place kept with nobody on it (for its own
|
|
191
194
|
// retry in Node, or taken for gone) is its too: interrupted, it gives that up as well.
|
|
192
195
|
const q = s.queue.find(x => (id ? x.id === id && (x.pid === shell || (why !== 'keep' && !x.pid)) : x.pid === shell)
|
|
193
196
|
|| (why !== 'keep' && x.keptFor === shell));
|
|
194
|
-
if (!q) return;
|
|
195
|
-
if (why === 'keep')
|
|
197
|
+
if (!q) return [];
|
|
198
|
+
if (why === 'keep') { keepFor(q, shell); return []; }
|
|
196
199
|
s.queue = s.queue.filter(x => x !== q);
|
|
197
200
|
if (q.wake?.notify) notified(q, signalExit(why));
|
|
198
201
|
if (q.wake) event({ kind: 'left', id: q.id, res: q.res, who: q.who, prio: q.prio || 'normal', type: q.kind, ms: now() - q.enq, why });
|
|
199
|
-
|
|
202
|
+
return inversions(s, loadConfig());
|
|
203
|
+
}));
|
|
200
204
|
}
|
|
201
205
|
|
|
202
206
|
// --notify's line for a thin wait that ended before its command started (src/commands.mjs notifyOnExit).
|
|
@@ -260,7 +264,7 @@ export function broker() {
|
|
|
260
264
|
const ctl = new net.Socket({ fd: fs.openSync(paths.ctl, O_RDWR | O_NONBLOCK), readable: true, writable: false });
|
|
261
265
|
const code = fs.readFileSync(path.join(SRC, 'version.mjs'), 'utf8');
|
|
262
266
|
const waits = new Map(); // queue id -> its wait (src/acquire.mjs waiter), with its FIFO as out
|
|
263
|
-
let cfg = loadConfig(), hist = history(), histAt = 0, beat = 0, idleSince = now(), pending = false, forced = false, pruned = 0, seen = '',
|
|
267
|
+
let cfg = loadConfig(), hist = history(), histAt = 0, beat = 0, idleSince = now(), pending = false, forced = false, pruned = 0, seen = '', fullAt = 0;
|
|
264
268
|
|
|
265
269
|
const exit = () => {
|
|
266
270
|
for (const w of waits.values()) w.out.close();
|
|
@@ -284,18 +288,25 @@ export function broker() {
|
|
|
284
288
|
pending = forced = false;
|
|
285
289
|
const t = now();
|
|
286
290
|
// A release changed the code: the waiters' shells start the new one.
|
|
287
|
-
|
|
291
|
+
// Said in the log, so a waiter's "broker gone, starting another" that follows reads as a release, not a crash.
|
|
292
|
+
try {
|
|
293
|
+
if (fs.readFileSync(path.join(SRC, 'version.mjs'), 'utf8') !== code) {
|
|
294
|
+
event({ kind: 'thin', why: 'a new version of dibs: the broker hands its waiters to a new one', pid: me.pid, waiting: waits.size });
|
|
295
|
+
return leave(true);
|
|
296
|
+
}
|
|
297
|
+
} catch {}
|
|
288
298
|
if (t - pruned > PRUNE_EVERY) { pruned = t; prune(); }
|
|
289
299
|
// With nobody to wait for, the lock is taken only to keep the beat fresh.
|
|
290
300
|
let quietNow = false, text = '';
|
|
291
301
|
try { text = fs.readFileSync(paths.state, 'utf8'); quietNow = !waits.size && !thinWaiters(JSON.parse(text)).length; } catch {}
|
|
292
|
-
if (!quietNow && !kicked && text === seen && t -
|
|
302
|
+
if (!quietNow && !kicked && text === seen && t - fullAt < QUIET_POLL && [...waits.values()].every(w => running(w.me.pid))) return;
|
|
293
303
|
if (quietNow) {
|
|
294
304
|
if (t - idleSince > IDLE_EXIT) return leave(false);
|
|
295
305
|
if (t - beat > BEAT) withState(s => { if (s.broker?.pid === me.pid || !s.broker) s.broker = { ...me, beat: (beat = t) }; });
|
|
296
306
|
return;
|
|
297
307
|
}
|
|
298
|
-
processes(); // read the process table before taking the lock, not while holding it
|
|
308
|
+
processes(); // read the process table and the machine before taking the lock, not while holding it
|
|
309
|
+
machine();
|
|
299
310
|
noticeScan();
|
|
300
311
|
try { cfg = loadConfig(); } catch {}
|
|
301
312
|
try { const m = fs.statSync(paths.history).mtimeMs; if (m !== histAt) { histAt = m; hist = history(); } } catch {}
|
|
@@ -347,7 +358,7 @@ export function broker() {
|
|
|
347
358
|
}
|
|
348
359
|
}
|
|
349
360
|
done();
|
|
350
|
-
idleSince =
|
|
361
|
+
idleSince = fullAt = now();
|
|
351
362
|
try { seen = fs.readFileSync(paths.state, 'utf8'); } catch {}
|
|
352
363
|
};
|
|
353
364
|
const done = () => { for (const [id, w] of waits) if (w.out.done) waits.delete(id); };
|
|
@@ -358,8 +369,12 @@ export function broker() {
|
|
|
358
369
|
tick();
|
|
359
370
|
}
|
|
360
371
|
|
|
361
|
-
// FIFOs of shells that are gone. Only in ~/.dibs/w, the folder dibs made for them.
|
|
372
|
+
// FIFOs of shells that are gone. Only in ~/.dibs/w, the folder dibs made for them. And what writers
|
|
373
|
+
// killed mid-change left beside the state and message locks (src/lock.mjs sweepLock): a claim
|
|
374
|
+
// sweeps too, but only after a death it sees, and the broker is always around while anyone waits.
|
|
362
375
|
function prune() {
|
|
376
|
+
sweepLock(paths.lock, [paths.state]);
|
|
377
|
+
sweepLock(path.join(HOME, 'messages.lock'), [path.join(HOME, 'messages.json'), path.join(HOME, 'inbox.json'), path.join(HOME, 'listeners.json')]);
|
|
363
378
|
let names = [];
|
|
364
379
|
try { names = fs.readdirSync(WAKE); } catch { return; }
|
|
365
380
|
for (const n of names) {
|