@dibssoftware/dibs 0.0.0-stage → 0.0.179

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 (75) hide show
  1. package/CHANGELOG.md +594 -0
  2. package/LICENSE +670 -0
  3. package/README.md +85 -2
  4. package/bin/dibs-guard.mjs +7 -0
  5. package/bin/dibs.mjs +548 -0
  6. package/docs/benchmarks.md +193 -0
  7. package/docs/files.md +30 -0
  8. package/docs/guard.md +91 -0
  9. package/docs/later.md +43 -0
  10. package/docs/machines.md +147 -0
  11. package/docs/questions.md +78 -0
  12. package/docs/reference.md +157 -0
  13. package/docs/relay.md +196 -0
  14. package/docs/releasing.md +27 -0
  15. package/docs/scheduling.md +163 -0
  16. package/docs/simulators.md +17 -0
  17. package/package.json +45 -4
  18. package/skills/dibs/SKILL.md +183 -0
  19. package/src/acquire.mjs +700 -0
  20. package/src/admin.mjs +145 -0
  21. package/src/agents.mjs +145 -0
  22. package/src/alerts.mjs +499 -0
  23. package/src/answers.mjs +65 -0
  24. package/src/board.mjs +202 -0
  25. package/src/broker.mjs +369 -0
  26. package/src/commands.mjs +810 -0
  27. package/src/config.mjs +324 -0
  28. package/src/connect.mjs +106 -0
  29. package/src/cool.mjs +160 -0
  30. package/src/diskwarn.mjs +154 -0
  31. package/src/events.mjs +96 -0
  32. package/src/files.mjs +1123 -0
  33. package/src/guard.mjs +613 -0
  34. package/src/history.mjs +247 -0
  35. package/src/hogs.mjs +227 -0
  36. package/src/later.mjs +487 -0
  37. package/src/measure.mjs +262 -0
  38. package/src/news.mjs +101 -0
  39. package/src/nodecheck.mjs +17 -0
  40. package/src/notes.mjs +171 -0
  41. package/src/notices.mjs +160 -0
  42. package/src/ondemand.mjs +572 -0
  43. package/src/overlap.mjs +151 -0
  44. package/src/override.mjs +360 -0
  45. package/src/owner.mjs +30 -0
  46. package/src/plain.mjs +470 -0
  47. package/src/platform.mjs +357 -0
  48. package/src/preempt.mjs +438 -0
  49. package/src/primer.mjs +89 -0
  50. package/src/proc.mjs +99 -0
  51. package/src/progress.mjs +187 -0
  52. package/src/qr.mjs +204 -0
  53. package/src/questions.mjs +389 -0
  54. package/src/quota.mjs +81 -0
  55. package/src/reads.mjs +54 -0
  56. package/src/relay.mjs +779 -0
  57. package/src/remote.mjs +584 -0
  58. package/src/reset.mjs +127 -0
  59. package/src/schedule.mjs +1020 -0
  60. package/src/scratch.mjs +61 -0
  61. package/src/serve.mjs +742 -0
  62. package/src/service.mjs +221 -0
  63. package/src/shard.mjs +59 -0
  64. package/src/simctl.mjs +42 -0
  65. package/src/simpool.mjs +258 -0
  66. package/src/state.mjs +201 -0
  67. package/src/stats.mjs +328 -0
  68. package/src/style.mjs +44 -0
  69. package/src/talk.mjs +702 -0
  70. package/src/thin.sh +130 -0
  71. package/src/trust.mjs +152 -0
  72. package/src/util.mjs +184 -0
  73. package/src/version.mjs +2 -0
  74. package/src/web.mjs +1119 -0
  75. package/src/worker.mjs +318 -0
package/src/reset.mjs ADDED
@@ -0,0 +1,127 @@
1
+ // onRelease: a command that puts a resource back after a lease on it ends,
2
+ // however it ends, so the next user finds it the way the config says (the
3
+ // shared simulator back at the default text size, say).
4
+ //
5
+ // The end is seen inside withState by whichever dibs call writes the state
6
+ // without the lease: dibs run as its command exits, dibs release, or any call
7
+ // that reaps a crashed holder or an expired hold. That call queues one entry
8
+ // per resource in s.resets, and once the lock is free it starts a supervisor
9
+ // (this file, run as a script) that runs the command with a timeout, logs any
10
+ // failure, and removes the entry. Queued under the lock, so each lease end
11
+ // queues exactly once; run outside it, so a slow command blocks nobody's state.
12
+ //
13
+ // While an entry is there, the next lease on that resource waits. Otherwise the
14
+ // reset races the next user, who'd screenshot a simulator still changing text
15
+ // size under them. The wait is bounded: the command is killed at its timeout,
16
+ // and an entry whose supervisor died or never started is dropped by the next
17
+ // state read.
18
+ import fs from 'node:fs';
19
+ import path from 'node:path';
20
+ import { spawn } from 'node:child_process';
21
+ import { fileURLToPath } from 'node:url';
22
+ import { paths, HOME } from './config.mjs';
23
+ import { withState } from './state.mjs';
24
+ import { pstart } from './proc.mjs';
25
+ import { stopTree, OS } from './platform.mjs';
26
+ import { coveredNow } from './schedule.mjs';
27
+ import { ms, dur, now, LEAN } from './util.mjs';
28
+
29
+ export const RESET_TIMEOUT = 30e3; // when the resource's onReleaseTimeout doesn't say
30
+ // A supervisor checks in within a second, even on a loaded machine. One that
31
+ // hasn't by this time never will, and its entry stops holding the resource.
32
+ const START_GRACE = 15e3;
33
+ const LOG = path.join(HOME, 'onrelease.log');
34
+ const KEEP_OUTPUT = 2000; // characters of a failed command's output kept in the log
35
+ const SELF = fileURLToPath(import.meta.url);
36
+
37
+ let starting = [];
38
+
39
+ // The user's file only: onRelease has no default. Read raw, since this runs
40
+ // under the lock, where dying on a bad file would leave the lock behind.
41
+ function settings() {
42
+ try { return JSON.parse(fs.readFileSync(paths.config, 'utf8')).resources || {}; } catch { return {}; }
43
+ }
44
+
45
+ // A run under its own session's hold isn't the end of that session's turn,
46
+ // and neither is a hold whose covered run is still going: the last of the two
47
+ // to end resets the resource.
48
+ function stillTheirs(s, l, r) {
49
+ if (l.kind === 'hold') return s.leases.some(x => x.kind === 'run' && x.covered?.includes(r) && x.spid && x.spid === l.pid);
50
+ return coveredNow(s, l, r);
51
+ }
52
+
53
+ // Called by withState with the leases this change removed.
54
+ export function queueResets(s, gone) {
55
+ if (!gone.length) return;
56
+ const conf = settings();
57
+ for (const l of gone) for (const r of l.res) {
58
+ const c = conf[r];
59
+ if (typeof c?.onRelease !== 'string' || stillTheirs(s, l, r)) continue;
60
+ const timeout = /^[\d.]+\s*(ms|s|m|h)?$/.test(String(c.onReleaseTimeout ?? '')) ? ms(c.onReleaseTimeout) : RESET_TIMEOUT;
61
+ const x = { id: ++s.seq, res: r, cmd: c.onRelease, timeout, lease: l.id, who: l.who, agent: l.agent, at: now() };
62
+ (s.resets ||= []).push(x);
63
+ starting.push(x.id);
64
+ }
65
+ }
66
+
67
+ // After the lock is released: start a supervisor for each reset this change
68
+ // queued. A change that wasn't written starts nothing.
69
+ export function startResets(written) {
70
+ const ids = starting.splice(0);
71
+ if (!written) return;
72
+ for (const id of ids) {
73
+ const p = spawn(process.execPath, [...LEAN, SELF, String(id)], { detached: true, stdio: 'ignore', windowsHide: true, env: { ...process.env, DIBS_HOME: HOME } });
74
+ p.on('error', () => {}); // its entry is dropped once START_GRACE passes
75
+ p.unref();
76
+ }
77
+ }
78
+
79
+ // On every state read: drop entries nothing is working on any more. A
80
+ // command whose supervisor is gone is killed with the entry, so the next user
81
+ // never gets the resource while it's still being reset under them.
82
+ export function reapResets(s, alive) {
83
+ if (!s.resets?.length) return;
84
+ const t = now();
85
+ s.resets = s.resets.filter(x => {
86
+ if (x.pid ? alive(x.pid, x.pstart) && t < x.started + x.timeout + START_GRACE : t - x.at < START_GRACE) return true;
87
+ if (x.cpid && alive(x.cpid, x.cpstart)) stopTree(x.cpid, 'SIGKILL');
88
+ note(x, x.pid ? 'its supervisor died before the command finished' : "its supervisor didn't start, so the command never ran");
89
+ return false;
90
+ });
91
+ }
92
+
93
+ function note(x, what, output = '') {
94
+ const tail = output.trim() ? `\n ${output.trim().split('\n').join('\n ')}` : '';
95
+ try { fs.appendFileSync(LOG, `${new Date().toISOString()} ${x.res} after ${x.agent || x.who}: ${what}: ${x.cmd}${tail}\n`); } catch {}
96
+ }
97
+
98
+ // The supervisor: claim the entry, run the command, log a failure, let the resource go.
99
+ async function supervise(id) {
100
+ const started = pstart(process.pid);
101
+ const x = withState(s => {
102
+ const x = (s.resets || []).find(r => r.id === id && !r.pid);
103
+ if (x) Object.assign(x, { pid: process.pid, pstart: started, started: now() });
104
+ return x && { ...x };
105
+ });
106
+ if (!x) return;
107
+ const child = spawn(x.cmd, { shell: true, detached: OS !== 'windows', cwd: HOME, stdio: ['ignore', 'pipe', 'pipe'], windowsHide: true,
108
+ env: { ...process.env, DIBS_RESOURCE: x.res, DIBS_RELEASED_BY: x.agent || x.who } });
109
+ // The command's group, so a reap that finds this supervisor dead can stop it too.
110
+ if (child.pid) withState(s => { const r = (s.resets || []).find(r => r.id === id); if (r) Object.assign(r, { cpid: child.pid, cpstart: pstart(child.pid) }); });
111
+ let output = '';
112
+ const keep = d => { output = (output + d).slice(-KEEP_OUTPUT); };
113
+ child.stdout.on('data', keep);
114
+ child.stderr.on('data', keep);
115
+ let killed = false;
116
+ const timer = setTimeout(() => { killed = true; stopTree(child.pid, 'SIGKILL'); }, x.timeout);
117
+ const code = await new Promise(r => {
118
+ child.on('error', e => { keep(e.message); r(127); });
119
+ child.on('exit', (c, sig) => r(c ?? sig));
120
+ });
121
+ clearTimeout(timer);
122
+ if (killed) note(x, `killed after ${dur(x.timeout)} (onReleaseTimeout)`, output);
123
+ else if (code !== 0) note(x, `exited ${code}`, output);
124
+ withState(s => { s.resets = (s.resets || []).filter(r => r.id !== id); });
125
+ }
126
+
127
+ if (process.argv[1] && path.resolve(process.argv[1]) === SELF) supervise(Number(process.argv[2]));