@dibssoftware/dibs 0.0.180 → 0.0.195
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +111 -1
- package/README.md +10 -8
- package/bin/dibs.mjs +379 -74
- package/docs/agents.md +303 -0
- package/docs/council.md +88 -0
- package/docs/files.md +3 -3
- package/docs/guard.md +86 -11
- package/docs/json.md +211 -0
- package/docs/later.md +5 -5
- package/docs/machines.md +6 -6
- package/docs/questions.md +104 -3
- package/docs/reference.md +124 -30
- package/docs/relay.md +135 -11
- package/docs/releasing.md +7 -1
- package/docs/scheduling.md +28 -6
- package/docs/secrets.md +53 -0
- package/docs/simulators.md +3 -3
- package/docs/windows.md +3 -3
- package/package.json +1 -1
- package/skills/dibs/SKILL.md +79 -149
- package/src/about.mjs +341 -0
- package/src/acquire.mjs +113 -28
- package/src/after.mjs +127 -0
- package/src/agents.mjs +2 -1
- package/src/alerts.mjs +109 -48
- package/src/answers.mjs +2 -2
- package/src/board.mjs +21 -16
- package/src/broker.mjs +23 -7
- package/src/carry.mjs +287 -0
- package/src/codex.mjs +132 -0
- package/src/commands.mjs +254 -86
- package/src/config.mjs +85 -14
- package/src/connect.mjs +4 -2
- package/src/cool.mjs +28 -12
- package/src/council.mjs +482 -0
- package/src/cwd.mjs +9 -0
- package/src/decisions.mjs +250 -0
- package/src/declare.mjs +96 -0
- package/src/diskreport.mjs +306 -0
- package/src/diskuse.mjs +420 -0
- package/src/diskwarn.mjs +68 -13
- package/src/doctor.mjs +149 -0
- package/src/doors.mjs +547 -0
- package/src/durable.mjs +143 -0
- package/src/envs.mjs +372 -0
- package/src/events.mjs +7 -2
- package/src/files.mjs +121 -7
- package/src/guard.mjs +288 -105
- package/src/guardd.mjs +8 -1
- package/src/halt.mjs +63 -0
- package/src/handoffs.mjs +362 -0
- package/src/history.mjs +34 -4
- package/src/hogs.mjs +36 -8
- package/src/jobprogress.mjs +283 -0
- package/src/later.mjs +21 -10
- package/src/lock.mjs +51 -11
- package/src/measure.mjs +60 -14
- package/src/media.mjs +240 -0
- package/src/news.mjs +31 -0
- package/src/notes.mjs +49 -8
- package/src/notices.mjs +126 -43
- package/src/ondemand.mjs +25 -2
- package/src/outcome.mjs +163 -0
- package/src/overlap.mjs +2 -2
- package/src/override.mjs +149 -79
- package/src/owner.mjs +15 -2
- package/src/people.mjs +236 -0
- package/src/pickup.mjs +72 -0
- package/src/picture.mjs +321 -0
- package/src/plain.mjs +84 -18
- package/src/platform.mjs +29 -7
- package/src/preempt.mjs +123 -15
- package/src/primer.mjs +91 -20
- package/src/prints.mjs +99 -0
- package/src/proc.mjs +11 -1
- package/src/progress.mjs +49 -3
- package/src/proof.mjs +102 -0
- package/src/push.mjs +271 -0
- package/src/questions.mjs +328 -57
- package/src/quota.mjs +1 -1
- package/src/recap.mjs +205 -0
- package/src/relay.mjs +262 -26
- package/src/remote.mjs +23 -10
- package/src/reviewgate.mjs +268 -0
- package/src/reviews.mjs +377 -0
- package/src/route.mjs +263 -0
- package/src/rules.mjs +216 -0
- package/src/schedule.mjs +238 -44
- package/src/schema.mjs +62 -0
- package/src/scratch.mjs +2 -2
- package/src/serve.mjs +3 -2
- package/src/service.mjs +5 -1
- package/src/signed.mjs +97 -0
- package/src/simpool.mjs +46 -1
- package/src/spend.mjs +375 -0
- package/src/state.mjs +43 -17
- package/src/talk.mjs +1082 -134
- package/src/thin.sh +2 -0
- package/src/trail.mjs +290 -0
- package/src/trailnote.mjs +209 -0
- package/src/util.mjs +7 -0
- package/src/vault.mjs +435 -0
- package/src/vendors.mjs +327 -0
- package/src/version.mjs +1 -1
- package/src/web.mjs +354 -60
- package/src/work.mjs +487 -0
- package/src/worker.mjs +7 -1
package/src/doctor.mjs
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// dibs doctor: every file dibs keeps its state in parses, and the state makes sense. What it
|
|
2
|
+
// can put right it does, under each file's lock, and the run goes on record (logChange) naming
|
|
3
|
+
// the agent or person who ran it. --check looks and changes nothing (exit 1 if anything needs it).
|
|
4
|
+
//
|
|
5
|
+
// A broken file is usually put right on its own: the next dibs call that takes its lock restores
|
|
6
|
+
// its last good copy (src/durable.mjs heal). This is for looking on purpose, after a crash or a
|
|
7
|
+
// disk that filled, and for the files nothing takes a lock for.
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { HOME, paths } from './config.mjs';
|
|
11
|
+
import { withState, peek, pidsOf } from './state.mjs';
|
|
12
|
+
import { underLock, heldBy, sweepLock } from './lock.mjs';
|
|
13
|
+
import { heal, parsed, lastGood, jsonLines } from './durable.mjs';
|
|
14
|
+
import { makeAlive } from './proc.mjs';
|
|
15
|
+
import { logChange } from './owner.mjs';
|
|
16
|
+
import { now, die } from './util.mjs';
|
|
17
|
+
|
|
18
|
+
// Files changed only under a lock, and the lock (relative to the state folder).
|
|
19
|
+
const LOCKED = {
|
|
20
|
+
'state.json': 'lock', 'messages.json': 'messages.lock', 'inbox.json': 'messages.lock', 'listeners.json': 'messages.lock',
|
|
21
|
+
'later/jobs.json': 'later/jobs.lock',
|
|
22
|
+
...Object.fromEntries(['questions', 'decisions', 'agents', 'envs', 'handoffs', 'disposable', 'rules', 'reviews', 'spend', 'vault', 'web', 'relay']
|
|
23
|
+
.map(n => [`${n}.json`, `${n}.lock`])),
|
|
24
|
+
};
|
|
25
|
+
// Files dibs rebuilds from scratch when they're missing: a broken one is moved aside.
|
|
26
|
+
const REBUILT = new Set(['alerts', 'hogs', 'nags', 'seen', 'primed', 'cpu', 'swap', 'machine', 'diskuse', 'listen-reminded', 'carry', 'recap', 'rearm', 'relay-status', 'notes', 'push']
|
|
27
|
+
.map(n => `${n}.json`));
|
|
28
|
+
// How long a lock may wait for its holder before doctor moves on and says so.
|
|
29
|
+
const LOCK_WAIT = 15e3;
|
|
30
|
+
|
|
31
|
+
export function doctor(rest) {
|
|
32
|
+
const check = rest.includes('--check');
|
|
33
|
+
const bad = rest.find(a => a !== '--check');
|
|
34
|
+
if (bad) die(`dibs doctor [--check]: unknown ${bad}`);
|
|
35
|
+
const found = [], fixed = [];
|
|
36
|
+
const say = (list, line) => list.push(line);
|
|
37
|
+
|
|
38
|
+
for (const rel of jsonFiles()) {
|
|
39
|
+
const file = path.join(HOME, rel);
|
|
40
|
+
let raw;
|
|
41
|
+
try { raw = fs.readFileSync(file, 'utf8'); } catch { continue; }
|
|
42
|
+
if (parsed(raw)) continue;
|
|
43
|
+
const good = lastGood(file);
|
|
44
|
+
if (check) { say(found, `${rel} doesn't parse${good ? `; its copy from ${when(good.at)} can be put back` : ''}`); continue; }
|
|
45
|
+
if (LOCKED[rel]) {
|
|
46
|
+
const r = underLock(path.join(HOME, LOCKED[rel]), () => heal(file, { by: 'doctor' }), { wait: LOCK_WAIT });
|
|
47
|
+
if (!r) say(found, `${rel} doesn't parse, and its lock stayed held for ${LOCK_WAIT / 1000}s: run dibs doctor again`);
|
|
48
|
+
else if (r.out?.from) say(fixed, `${rel} didn't parse: put back its copy from ${when(r.out.from)}${r.out.kept ? ` (${r.out.kept})` : ''}; the broken file is kept at ${r.out.saved}`);
|
|
49
|
+
else if (r.out) say(fixed, `${rel} didn't parse and had no earlier copy: it starts empty; the broken file is kept at ${r.out.saved}`);
|
|
50
|
+
} else if (REBUILT.has(rel)) {
|
|
51
|
+
const saved = `${file}.corrupt-${now()}`;
|
|
52
|
+
try { fs.renameSync(file, saved); say(fixed, `${rel} didn't parse: moved aside to ${saved}; dibs rebuilds it`); }
|
|
53
|
+
catch (e) { say(found, `${rel} doesn't parse, and moving it aside failed: ${e.message}`); }
|
|
54
|
+
} else if (rel === 'config.json') {
|
|
55
|
+
say(found, `config.json doesn't parse${good ? `: dibs runs on its copy from ${when(good.at)} until it's fixed` : ''}. Fix ${file}, or remove it to get the defaults back`);
|
|
56
|
+
} else say(found, `${rel} doesn't parse, and dibs can't rebuild it: fix ${file} or move it aside`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// A lock its holder died with is taken back by the next dibs that wants it; here, at once,
|
|
60
|
+
// with whatever that writer left half made beside it.
|
|
61
|
+
for (const rel of new Set(Object.values(LOCKED))) {
|
|
62
|
+
const lock = path.join(HOME, rel), h = heldBy(lock);
|
|
63
|
+
const files = Object.keys(LOCKED).filter(f => LOCKED[f] === rel).map(f => path.join(HOME, f));
|
|
64
|
+
const left = leftovers(lock, files);
|
|
65
|
+
if (check) {
|
|
66
|
+
if (h?.pid && !alive(h.pid)) say(found, `${rel} is held by pid ${h.pid}, which has ended`);
|
|
67
|
+
for (const n of left) say(found, `${n} was left by a writer that died`);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (h?.pid && !alive(h.pid) && underLock(lock, () => {}, { wait: LOCK_WAIT, files })) say(fixed, `${rel} was held by pid ${h.pid}, which had ended: taken back`);
|
|
71
|
+
sweepLock(lock, files);
|
|
72
|
+
for (const n of left) if (!fs.existsSync(path.join(path.dirname(lock), n))) say(fixed, `removed ${n}, left by a writer that died`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
stateCheck(check, found, fixed);
|
|
76
|
+
const cut = cutLines();
|
|
77
|
+
const kept = corruptCopies();
|
|
78
|
+
|
|
79
|
+
if (fixed.length) logChange('doctor', { fixed });
|
|
80
|
+
for (const l of fixed) console.log(`fixed: ${l}`);
|
|
81
|
+
for (const l of found) console.log(`${check ? 'needs fixing' : 'still wrong'}: ${l}`);
|
|
82
|
+
for (const l of cut) console.log(`note: ${l}`);
|
|
83
|
+
if (kept.length) console.log(`note: kept from earlier repairs, remove once you've looked: ${kept.join(', ')}`);
|
|
84
|
+
if (!fixed.length && !found.length) console.log(`every state file in ${HOME} parses, and no lease or waiter is left from a process that ended.`);
|
|
85
|
+
if (found.length) process.exitCode = 1;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Every .json file dibs may keep, relative to the state folder.
|
|
89
|
+
function jsonFiles() {
|
|
90
|
+
const top = (() => { try { return fs.readdirSync(HOME).filter(n => n.endsWith('.json')); } catch { return []; } })();
|
|
91
|
+
return [...new Set([...top, ...Object.keys(LOCKED).filter(f => fs.existsSync(path.join(HOME, f)))])].sort();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Leases and waiters of processes that ended: dropped by the same reap every change runs. Ids
|
|
95
|
+
// handed out must stay above every id in use, or a new job would share one with a running one.
|
|
96
|
+
function stateCheck(check, found, fixed) {
|
|
97
|
+
const s = peek();
|
|
98
|
+
const alive = makeAlive(pidsOf(s));
|
|
99
|
+
const t = now();
|
|
100
|
+
const deadLease = l => l.kind === 'hold' ? !(t < l.expires) || (l.pid && !alive(l.pid, l.pstart)) : !alive(l.pid, l.pstart);
|
|
101
|
+
const deadWaiter = q => q.pid && !alive(q.pid, q.pstart);
|
|
102
|
+
const top = Math.max(0, ...[...s.leases, ...s.queue].map(x => x.id).filter(Number.isFinite));
|
|
103
|
+
if (check) {
|
|
104
|
+
for (const l of s.leases.filter(deadLease)) found.push(`lease #${l.id} (${l.who}, ${[].concat(l.res).join(',')}) belongs to a process that ended`);
|
|
105
|
+
for (const q of s.queue.filter(deadWaiter)) found.push(`waiter #${q.id} (${q.who}, ${[].concat(q.res).join(',')}) belongs to a process that ended`);
|
|
106
|
+
if (s.seq < top) found.push(`the next job number (${s.seq + 1}) is one already in use`);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const r = withState(st => {
|
|
110
|
+
const raised = st.seq < top ? top : null;
|
|
111
|
+
if (raised) st.seq = top;
|
|
112
|
+
return { raised, leases: st.leases.map(l => l.id), queue: st.queue.map(q => q.id) };
|
|
113
|
+
}, LOCK_WAIT);
|
|
114
|
+
if (!r) return found.push(`the state lock stayed held for ${LOCK_WAIT / 1000}s, so the state wasn't checked: run dibs doctor again`);
|
|
115
|
+
for (const l of s.leases) if (!r.leases.includes(l.id)) fixed.push(`dropped lease #${l.id} (${l.who}, ${[].concat(l.res).join(',')}): its process had ended`);
|
|
116
|
+
for (const q of s.queue) if (!r.queue.includes(q.id)) fixed.push(`dropped waiter #${q.id} (${q.who}, ${[].concat(q.res).join(',')}): its process had ended`);
|
|
117
|
+
if (r.raised) fixed.push(`job numbers start after #${r.raised} again, past every one in use`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Lines cut short in the logs: every reader skips them, so this is for knowing, not fixing.
|
|
121
|
+
function cutLines() {
|
|
122
|
+
const out = [];
|
|
123
|
+
let names = [];
|
|
124
|
+
try { names = fs.readdirSync(HOME).filter(n => n.endsWith('.jsonl')); } catch {}
|
|
125
|
+
for (const n of names) {
|
|
126
|
+
let text = '';
|
|
127
|
+
try { text = fs.readFileSync(path.join(HOME, n), 'utf8'); } catch { continue; }
|
|
128
|
+
const lines = text.split('\n').filter(l => l.trim()).length, good = jsonLines(text).length;
|
|
129
|
+
if (good < lines) out.push(`${n} has ${lines - good} line${lines - good === 1 ? '' : 's'} cut short (a crash or a full disk mid-write); every reader skips ${lines - good === 1 ? 'it' : 'them'}`);
|
|
130
|
+
}
|
|
131
|
+
return out;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const corruptCopies = () => { try { return fs.readdirSync(HOME).filter(n => /\.corrupt-\d+$/.test(n)); } catch { return []; } };
|
|
135
|
+
|
|
136
|
+
// A dead writer's temporary files beside a lock: what sweepLock removes, named first to say what went.
|
|
137
|
+
function leftovers(lock, files) {
|
|
138
|
+
const dir = path.dirname(lock), base = path.basename(lock);
|
|
139
|
+
const names = new Set(files.map(f => path.basename(f)));
|
|
140
|
+
let all = [];
|
|
141
|
+
try { all = fs.readdirSync(dir); } catch { return []; }
|
|
142
|
+
return all.filter(n => {
|
|
143
|
+
const m = n.match(/^(.*?)(?:\.bak)?\.(\d+)(?:\.t\d+)?\.tmp$/) || n.match(/^(.*)\.(?:new|gone)\.(\d+)\./);
|
|
144
|
+
return m && (names.has(m[1]) || m[1] === base) && !alive(Number(m[2]));
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const alive = pid => { try { process.kill(pid, 0); return true; } catch (e) { return e.code === 'EPERM'; } };
|
|
149
|
+
const when = at => new Date(at).toLocaleString();
|