@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.
Files changed (107) hide show
  1. package/CHANGELOG.md +111 -1
  2. package/README.md +10 -8
  3. package/bin/dibs.mjs +379 -74
  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 +135 -11
  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/docs/windows.md +3 -3
  19. package/package.json +1 -1
  20. package/skills/dibs/SKILL.md +79 -149
  21. package/src/about.mjs +341 -0
  22. package/src/acquire.mjs +113 -28
  23. package/src/after.mjs +127 -0
  24. package/src/agents.mjs +2 -1
  25. package/src/alerts.mjs +109 -48
  26. package/src/answers.mjs +2 -2
  27. package/src/board.mjs +21 -16
  28. package/src/broker.mjs +23 -7
  29. package/src/carry.mjs +287 -0
  30. package/src/codex.mjs +132 -0
  31. package/src/commands.mjs +254 -86
  32. package/src/config.mjs +85 -14
  33. package/src/connect.mjs +4 -2
  34. package/src/cool.mjs +28 -12
  35. package/src/council.mjs +482 -0
  36. package/src/cwd.mjs +9 -0
  37. package/src/decisions.mjs +250 -0
  38. package/src/declare.mjs +96 -0
  39. package/src/diskreport.mjs +306 -0
  40. package/src/diskuse.mjs +420 -0
  41. package/src/diskwarn.mjs +68 -13
  42. package/src/doctor.mjs +149 -0
  43. package/src/doors.mjs +547 -0
  44. package/src/durable.mjs +143 -0
  45. package/src/envs.mjs +372 -0
  46. package/src/events.mjs +7 -2
  47. package/src/files.mjs +121 -7
  48. package/src/guard.mjs +288 -105
  49. package/src/guardd.mjs +8 -1
  50. package/src/halt.mjs +63 -0
  51. package/src/handoffs.mjs +362 -0
  52. package/src/history.mjs +34 -4
  53. package/src/hogs.mjs +36 -8
  54. package/src/jobprogress.mjs +283 -0
  55. package/src/later.mjs +21 -10
  56. package/src/lock.mjs +51 -11
  57. package/src/measure.mjs +60 -14
  58. package/src/media.mjs +240 -0
  59. package/src/news.mjs +31 -0
  60. package/src/notes.mjs +49 -8
  61. package/src/notices.mjs +126 -43
  62. package/src/ondemand.mjs +25 -2
  63. package/src/outcome.mjs +163 -0
  64. package/src/overlap.mjs +2 -2
  65. package/src/override.mjs +149 -79
  66. package/src/owner.mjs +15 -2
  67. package/src/people.mjs +236 -0
  68. package/src/pickup.mjs +72 -0
  69. package/src/picture.mjs +321 -0
  70. package/src/plain.mjs +84 -18
  71. package/src/platform.mjs +29 -7
  72. package/src/preempt.mjs +123 -15
  73. package/src/primer.mjs +91 -20
  74. package/src/prints.mjs +99 -0
  75. package/src/proc.mjs +11 -1
  76. package/src/progress.mjs +49 -3
  77. package/src/proof.mjs +102 -0
  78. package/src/push.mjs +271 -0
  79. package/src/questions.mjs +328 -57
  80. package/src/quota.mjs +1 -1
  81. package/src/recap.mjs +205 -0
  82. package/src/relay.mjs +262 -26
  83. package/src/remote.mjs +23 -10
  84. package/src/reviewgate.mjs +268 -0
  85. package/src/reviews.mjs +377 -0
  86. package/src/route.mjs +263 -0
  87. package/src/rules.mjs +216 -0
  88. package/src/schedule.mjs +238 -44
  89. package/src/schema.mjs +62 -0
  90. package/src/scratch.mjs +2 -2
  91. package/src/serve.mjs +3 -2
  92. package/src/service.mjs +5 -1
  93. package/src/signed.mjs +97 -0
  94. package/src/simpool.mjs +46 -1
  95. package/src/spend.mjs +375 -0
  96. package/src/state.mjs +43 -17
  97. package/src/talk.mjs +1082 -134
  98. package/src/thin.sh +2 -0
  99. package/src/trail.mjs +290 -0
  100. package/src/trailnote.mjs +209 -0
  101. package/src/util.mjs +7 -0
  102. package/src/vault.mjs +435 -0
  103. package/src/vendors.mjs +327 -0
  104. package/src/version.mjs +1 -1
  105. package/src/web.mjs +354 -60
  106. package/src/work.mjs +487 -0
  107. package/src/worker.mjs +7 -1
package/src/rules.mjs ADDED
@@ -0,0 +1,216 @@
1
+ // Standing rules: the person answers a kind of question once, and dibs answers it the same way
2
+ // every time after. "Always yes to TestFlight uploads", "never deploy to production after 18:00",
3
+ // "no to deleting simulators other than dibs-pool".
4
+ //
5
+ // Matching is plain and can be explained in a line: no model reads the question. A rule covers
6
+ // questions asked with its --about (a kind the asker names, like testflight or deploy), and then
7
+ // only if every --if word is in the question itself, no --unless word is anywhere in it, and the
8
+ // clock is inside its --between. Two rules that both cover a question and disagree answer neither:
9
+ // the question goes on as if there were no rules. A --needs-you question (a login, a code on the
10
+ // person's phone) never gets a rule's answer: no answer does what only the person can.
11
+ //
12
+ // Rules keep their own file and lock, as questions do. dibs question reads the file without the
13
+ // lock (writes are renames) and takes the lock only to count a rule's use.
14
+ import fs from 'node:fs';
15
+ import path from 'node:path';
16
+ import { HOME } from './config.mjs';
17
+ import { actor, logChange } from './owner.mjs';
18
+ import { underLock, commit } from './lock.mjs';
19
+ import { clock, die, dur, now, say, fakeFull } from './util.mjs';
20
+ import { bold, dim, amber, wrap } from './style.mjs';
21
+ import { readJSON } from './durable.mjs';
22
+
23
+ const FILE = path.join(HOME, 'rules.json');
24
+ const LOCK = path.join(HOME, 'rules.lock');
25
+ // Writes take milliseconds: a live holder past this is stuck on something else, and the caller hears so.
26
+ const LOCK_WAIT = 15e3;
27
+ // A kind of question: short enough to type, plain enough to match exactly.
28
+ export const ABOUT = /^[a-z0-9][a-z0-9._-]{0,39}$/;
29
+ // Enough to stop a typo'd loop from making thousands; far more than a person keeps in their head.
30
+ const MOST = 200;
31
+
32
+ function locked(fn) {
33
+ const r = underLock(LOCK, h => {
34
+ const s = load();
35
+ const before = JSON.stringify(s);
36
+ const out = fn(s);
37
+ if (JSON.stringify(s) !== before) commit(h, FILE, JSON.stringify(s, null, 1), { fake: () => fakeFull('state') });
38
+ return out;
39
+ }, { wait: LOCK_WAIT, files: [FILE] });
40
+ if (!r) die(`${LOCK} is held by a dibs that's still running, and stayed held for ${LOCK_WAIT / 1000}s. Nothing was changed: run the same command again.`, 75);
41
+ return r.out;
42
+ }
43
+
44
+ function load() {
45
+ let s = {};
46
+ try { s = readJSON(FILE); } catch (e) {
47
+ if (e.code !== 'ENOENT') say(`${FILE} was unreadable (${e.message}); no rule answers anything until it's fixed or removed.`);
48
+ }
49
+ s.seq ||= 0; s.rules ||= [];
50
+ return s;
51
+ }
52
+
53
+ // Every rule, read without the lock.
54
+ export const readRules = () => load().rules;
55
+
56
+ // "18:00" -> minutes since midnight, or null.
57
+ function readTime(hhmm) {
58
+ const m = String(hhmm).match(/^(\d{1,2}):(\d{2})$/);
59
+ return m && Number(m[1]) <= 23 && Number(m[2]) <= 59 ? Number(m[1]) * 60 + Number(m[2]) : null;
60
+ }
61
+ // Inside start-end on the clock; an end before the start runs past midnight, as dibs hours does.
62
+ function inside(w, t) {
63
+ const d = new Date(t), at = d.getHours() * 60 + d.getMinutes();
64
+ const a = readTime(w.start), b = readTime(w.end);
65
+ return a <= b ? at >= a && at < b : at >= a || at < b;
66
+ }
67
+
68
+ const lower = s => String(s ?? '').toLowerCase();
69
+ const escape = s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
70
+ // A whole word or phrase: "upload" is in "upload build 12", not in "uploads" or "reupload".
71
+ const hasWord = (text, w) => new RegExp(`(^|[^\\p{L}\\p{N}])${escape(lower(w))}($|[^\\p{L}\\p{N}])`, 'u').test(lower(text));
72
+
73
+ // Why this rule doesn't answer this question, or null when it does. q: { about, text, purpose,
74
+ // cantUndo, needsYou, options }.
75
+ export function misses(rule, q, t = now()) {
76
+ if (rule.about !== q.about) return `it's about ${rule.about}, the question is about ${q.about || 'nothing (no --about)'}`;
77
+ // An agent may set a rule, but only the person's own rules answer what can't be undone.
78
+ if (rule.byAgent && q.cantUndo != null) return `${rule.by}, an agent, set it, and an agent's rule answers only what can be undone: this is a one-way door, so it goes to the person`;
79
+ // --if words must be in what's asked: the question and what can't be undone.
80
+ const asked = `${q.text} ${q.cantUndo || ''}`;
81
+ const absent = (rule.if || []).filter(w => !hasWord(asked, w));
82
+ if (absent.length) return `the question doesn't say ${absent.map(w => `"${w}"`).join(' or ')}`;
83
+ // --unless words count anywhere, the asker's reasons too: when in doubt, the rule steps aside.
84
+ const all = lower([q.text, q.purpose, q.cantUndo, q.needsYou, ...(q.options || [])].filter(Boolean).join(' '));
85
+ const present = (rule.unless || []).filter(w => all.includes(lower(w)));
86
+ if (present.length) return `the question mentions ${present.map(w => `"${w}"`).join(' and ')}`;
87
+ if (rule.between && !inside(rule.between, t)) return `it's ${clock(t)}, outside ${rule.between.start}-${rule.between.end}`;
88
+ if (q.options && !q.options.some(o => lower(o) === lower(rule.answer))) return `its answer, ${rule.answer}, isn't one of the options (${q.options.join(', ')})`;
89
+ return null;
90
+ }
91
+
92
+ // What the rules say about a question: { rule, answer } when exactly one answer comes from them,
93
+ // { clash: [rules] } when rules that cover it disagree, else { none, near } with the rules about
94
+ // the same kind and why each stepped aside.
95
+ export function ruleFor(q, list = readRules(), t = now()) {
96
+ if (!q.about) return { none: 'it has no --about, so no rule covers it', near: [] };
97
+ const same = list.filter(r => r.about === q.about);
98
+ const hits = same.filter(r => !misses(r, q, t));
99
+ const near = same.filter(r => misses(r, q, t)).map(r => ({ rule: r, why: misses(r, q, t) }));
100
+ if (q.needsYou != null) return { none: 'it needs the person to do something, and no rule can', near };
101
+ if (!hits.length) return { none: same.length ? `no rule about ${q.about} covers it` : `there are no rules about ${q.about}`, near };
102
+ const answers = new Set(hits.map(r => lower(r.answer)));
103
+ if (answers.size > 1) return { clash: hits, near };
104
+ // The most particular rule explains it best: the one with the most conditions.
105
+ const conds = r => (r.if || []).length + (r.unless || []).length + (r.between ? 1 : 0);
106
+ const rule = [...hits].sort((a, b) => conds(b) - conds(a))[0];
107
+ return { rule, answer: q.options?.find(o => lower(o) === lower(rule.answer)) ?? rule.answer, near };
108
+ }
109
+
110
+ // What a rule says, in a line: "about testflight, if it says upload: yes".
111
+ export function describeRule(r) {
112
+ const when = [
113
+ r.if?.length && `if it says ${r.if.map(w => `"${w}"`).join(' and ')}`,
114
+ r.unless?.length && `unless it mentions ${r.unless.map(w => `"${w}"`).join(' or ')}`,
115
+ r.between && (r.between.end === '00:00' ? `after ${r.between.start}` : r.between.start === '00:00' ? `before ${r.between.end}` : `between ${r.between.start} and ${r.between.end}`),
116
+ ].filter(Boolean).join(', ');
117
+ return `about ${r.about}${when ? `, ${when}` : ''}: ${r.answer}`;
118
+ }
119
+
120
+ // A rule answered question qid: counted on the rule, for the list. Best effort: the answer
121
+ // already stands, and a lost count is no reason to fail the asker.
122
+ export function noteHit(id, qid, t = now()) {
123
+ try { locked(s => { const r = s.rules.find(x => x.id === id); if (r) Object.assign(r, { hits: (r.hits || 0) + 1, lastAt: t, lastQuestion: qid }); }); } catch {}
124
+ }
125
+
126
+ const shown = r => ({ id: r.id, about: r.about, answer: r.answer, if: r.if || [], unless: r.unless || [], between: r.between || null, why: r.why || null,
127
+ by: r.by, byAgent: !!r.byAgent, at: r.at, hits: r.hits || 0, lastAt: r.lastAt || null, lastQuestion: r.lastQuestion ?? null, says: describeRule(r) });
128
+ export const rulesJson = () => ({ rules: readRules().map(shown) });
129
+
130
+ // Which questions a rule covers, whatever it answers: two rules with the same conditions only ever clash.
131
+ const conditions = r => JSON.stringify([r.about, (r.if || []).map(lower).sort(), (r.unless || []).map(lower).sort(), r.between || null]);
132
+ const words = v => v.split(',').map(x => x.trim()).filter(Boolean);
133
+ const USE = 'dibs rules add --about testflight --answer yes [--if upload] [--unless production] [--between 18:00-08:00] -m "why"';
134
+
135
+ function parseAdd(args) {
136
+ const o = { if: [], unless: [] };
137
+ for (let i = 0; i < args.length; i++) {
138
+ const a = args[i], next = () => args[++i] ?? die(`${a} needs a value. ${USE}`);
139
+ if (a === '--about') o.about = lower(next().trim());
140
+ else if (a === '--answer') o.answer = next().trim();
141
+ else if (a === '--if') o.if.push(...words(next()));
142
+ else if (a === '--unless') o.unless.push(...words(next()));
143
+ else if (a === '--between') o.between = next();
144
+ else if (a === '--after') o.between = `${next()}-00:00`;
145
+ else if (a === '--before') o.between = `00:00-${next()}`;
146
+ else if (a === '-m' || a === '--msg') o.why = next().trim();
147
+ else die(`unknown ${a.startsWith('-') ? 'flag' : 'word'} ${a}. ${USE}`);
148
+ }
149
+ if (!o.about || !ABOUT.test(o.about)) die(`a rule needs --about: the kind of question it answers, in lowercase letters, digits and dashes (testflight, deploy, simulators). Nothing was added. ${USE}`);
150
+ if (!o.answer) die(`a rule needs --answer: what it says every time (yes, no, or an option). Nothing was added. ${USE}`);
151
+ if (o.between != null) {
152
+ const [start, end, extra] = String(o.between).split('-');
153
+ if (extra != null || readTime(start) == null || readTime(end) == null || readTime(start) === readTime(end))
154
+ die(`can't read --between ${o.between}. Use 18:00-08:00 (an end before the start runs past midnight), --after 18:00 or --before 09:00. Nothing was added.`);
155
+ o.between = { start: start.padStart(5, '0'), end: end.padStart(5, '0') };
156
+ }
157
+ return o;
158
+ }
159
+
160
+ // dibs rules [--json] | add ... | test "question" --about x [...] | remove <id>
161
+ export async function rulesCmd(args) {
162
+ const [verb, ...rest] = args;
163
+ if (verb === 'add') return add(rest);
164
+ if (verb === 'remove' || verb === 'rm') return remove(rest);
165
+ if (verb === 'test') return (await import('./questions.mjs')).testRule(rest);
166
+ if (verb && verb !== '--json') die(`dibs rules takes add, test, remove or --json, not ${verb}. ${USE}`);
167
+ if (rest.length) die(`unknown ${rest[0]}. Try: dibs rules [--json]`);
168
+ if (verb === '--json') return console.log(JSON.stringify(rulesJson(), null, 1));
169
+ const list = readRules(), t = now();
170
+ if (!list.length) return console.log(`No standing rules: every question goes the usual way. Add one: ${USE}`);
171
+ const out = [bold(`${list.length} standing rule${list.length > 1 ? 's' : ''}. A question asked with the same --about gets its answer at once.`)];
172
+ for (const r of list) {
173
+ out.push('', ` ${r.id} ${describeRule(r)}`);
174
+ if (r.why) out.push(dim(wrap(` ${r.why}`, ' ')));
175
+ const used = r.hits ? `answered ${r.hits} question${r.hits > 1 ? 's' : ''}, last #${r.lastQuestion} ${dur(t - r.lastAt)} ago` : 'not used yet';
176
+ out.push(dim(` set by ${r.by} ${dur(t - r.at)} ago; ${used}`));
177
+ if (r.byAgent) out.push(amber(' set by an agent, not the person'));
178
+ }
179
+ out.push('', `Remove one: dibs rules remove ${list[0].id} Try one: dibs rules test "Upload build 12 to TestFlight?" --about ${list[0].about}`);
180
+ console.log(out.join('\n'));
181
+ }
182
+
183
+ function add(args) {
184
+ const o = parseAdd(args);
185
+ const who = actor();
186
+ const r = locked(s => {
187
+ if (s.rules.length >= MOST) return { full: true };
188
+ const same = s.rules.find(x => conditions(x) === conditions(o));
189
+ if (same && lower(same.answer) === lower(o.answer)) return { rule: { ...same }, again: true };
190
+ const rule = { id: `r${++s.seq}`, about: o.about, answer: o.answer, ...(o.if.length && { if: o.if }), ...(o.unless.length && { unless: o.unless }),
191
+ ...(o.between && { between: o.between }), ...(o.why && { why: o.why }), by: who.name, ...(who.agent && { byAgent: true }), ...(who.via && { via: who.via }), at: now(), hits: 0 };
192
+ s.rules.push(rule);
193
+ // The same conditions with another answer: the new one replaces it.
194
+ const replaced = same ? s.rules.splice(s.rules.indexOf(same), 1)[0] : null;
195
+ return { rule: { ...rule }, replaced };
196
+ });
197
+ if (r.full) die(`there are already ${MOST} rules. Remove some first: dibs rules`, 1);
198
+ if (r.again) return console.log(`${r.rule.id} already says that: ${describeRule(r.rule)}. Nothing changed.`);
199
+ logChange('rule', { rule: r.rule.id, says: describeRule(r.rule), ...(r.replaced && { replaced: r.replaced.id }) });
200
+ console.log(`${r.rule.id}: ${describeRule(r.rule)}.${r.replaced ? ` It replaces ${r.replaced.id}, which answered ${r.replaced.answer} to the same questions.` : ''}`);
201
+ // An agent's rule answers two-way doors only (src/questions.mjs ruleFor): a one-way door of its kind still goes to the person.
202
+ say(`questions asked with --about ${r.rule.about} that it covers get ${r.rule.answer} at once, ${r.rule.byAgent ? "two-way doors only: one-way doors still go to the person, since an agent set it" : 'one-way doors too'}, and the person sees which rule answered.`
203
+ + ` Check one: dibs rules test "..." --about ${r.rule.about}. Remove it: dibs rules remove ${r.rule.id}`);
204
+ }
205
+
206
+ function remove(args) {
207
+ const id = args[0];
208
+ if (!id || args.length > 1) die('which rule? dibs rules remove <id> (the ids: dibs rules)');
209
+ const r = locked(s => {
210
+ const i = s.rules.findIndex(x => x.id === id);
211
+ return i < 0 ? null : s.rules.splice(i, 1)[0];
212
+ });
213
+ if (!r) die(`no rule ${id}. See them: dibs rules`, 1);
214
+ logChange('rule removed', { rule: r.id, says: describeRule(r) });
215
+ console.log(`removed ${r.id} (${describeRule(r)}). Questions about ${r.about} go the usual way again.`);
216
+ }