@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/state.mjs CHANGED
@@ -21,7 +21,9 @@ import { stopTree, starts, liveGroups, wokeAt, OS } from './platform.mjs';
21
21
  import { settle, flush } from './preempt.mjs';
22
22
  import { reapPool } from './simpool.mjs';
23
23
  import { queueResets, startResets, reapResets } from './reset.mjs';
24
+ import { noteEnds, recordEnds } from './envs.mjs';
24
25
  import { logChanges, event } from './events.mjs';
26
+ import { parsed, lastGood, heal, jsonLines } from './durable.mjs';
25
27
  import { die, now, say, dur, noSpace, fakeFull, unpolled, GHOST_AFTER, POLLED_EVERY, UNPOLLED } from './util.mjs';
26
28
 
27
29
  export const KEEP_PLACE = 120e3; // a waiter that gave up keeps its place this long
@@ -81,7 +83,9 @@ function change(fn, lock) {
81
83
  reap(s);
82
84
  const out = fn(s);
83
85
  settle(s, now(), history); // resume work paused for a lease that has ended, and never leave every job paused
84
- queueResets(s, before.filter(l => !s.leases.some(x => x.id === l.id))); // onRelease for every lease this change ended
86
+ const gone = before.filter(l => !s.leases.some(x => x.id === l.id));
87
+ queueResets(s, gone); // onRelease for every lease this change ended
88
+ noteEnds(gone); // and what each environment it ended a lease on now has (src/envs.mjs)
85
89
  // Nothing changed, nothing written: most waiters' polls change nothing, and
86
90
  // an unchanged file tells a waiter it can poll less often (src/acquire.mjs nextPoll).
87
91
  const text = JSON.stringify(s, null, 1);
@@ -101,11 +105,12 @@ function change(fn, lock) {
101
105
  lock.release();
102
106
  if (moved && kicks) try { kick(); } catch {} // after the lock, which the broker takes next
103
107
  startResets(written);
108
+ recordEnds(written);
104
109
  }
105
110
  }
106
111
 
107
112
  // Every pid the state names, whose start times reap() checks.
108
- export const pidsOf = s => [...(s.leases || []).map(l => l.pid), ...(s.queue || []).map(q => q.pid || q.keptFor), ...(s.resets || []).flatMap(x => [x.pid, x.cpid])];
113
+ export const pidsOf = s => [...(s.leases || []).map(l => l.pid), ...(s.queue || []).map(q => q.pid || q.keptFor), ...(s.resets || []).flatMap(x => [x.pid, x.cpid]), ...(s.afters || []).map(x => x.pid)];
109
114
 
110
115
  // Read those start times before taking the lock, so reap() under it finds them in
111
116
  // makeAlive's cache instead of running ps: on 2026-11-11 that ps (100 to 700 ms on a
@@ -140,18 +145,26 @@ export function withStateIfRoom(fn) {
140
145
  // The state as last written, without the lock and without reaping: what dibs
141
146
  // web shows. Writes are a rename, so this never sees half a file; a holder that
142
147
  // died shows until the next dibs call reaps it.
143
- export function peek() { return read().s; }
148
+ export function peek() { return read(false).s; }
144
149
 
145
- function read() {
146
- let s = { seq: 0, leases: [], queue: [] }, raw = null;
147
- if (fs.existsSync(paths.state)) {
148
- try { raw = fs.readFileSync(paths.state, 'utf8'); s = JSON.parse(raw); }
149
- catch {
150
- const bad = `${paths.state}.corrupt-${now()}`;
151
- fs.copyFileSync(paths.state, bad);
152
- say(`state file was unreadable; saved it to ${bad} and started fresh. Running jobs keep running but aren't tracked.`);
153
- }
150
+ // A state file that doesn't parse (a disk error, a power cut, an editor) is put right under the
151
+ // lock before anyone reads it (src/lock.mjs heals it from state.json.bak, src/durable.mjs), and
152
+ // what came back and what was lost goes in the alerts. Looked at again here in case that couldn't
153
+ // be done; a reader without the lock shows the last good copy and changes nothing. A file that
154
+ // can't be read at all (not one that's broken) throws: starting fresh over it would forget
155
+ // every lease for a passing error.
156
+ function read(locked = true) {
157
+ let raw = null;
158
+ const load = () => { try { raw = fs.readFileSync(paths.state, 'utf8'); } catch (e) { if (e.code !== 'ENOENT') throw e; raw = null; } };
159
+ load();
160
+ let s = raw == null ? null : parsed(raw);
161
+ if (raw != null && !s && locked && heal(paths.state)) { load(); s = raw == null ? null : parsed(raw); }
162
+ if (raw != null && !s) {
163
+ // Moved aside first, so the write that follows doesn't keep the broken file as the last good copy.
164
+ if (locked) try { fs.renameSync(paths.state, `${paths.state}.corrupt-${now()}`); } catch {}
165
+ s = lastGood(paths.state)?.v; raw = null;
154
166
  }
167
+ s ||= { seq: 0, leases: [], queue: [] };
155
168
  s.seq ||= 0; s.leases ||= []; s.queue ||= [];
156
169
  return { s, raw };
157
170
  }
@@ -169,7 +182,7 @@ function reap(s) {
169
182
  killLease(l);
170
183
  // Its dibs was killed, so nobody recorded what it measured. A job big enough
171
184
  // to get its dibs killed is the one the next run most needs to plan for.
172
- if (l.peak?.mem) record(l.key, t - l.start - (l.pausedMs || 0), l.who, peakOf(l), true);
185
+ if (l.peak?.mem) record(l.key, t - l.start - (l.pausedMs || 0), l.who, peakOf(l), true, l.repo);
173
186
  return false;
174
187
  });
175
188
  // A waiter whose process is gone leaves the line, however it went: a place
@@ -209,6 +222,17 @@ function reap(s) {
209
222
  }
210
223
  return q.pid || (q.keepUntil && t < q.keepUntil);
211
224
  });
225
+ // A job waiting --after another (src/after.mjs) whose dibs is gone leaves with it, as a waiter does.
226
+ if (s.afters) {
227
+ s.afters = s.afters.filter(x => {
228
+ if (alive(x.pid, x.pstart)) return true;
229
+ vanished.push({ at: t, kind: 'left', id: x.id, res: x.res, who: x.who, type: 'after', ms: t - x.enq, why: 'gone' });
230
+ return false;
231
+ });
232
+ if (!s.afters.length) delete s.afters;
233
+ }
234
+ // A dibs halt past its time lifts itself (src/halt.mjs).
235
+ if (s.halt?.until != null && t >= s.halt.until) delete s.halt;
212
236
  // What the owner stopped, for a waiter that finds its place gone to say why.
213
237
  if (s.stopped) {
214
238
  for (const id of Object.keys(s.stopped)) if (t - s.stopped[id].at > STOPPED_FOR) delete s.stopped[id];
@@ -257,7 +281,7 @@ const HISTORY_CHUNK = 256 * 1024; // about 2,000 lines, so one read nearly alw
257
281
  export function history() {
258
282
  const lines = tail(paths.history, HISTORY_KEEP);
259
283
  if (lines.length < HISTORY_KEEP) lines.unshift(...tail(paths.historyOlder, HISTORY_KEEP - lines.length));
260
- return lines.slice(-HISTORY_KEEP).flatMap(l => { try { return [JSON.parse(l)]; } catch { return []; } });
284
+ return jsonLines(lines.slice(-HISTORY_KEEP).join('\n'));
261
285
  }
262
286
 
263
287
  // The last `need` lines of a file (fewer if it has fewer), none for an empty or missing one.
@@ -286,8 +310,10 @@ function tail(file, need) {
286
310
  // only: its time says nothing about a run's (schedule.mjs typical skips it).
287
311
  // Run times are worth less than the run: on a full disk this one goes unrecorded.
288
312
  // A lease's peak for its history line, marked when the watcher cleared caches during it.
289
- export const peakOf = l => (l.cleaned && l.peak ? { ...l.peak, cleaned: true } : l.peak);
290
- export function record(key, duration, who, peak, failed = false) {
291
- try { fs.appendFileSync(paths.history, JSON.stringify({ key, dur: duration, who, at: now(), ...(peak ? { mem: peak.mem, cpu: peak.cpu, ...(peak.fit && { fit: true }), ...(peak.in != null && { peakIn: Math.round(peak.in) }), ...(peak.ate != null && { ate: Math.round(peak.ate) }), ...(peak.cleaned && { cleaned: true }) } : {}), ...(failed && { failed: true }) }) + '\n'); }
313
+ // cpuMs: the CPU it had in all (src/measure.mjs), so a later run of the same command is judged against its own.
314
+ export const peakOf = l => (l.peak ? { ...l.peak, ...(l.cleaned && { cleaned: true }), ...(l.cpuMs != null && { cpuMs: l.cpuMs }) } : l.peak);
315
+ // repo: the project it ran in, so a size learned in one repo isn't lent to the same command in another (src/measure.mjs learned).
316
+ export function record(key, duration, who, peak, failed = false, repo = null) {
317
+ try { fs.appendFileSync(paths.history, JSON.stringify({ key, dur: duration, who, at: now(), ...(repo && { repo }), ...(peak ? { mem: peak.mem, cpu: peak.cpu, ...(peak.fit && { fit: true }), ...(peak.in != null && { peakIn: Math.round(peak.in) }), ...(peak.ate != null && { ate: Math.round(peak.ate) }), ...(peak.cleaned && { cleaned: true }), ...(peak.cpuMs != null && { cpuMs: Math.round(peak.cpuMs) }) } : {}), ...(failed && { failed: true }) }) + '\n'); }
292
318
  catch (e) { if (!noSpace(e)) throw e; }
293
319
  }