@dibssoftware/dibs 0.0.181 → 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.
Files changed (105) hide show
  1. package/CHANGELOG.md +103 -1
  2. package/README.md +10 -8
  3. package/bin/dibs.mjs +370 -73
  4. package/docs/agents.md +303 -0
  5. package/docs/council.md +88 -0
  6. package/docs/files.md +3 -3
  7. package/docs/guard.md +86 -11
  8. package/docs/json.md +211 -0
  9. package/docs/later.md +5 -5
  10. package/docs/machines.md +6 -6
  11. package/docs/questions.md +104 -3
  12. package/docs/reference.md +124 -30
  13. package/docs/relay.md +120 -9
  14. package/docs/releasing.md +7 -1
  15. package/docs/scheduling.md +28 -6
  16. package/docs/secrets.md +53 -0
  17. package/docs/simulators.md +3 -3
  18. package/package.json +1 -1
  19. package/skills/dibs/SKILL.md +79 -149
  20. package/src/about.mjs +341 -0
  21. package/src/acquire.mjs +111 -27
  22. package/src/after.mjs +127 -0
  23. package/src/agents.mjs +2 -1
  24. package/src/alerts.mjs +95 -42
  25. package/src/answers.mjs +2 -2
  26. package/src/board.mjs +21 -16
  27. package/src/broker.mjs +23 -7
  28. package/src/carry.mjs +287 -0
  29. package/src/codex.mjs +132 -0
  30. package/src/commands.mjs +243 -82
  31. package/src/config.mjs +85 -14
  32. package/src/connect.mjs +4 -2
  33. package/src/cool.mjs +27 -11
  34. package/src/council.mjs +482 -0
  35. package/src/cwd.mjs +9 -0
  36. package/src/decisions.mjs +250 -0
  37. package/src/declare.mjs +96 -0
  38. package/src/diskreport.mjs +306 -0
  39. package/src/diskuse.mjs +420 -0
  40. package/src/diskwarn.mjs +68 -13
  41. package/src/doctor.mjs +149 -0
  42. package/src/doors.mjs +547 -0
  43. package/src/durable.mjs +143 -0
  44. package/src/envs.mjs +372 -0
  45. package/src/events.mjs +7 -2
  46. package/src/files.mjs +118 -6
  47. package/src/guard.mjs +239 -86
  48. package/src/guardd.mjs +8 -1
  49. package/src/halt.mjs +63 -0
  50. package/src/handoffs.mjs +362 -0
  51. package/src/history.mjs +34 -4
  52. package/src/hogs.mjs +36 -8
  53. package/src/jobprogress.mjs +283 -0
  54. package/src/later.mjs +21 -10
  55. package/src/lock.mjs +33 -9
  56. package/src/measure.mjs +42 -11
  57. package/src/media.mjs +240 -0
  58. package/src/news.mjs +31 -0
  59. package/src/notes.mjs +49 -8
  60. package/src/notices.mjs +126 -43
  61. package/src/ondemand.mjs +25 -2
  62. package/src/outcome.mjs +163 -0
  63. package/src/overlap.mjs +2 -2
  64. package/src/override.mjs +149 -79
  65. package/src/owner.mjs +15 -2
  66. package/src/people.mjs +236 -0
  67. package/src/pickup.mjs +72 -0
  68. package/src/picture.mjs +321 -0
  69. package/src/plain.mjs +84 -18
  70. package/src/platform.mjs +19 -0
  71. package/src/preempt.mjs +123 -15
  72. package/src/primer.mjs +91 -20
  73. package/src/prints.mjs +99 -0
  74. package/src/proc.mjs +1 -1
  75. package/src/progress.mjs +49 -3
  76. package/src/proof.mjs +102 -0
  77. package/src/push.mjs +271 -0
  78. package/src/questions.mjs +328 -57
  79. package/src/quota.mjs +1 -1
  80. package/src/recap.mjs +205 -0
  81. package/src/relay.mjs +139 -20
  82. package/src/remote.mjs +23 -10
  83. package/src/reviewgate.mjs +268 -0
  84. package/src/reviews.mjs +377 -0
  85. package/src/route.mjs +263 -0
  86. package/src/rules.mjs +216 -0
  87. package/src/schedule.mjs +238 -44
  88. package/src/schema.mjs +62 -0
  89. package/src/scratch.mjs +2 -2
  90. package/src/serve.mjs +3 -2
  91. package/src/signed.mjs +97 -0
  92. package/src/simpool.mjs +46 -1
  93. package/src/spend.mjs +375 -0
  94. package/src/state.mjs +43 -17
  95. package/src/talk.mjs +1078 -133
  96. package/src/thin.sh +2 -0
  97. package/src/trail.mjs +290 -0
  98. package/src/trailnote.mjs +209 -0
  99. package/src/util.mjs +7 -0
  100. package/src/vault.mjs +435 -0
  101. package/src/vendors.mjs +327 -0
  102. package/src/version.mjs +1 -1
  103. package/src/web.mjs +353 -60
  104. package/src/work.mjs +487 -0
  105. package/src/worker.mjs +7 -1
package/src/push.mjs ADDED
@@ -0,0 +1,271 @@
1
+ // Notifications on the person's phone (the dibs iPhone app) when something reaches them: a
2
+ // question only they can answer (--one-way or --needs-you), a note or reply an agent leaves them,
3
+ // a handoff no agent took, work an agent left unfinished when its session ended, a hold on new jobs
4
+ // an agent put on, a task an agent finished or says it's stuck on, spend past 80% of a cap
5
+ // (src/spend.mjs), and the few things dibs saw an agent ship (src/trail.mjs): a merge into main, a
6
+ // version tag or release, a build uploaded to App Store Connect, a package published. The relay
7
+ // connector (src/relay.mjs) runs this once a second and sends what it finds to
8
+ // the relay, which hands it to Apple.
9
+ //
10
+ // Neither the relay nor Apple reads a word of it. Each phone that asked for notifications gave this
11
+ // Mac, over its end-to-end encrypted session, its APNs token and a 32-byte push key it made (kept
12
+ // on the device record, src/relay.mjs registerPush). What a notification says is sealed under that
13
+ // key (sealPush); Apple carries the box beside a fixed "Something needs you", and the phone's
14
+ // notification extension opens the box and shows the real words in its place.
15
+ //
16
+ // Ten notes in a row buzz once: what's new is gathered until nothing new has come for QUIET, or
17
+ // for MAX_HOLD at most, and a phone hears from this Mac at most once per GAP. Outside the person's
18
+ // hours (dibs hours) only urgent questions go out; the rest wait for the morning, as questions do,
19
+ // and go then if they still need the person.
20
+ //
21
+ // The attention budget (dibs attention, config.json "attention") caps how many notifications go out
22
+ // an hour. Past it only urgent questions go; the rest stay gathered for the batch that goes once the
23
+ // hour has room again, and what the person deals with on the Mac meanwhile never buzzes at all (the
24
+ // recap has it).
25
+ import fs from 'node:fs';
26
+ import path from 'node:path';
27
+ import crypto from 'node:crypto';
28
+ import { HOME, paths, loadConfig } from './config.mjs';
29
+ import { questionsJson, offHours, attention } from './questions.mjs';
30
+ import { readRules, describeRule } from './rules.mjs';
31
+ import { threadsJson } from './talk.mjs';
32
+ import { handoffsNow } from './handoffs.mjs';
33
+ import { sweepSoon } from './carry.mjs';
34
+ import { read as readPosts, tasks, FILE as PROGRESS } from './progress.mjs';
35
+ import { trailNow } from './trail.mjs';
36
+ import { TRAIL } from './trailnote.mjs';
37
+ import { peek } from './state.mjs';
38
+ import { halted, haltUntil } from './schedule.mjs';
39
+ import { readPeople, several, hoursCfg, byDevice } from './people.mjs';
40
+ import { capsNear, money } from './spend.mjs';
41
+ import { now } from './util.mjs';
42
+
43
+ // What's been sent (or was there before notifications were on): keys of things that still exist.
44
+ const FILE = path.join(HOME, 'push.json');
45
+ // Nothing new for this long: send what's gathered.
46
+ const QUIET = 5e3;
47
+ // Something has waited this long: send it even if more keeps coming.
48
+ const MAX_HOLD = 30e3;
49
+ // A phone hears from this Mac at most this often; what comes meanwhile goes in the next one.
50
+ const GAP = 30e3;
51
+ // The attention budget counts notifications over this window.
52
+ const HOUR = 36e5;
53
+ // An open handoff no agent has taken in this long is the person's to sort out.
54
+ const HANDOFF_WAIT = 10 * 60e3;
55
+ // A task closed or marked stuck longer ago than this is old news by the time the phone could hear
56
+ // of it (notifications were off, or the connector was down): the recap has it instead.
57
+ const TASK_NEWS = 2 * 36e5;
58
+ // Lines a notification about several things lists; the rest is "and 3 more".
59
+ const LINES = 4;
60
+ const LINE_MAX = 90;
61
+ const TITLE_MAX = 80;
62
+ const BODY_MAX = 600;
63
+ // The sealed JSON is padded to one of these sizes, so its length says little about the words.
64
+ const PADS = [512, 1024, 2048];
65
+ const AAD = 'dibs-push-v1';
66
+
67
+ const b64u = b => Buffer.from(b).toString('base64url');
68
+ const cut = (s, n) => { const x = String(s ?? '').replace(/\s+/g, ' ').trim(); return x.length > n ? `${x.slice(0, n - 3)}...` : x; };
69
+
70
+ // AES-256-GCM under the phone's push key, the machine and device ids as associated data, so a box
71
+ // opens only on the phone it was sealed for, as from this Mac. iv (12) || ciphertext || tag (16).
72
+ export function sealPush(key, mid, did, obj) {
73
+ let j = JSON.stringify(obj);
74
+ const size = PADS.find(p => Buffer.byteLength(j) <= p);
75
+ if (!size) return null;
76
+ j += ' '.repeat(size - Buffer.byteLength(j));
77
+ const iv = crypto.randomBytes(12);
78
+ const c = crypto.createCipheriv('aes-256-gcm', key, iv);
79
+ c.setAAD(Buffer.from(`${AAD} ${mid} ${did}`));
80
+ return b64u(Buffer.concat([iv, c.update(j), c.final(), c.getAuthTag()]));
81
+ }
82
+ export function openPush(key, mid, did, box) {
83
+ const b = Buffer.from(box, 'base64url');
84
+ const d = crypto.createDecipheriv('aes-256-gcm', key, b.subarray(0, 12));
85
+ d.setAAD(Buffer.from(`${AAD} ${mid} ${did}`));
86
+ d.setAuthTag(b.subarray(b.length - 16));
87
+ return JSON.parse(Buffer.concat([d.update(b.subarray(12, b.length - 16)), d.final()]).toString('utf8'));
88
+ }
89
+
90
+ // Everything that could reach the person now, each with a key that stays the same while it exists.
91
+ // held: outside their hours, so it waits. Read without any lock, as the page reads them.
92
+ // With two or more people, each item names the person it's for (for), the owner of the agent it's
93
+ // about, and waits for that person's own hours; one with no for is everyone's.
94
+ export function gather(cfg = loadConfig(), t = now(), p = readPeople()) {
95
+ const out = [];
96
+ const many = several(p);
97
+ const off = offHours(cfg, t);
98
+ const offFor = who => (many && who ? offHours(hoursCfg(cfg, who, p), t) : off);
99
+ const mine = who => (many && who ? { for: who } : {});
100
+ const th = threadsJson(t);
101
+ const ownerOf = new Map(th.threads.filter(x => x.owner).map(x => [x.to, x.owner]));
102
+ const threadOf = new Map();
103
+ for (const x of th.threads) for (const m of x.messages) if (m.questionId != null) threadOf.set(m.questionId, x.to);
104
+ for (const q of questionsJson(cfg, t).questions) {
105
+ if (q.state !== 'open' || !q.oneWay) continue;
106
+ const who = threadOf.get(q.id) || q.agent || q.who;
107
+ out.push({ key: `q${q.id}`, kind: q.needsYou ? 'needs' : 'question', who, text: q.text, q: String(q.id), thread: who, options: q.options || null, held: q.held,
108
+ ...(q.prio === 'urgent' && { urgent: true }), ...mine(q.for) });
109
+ }
110
+ for (const x of th.threads) {
111
+ if (!x.unread) continue;
112
+ const unread = x.messages.filter(m => m.from === 'agent' && m.kind !== 'delivered').slice(-x.unread);
113
+ // A poll buzzes as a note does; a note marked needs-you (dibs say --kind needs-you) as a question that needs the person.
114
+ for (const m of unread) if (m.kind === 'note' || m.kind === 'poll') out.push({ key: m.id, kind: m.badge === 'needs-you' ? 'needs' : 'note', who: x.desk ? 'dibs' : x.to,
115
+ text: m.kind === 'poll' ? `Poll: ${m.text}` : m.summary ?? m.text, thread: x.to, held: offFor(x.owner), ...mine(x.owner) });
116
+ }
117
+ for (const x of handoffsNow(t)) {
118
+ // Work a session left unfinished (src/carry.mjs): the person hears at once when no listening
119
+ // agent was offered it, else once that one gives it back or lets it sit. Not once it's given to someone.
120
+ if (x.carry) {
121
+ const c = x.carry;
122
+ if (x.state === 'open' && !x.to && (!c.offered || c.returned || t - c.offeredAt >= HANDOFF_WAIT))
123
+ out.push({ key: `carry:${x.id}:${c.stoppedAt}`, kind: 'carry', who: c.who, text: `${x.title}. Pick it up?`, thread: c.who, held: off });
124
+ continue;
125
+ }
126
+ // A work item (dibs work add) waits to be claimed by design; only a handoff nobody took is news.
127
+ // Keyed by its old h id, so a handoff pushed before ids became w isn't pushed again.
128
+ if (x.state === 'open' && !x.kind && x.from !== 'the person' && t - x.at >= HANDOFF_WAIT) out.push({ key: x.id.replace(/^w/, 'h'), kind: 'handoff', who: x.from, text: x.title, held: off });
129
+ }
130
+ // What agents finished, and what they say they're stuck on: the person shouldn't have to look.
131
+ for (const k of tasks(readPosts({ since: t - TASK_NEWS - 864e5 }), t)) {
132
+ const at = k.state === 'done' ? k.doneAt : k.state === 'blocked' ? k.last : 0;
133
+ if (!at || t - at > TASK_NEWS || k.closedBy) continue;
134
+ const kind = k.state === 'done' ? 'done' : 'stuck';
135
+ const owner = k.owner || ownerOf.get(k.who);
136
+ out.push({ key: `${kind}:${k.who}:${k.agentId || ''}:${k.task || k.project || ''}:${at}`, kind, who: k.who, text: k.task ? `${k.task}: ${k.text}` : k.text, thread: k.who, held: offFor(owner), ...mine(owner) });
137
+ }
138
+ // What dibs saw an agent ship. Commits, pushes and test runs stay on the page and in the recap.
139
+ for (const x of trailNow({ since: t - TASK_NEWS, t, events: [] })) {
140
+ if (x.ships) out.push({ key: `trail:${x.id}`, kind: 'shipped', who: x.who, text: `${x.text}${x.repo ? ` in ${x.repo}` : ''}`, thread: x.who, auto: true, held: off });
141
+ }
142
+ // A standing rule an agent set answers questions in the person's place: they hear of it.
143
+ for (const r of readRules()) if (r.byAgent) out.push({ key: `rule:${r.id}`, kind: 'rule', who: r.by, text: describeRule(r), held: off });
144
+ // Spend nearing a cap: once at NEAR and once past it, each period.
145
+ for (const c of capsNear(t)) {
146
+ const over = c.used > c.limit;
147
+ out.push({ key: `spend:${c.per}:${c.on || ''}:${c.project || ''}:${c.since}:${over ? 'over' : 'near'}`, kind: 'spend', who: c.name,
148
+ text: `${money(c.used)} of the ${money(c.limit)} cap ${c.per === 'day' ? 'today' : 'this month'} (${Math.round(c.pct * 100)}%), spent or held.`, held: off, over });
149
+ }
150
+ const h = halted(peek(), t);
151
+ // Only an agent's hold: the person knows about their own.
152
+ if (h?.by && !h.byPerson && !h.by.startsWith('the person')) out.push({ key: `halt${h.at}`, kind: 'halt', who: h.by, text: h.reason || `No new jobs start ${haltUntil(h)}.`, held: off });
153
+ return out;
154
+ }
155
+
156
+ const TITLES = { question: w => `${w} asks`, needs: w => `${w} needs you`, note: w => w, handoff: w => `Nobody took ${w}'s handoff`, carry: w => `${w} stopped mid-task`, done: w => `${w} finished`, stuck: w => `${w} is stuck`, shipped: w => `${w} shipped`, halt: w => `${w} put new jobs on hold`, rule: w => `${w} set a standing rule`,
157
+ spend: (w, x) => (x.over ? `Spend on ${w} passed its cap` : `Spend on ${w} is near its cap`) };
158
+ const plural = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
159
+
160
+ // What one notification says about these things, and where tapping it goes.
161
+ export function describe(items) {
162
+ if (items.length === 1) {
163
+ const [x] = items;
164
+ return { v: 1, kind: x.kind, title: cut(TITLES[x.kind](x.who, x), TITLE_MAX), body: cut(x.text, BODY_MAX), ...(x.auto && { auto: true }),
165
+ ...(x.q && { q: x.q }), ...(x.thread && { thread: x.thread }), ...(x.options && x.kind === 'question' && { options: x.options.slice(0, 4).map(o => cut(o, 40)) }) };
166
+ }
167
+ const count = k => items.filter(x => x.kind === k).length;
168
+ const qs = items.filter(x => x.q).length, notes = count('note'), done = count('done'), stuck = count('stuck'), shipped = count('shipped');
169
+ const rest = items.length - qs - notes - done - stuck - shipped;
170
+ const title = [stuck && `${stuck} stuck`, qs && plural(qs, 'question'), notes && plural(notes, 'note'), done && `${done} finished`, shipped && `${shipped} shipped`, rest && plural(rest, 'other thing')].filter(Boolean).join(', ');
171
+ const lines = items.slice(0, LINES).map(x => cut(`${x.who}: ${x.text}`, LINE_MAX));
172
+ if (items.length > LINES) lines.push(`and ${items.length - LINES} more`);
173
+ const threads = [...new Set(items.map(x => x.thread).filter(Boolean))];
174
+ return { v: 1, kind: 'many', title, body: lines.join('\n'), n: items.length,
175
+ ...(threads.length === 1 && items.every(x => x.thread) && { thread: threads[0] }) };
176
+ }
177
+
178
+ // What one phone hears of items: its person's (dibs people set <name> --device), and what's
179
+ // everyone's, as their notify setting allows. A phone that's nobody's hears only what's everyone's
180
+ // once two or more people are named (the owner, 2026-11-26), and everything before that.
181
+ const ASKS = new Set(['question', 'needs']);
182
+ export function forPhone(items, did, p = readPeople()) {
183
+ const who = byDevice(p, did);
184
+ if (!who) return several(p) ? items.filter(x => !x.for) : items;
185
+ const notify = p[who].notify || 'all';
186
+ if (notify === 'off') return [];
187
+ return items.filter(x => (!x.for || x.for === who) && (notify === 'all' || ASKS.has(x.kind)));
188
+ }
189
+
190
+ // push.json: seen, and sent (when each of the last hour's notifications went, for the budget), so a
191
+ // restarted connector neither repeats itself nor starts a fresh hour.
192
+ function readPush() {
193
+ try {
194
+ const j = JSON.parse(fs.readFileSync(FILE, 'utf8'));
195
+ return { seen: Array.isArray(j.seen) ? new Set(j.seen) : null, sent: Array.isArray(j.sent) ? j.sent.filter(Number.isFinite) : [] };
196
+ } catch { return { seen: null, sent: [] }; }
197
+ }
198
+ const readSeen = () => readPush().seen;
199
+ function writeSeen(seen, sent = readPush().sent) {
200
+ const tmp = `${FILE}.${process.pid}`;
201
+ fs.writeFileSync(tmp, JSON.stringify({ seen: [...seen], sent }), { mode: 0o600 });
202
+ fs.renameSync(tmp, FILE);
203
+ }
204
+
205
+ // What a change to any of the sources looks like from outside: their files' times, and the minute
206
+ // (the person's hours start on one, a handoff turns ten minutes old on one, and a merge noted on the
207
+ // trail before it ran shows in the reflog by the next).
208
+ const SOURCES = ['questions.json', 'messages.json', 'handoffs.json', 'rules.json', 'spend.json'].map(f => path.join(HOME, f)).concat(paths.state, paths.config, PROGRESS, TRAIL);
209
+ const mtime = f => { try { return fs.statSync(f).mtimeMs; } catch { return 0; } };
210
+
211
+ // The connector's notifier. phones(): paired devices with a push registration, [{ did, rth, tok,
212
+ // key }]. send(text): to the relay, false when it isn't connected. forget(rth, tok): the relay says
213
+ // that token is dead.
214
+ export function pusher({ mid, phones, send, forget, clock = now, cfg = loadConfig }) {
215
+ let pending = new Map(), firstAt = 0, grewAt = 0, lastSent = 0, sig = '';
216
+ function refresh(t) {
217
+ const all = gather(cfg(), t);
218
+ let seen = readSeen();
219
+ // The first look, ever: what's already there was there before notifications.
220
+ if (!seen) { writeSeen(new Set(all.map(x => x.key))); pending = new Map(); return; }
221
+ // Keys of things gone (read, answered, taken) are let go of; they never come back.
222
+ const keys = new Set(all.map(x => x.key));
223
+ const kept = new Set([...seen].filter(k => keys.has(k)));
224
+ if (kept.size !== seen.size) writeSeen(seen = kept);
225
+ const next = new Map(all.filter(x => !x.held && !seen.has(x.key)).map(x => [x.key, x]));
226
+ if ([...next.keys()].some(k => !pending.has(k))) { grewAt = t; if (!pending.size) firstAt = t; }
227
+ pending = next;
228
+ }
229
+ function flush(t) {
230
+ const sent = readPush().sent.filter(x => t - x < HOUR);
231
+ const cap = attention(cfg());
232
+ // Over budget: only urgent questions go now; the rest stay pending for when the hour has room.
233
+ const items = [...pending.values()].filter(x => cap == null || sent.length < cap || x.urgent);
234
+ if (!items.length) return false;
235
+ const list = phones();
236
+ // Each phone hears what's for its person, or everything when it's nobody's in particular.
237
+ const n = list.map(p => {
238
+ const mine = forPhone(items, p.did);
239
+ return mine.length ? { h: p.rth, tok: p.tok, d: sealPush(Buffer.from(p.key, 'base64url'), mid, p.did, describe(mine)) } : null;
240
+ }).filter(x => x?.d);
241
+ if (n.length) {
242
+ if (!send(JSON.stringify({ t: 'push', id: b64u(crypto.randomBytes(9)), n }))) return false;
243
+ lastSent = t;
244
+ sent.push(t);
245
+ }
246
+ // Sent, or nobody to send it to: either way it's been dealt with, and won't go out later.
247
+ const seen = readSeen() || new Set();
248
+ for (const x of items) { seen.add(x.key); pending.delete(x.key); }
249
+ writeSeen(seen, sent);
250
+ return true;
251
+ }
252
+ return {
253
+ tick() {
254
+ // Sessions that ended mid-task become handoffs on some process's look; the connector is one that's always up.
255
+ sweepSoon();
256
+ const t = clock();
257
+ const s = SOURCES.map(mtime).join(',') + `,${Math.floor(t / 60e3)}`;
258
+ if (s !== sig) { sig = s; refresh(t); }
259
+ if (!pending.size) return false;
260
+ if (t - grewAt < QUIET && t - firstAt < MAX_HOLD) return false;
261
+ if (t - lastSent < GAP) return false;
262
+ return flush(t);
263
+ },
264
+ // The relay's answer: a token Apple called dead is forgotten here too.
265
+ answer(m) {
266
+ const sent = new Map(phones().map(p => [p.rth, p.tok]));
267
+ for (const r of Array.isArray(m?.r) ? m.r : []) if (r?.gone === true && sent.has(r.h)) forget(r.h, sent.get(r.h));
268
+ },
269
+ get pending() { return [...pending.values()]; },
270
+ };
271
+ }