@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
@@ -0,0 +1,160 @@
1
+ // Notices about an agent's own jobs, left in its inbox (src/talk.mjs) so the
2
+ // guard hook hands each over once, beside its next tool call. Most agents have
3
+ // nobody watching the board for them: this is how they hear that a job of
4
+ // theirs looks stuck while others wait, used far more memory than it asked
5
+ // for, or was paused for low disk or memory and shouldn't be run again.
6
+ //
7
+ // The scan reads state.json without its lock (writes are renames, so it is
8
+ // always whole) and runs from processes that are alive anyway: every waiting
9
+ // dibs and every running one. Each notice has a name (idle:#12), so however
10
+ // many of them scan, an agent hears about each thing once.
11
+ import fs from 'node:fs';
12
+ import path from 'node:path';
13
+ import { paths, HOME } from './config.mjs';
14
+ import { peek, history } from './state.mjs';
15
+ import { notify } from './talk.mjs';
16
+ import { minutes, stuckFor } from './plain.mjs';
17
+ import { stuckLeases } from './acquire.mjs';
18
+ import { overAskLine } from './measure.mjs';
19
+ import { now, callerPid, clock } from './util.mjs';
20
+ import { read as readPosts, tasks, QUIET_AFTER, EXPIRE_AFTER } from './progress.mjs';
21
+ import { quoted } from './style.mjs';
22
+
23
+ // Which runs look stuck and which used more memory than they asked for is
24
+ // decided where the board decides it (stuckLeases, l.overAsk), so the notice
25
+ // and the board never disagree. This file only delivers.
26
+ // How often one process looks. The facts change on a scale of minutes.
27
+ export const NOTICE_EVERY = 20e3;
28
+
29
+ let last = 0;
30
+ const sent = new Set();
31
+
32
+ const what = l => `#${l.id} (${l.purpose ? `"${l.purpose}", ` : ''}${(l.cmd || l.res.join(',')).slice(0, 60)})`;
33
+
34
+ // [lease, once, text] for each notice the state holds, for the lease's agent.
35
+ export function noticesIn(s, t = now()) {
36
+ const out = [];
37
+ const waiting = (s.queue || []).filter(q => q.pid);
38
+ const stuck = new Set(stuckLeases({ queue: [], leases: [], ...s }, t));
39
+ for (const l of s.leases || []) {
40
+ const others = waiting.filter(q => q.res.some(r => l.res.includes(r)));
41
+ if (stuck.has(l) && others.length)
42
+ out.push([l, `idle:#${l.id}`, `your job ${what(l)} has used no CPU for ${minutes(stuckFor(l, t))} while ${others.length} other `
43
+ + `${others.length === 1 ? 'job waits' : 'jobs wait'} for ${l.res.join(', ')}. If it's stuck, end it: stop the tool call or background task `
44
+ + `running it, or kill ${l.pid} (its dibs run, which takes the whole job down and frees the turn). If it's still working, leave it.`]);
45
+ if (l.overAsk?.used && l.overAsk.asked && l.overAsk.suggest)
46
+ out.push([l, `mem:#${l.id}`, `your job ${what(l)}: ${overAskLine(l.overAsk)}`]);
47
+ if (l.loadPast)
48
+ out.push([l, `loadpast:#${l.id}`, `your job ${what(l)} waited ${minutes(l.loadWaited || 0)} for the machine's load to fall, so it started anyway at background priority on the efficiency cores. `
49
+ + 'It runs slower there. If a person is waiting on it now, raise it: dibs boost ' + l.id + ' high -m "why".']);
50
+ const watcher = (l.pauses || []).filter(p => p.watcher && !p.gone);
51
+ if (l.pausedAt && watcher.length)
52
+ out.push([l, `paused:#${l.id}:${l.pausedAt}`, `your job ${what(l)} was paused because free ${watcher.some(p => p.disk) ? 'disk' : 'memory'} is low. `
53
+ + "It keeps its turn and resumes by itself when there's room: don't run it again or end it."]);
54
+ }
55
+ return out;
56
+ }
57
+
58
+ // Leave the notices the state holds, at most every NOTICE_EVERY ms from this process.
59
+ // Never throws: a notice is a nicety, and its callers are mid-wait or mid-run.
60
+ export function noticeScan(t = now()) {
61
+ if (t - last < NOTICE_EVERY) return;
62
+ last = t;
63
+ try {
64
+ const s = JSON.parse(fs.readFileSync(paths.state, 'utf8'));
65
+ for (const [l, once, text] of noticesIn(s, t)) {
66
+ if (sent.has(once)) continue;
67
+ sent.add(once);
68
+ notify(l, once, text);
69
+ }
70
+ } catch {}
71
+ }
72
+
73
+ // At most one nudge per task this often. On 2026-11-03, 5 of 35 notices were
74
+ // these, and agents were posting progress only to stop them.
75
+ export const NAG_EVERY = 2 * 36e5;
76
+ // An agent with no job running or waiting, and none run in this long, isn't
77
+ // working now: its open task waits quietly for it to come back.
78
+ export const AWAY_AFTER = 30 * 6e4;
79
+ const NAGS = path.join(HOME, 'nags.json');
80
+
81
+ // [to, once, text, task] for each task still in progress that needs a word
82
+ // from whoever posted it: past its ETA, or with no post for QUIET_AFTER.
83
+ // Expired tasks are let go. Only tasks posted from a Claude Code session: the
84
+ // hook is how the notice arrives. A subagent's task is nudged to that subagent;
85
+ // once it has ended (ended(k)), its lead hears once that it was left open.
86
+ // nagged: when each task was last nudged; working(k): whether the agent the
87
+ // notice would go to is at work now.
88
+ export function progressNoticesIn(posts, t = now(), { nagged = {}, working = () => true, ended = () => false } = {}) {
89
+ const out = [];
90
+ for (const k of tasks(posts, t)) {
91
+ if (k.state !== 'doing' || !k.session) continue;
92
+ const task = `${k.who}:${k.agentId || 'main'}:${k.task || k.project}`;
93
+ const name = k.task ? `"${k.task}"` : `in ${k.project || 'this folder'}`;
94
+ if (k.agentId && ended(k)) {
95
+ // Said once: nothing to do, it expires by itself; the lead only shouldn't be surprised.
96
+ const lead = `${task}:lead`;
97
+ if (nagged[lead] || !working({ ...k, agentId: null }) || !(k.overdue || t - k.last > QUIET_AFTER)) continue;
98
+ out.push([{ sessionId: k.session, who: k.who }, `left:${task}`, `your subagent ${k.label || k.agentId.slice(0, 8)} ended with its progress task ${name} still open `
99
+ + `(last post at ${clock(k.last)}). Nothing to do: it's let go ${minutes(Math.max(k.last, k.eta || 0) + EXPIRE_AFTER - t)} from now. `
100
+ + `If the work goes on, post it under your own task.`, lead]);
101
+ continue;
102
+ }
103
+ if (t - (nagged[task] || 0) < NAG_EVERY || !working(k)) continue;
104
+ const to = { sessionId: k.session, who: k.who, ...(k.agentId && { agentId: k.agentId }) };
105
+ const again = (...more) => quoted(['dibs', 'progress', 'where it stands', ...(k.task ? ['--task', k.task] : []), ...more]);
106
+ if (k.overdue) out.push([to, `late:${task}:${k.eta}`, `your progress task ${name} was due at ${clock(k.eta)} and is still open. `
107
+ + `If it's finished, say so: ${again('--done')}. If not, post where it stands with a new time: ${again('--eta', '30m')}.`, task]);
108
+ else if (t - k.last > QUIET_AFTER) out.push([to, `quiet:${task}:${k.last}`, `your progress task ${name} has had no post for ${minutes(t - k.last)}. `
109
+ + `Post where it stands (${again()}), or close it: --done if it's finished, --blocked if it's stuck.`, task]);
110
+ }
111
+ return out;
112
+ }
113
+
114
+ const DAY = 864e5;
115
+ // From the throttle watcher (src/cool.mjs), which is always running: no waiter needed.
116
+ export function progressScan(t = now()) {
117
+ try {
118
+ let nagged = {};
119
+ try { nagged = JSON.parse(fs.readFileSync(NAGS, 'utf8')); } catch {}
120
+ const s = peek(), hist = history();
121
+ // At work: a job running or waiting, or one run in the last AWAY_AFTER. A
122
+ // subagent counts only its own jobs (they carry its id) and its own posts.
123
+ const mine = k => x => x.sessionId === k.session && (k.agentId ? x.agentId === k.agentId : true);
124
+ const busy = k => (s.leases || []).some(mine(k)) || (s.queue || []).some(q => q.pid && mine(k)(q));
125
+ const working = k => (k.agentId ? busy(k) || t - k.last < AWAY_AFTER
126
+ : busy(k) || (s.leases || []).some(l => l.who === k.who) || hist.some(r => r.who === k.who && t - r.at < AWAY_AFTER));
127
+ const ended = k => !busy(k) && t - k.last >= AWAY_AFTER;
128
+ const before = JSON.stringify(nagged);
129
+ for (const [to, once, text, task] of progressNoticesIn(readPosts({ since: t - DAY }), t, { nagged, working, ended })) {
130
+ if (sent.has(once)) continue;
131
+ sent.add(once);
132
+ if (notify(to, once, text)) nagged[task] = t;
133
+ }
134
+ // A lead's one word about a subagent's task is kept as long as the task can be: never twice.
135
+ for (const [k, at] of Object.entries(nagged)) if (t - at >= (k.endsWith(':lead') ? DAY : NAG_EVERY)) delete nagged[k];
136
+ if (JSON.stringify(nagged) !== before) fs.writeFileSync(NAGS, JSON.stringify(nagged));
137
+ } catch {}
138
+ }
139
+
140
+ // The same command from the same agent, already waiting or running, started
141
+ // by another tool call: almost always a retry after Claude Code moved the
142
+ // first call to the background. Running it again would run it twice, so the
143
+ // second copy refuses (exit 2) and says where the first one is. A copy from
144
+ // the same shell (a script starting several at once) has the same parent and
145
+ // isn't a retry; neither is one with a different -m, which is how an agent
146
+ // asks for a second copy on purpose. Entries from before 0.0.86 carry no
147
+ // parent or folder and never match. Returns { id, text }: the first copy's id and what to say.
148
+ export function twinOf(s, o, id, t = now()) {
149
+ const cmd = (o.cmd || []).join(' ');
150
+ const res = [...o.res].sort().join();
151
+ const same = x => x.cmd === cmd && [...x.res].sort().join() === res && (x.purpose || '') === (o.purpose || '')
152
+ && x.cwd === process.cwd() && x.ppid && x.ppid !== callerPid()
153
+ && (id.spid ? x.spid === id.spid && (x.agentId || null) === (o.agentId || null) : !x.spid && x.who === id.who);
154
+ const x = (s.leases || []).find(l => l.kind === 'run' && same(l)) || (s.queue || []).find(q => q.pid && same(q));
155
+ if (!x) return null;
156
+ const running = !!(s.leases || []).includes(x);
157
+ return { id: x.id, text: `you already have this exact command ${running ? `running as #${x.id} (for ${minutes(t - x.start)})` : `waiting in line as #${x.id}`}, `
158
+ + 'from an earlier call. Nothing ran here (exit 2): a second copy would run it twice. That earlier call reports when it ends; '
159
+ + "if it went to the background, wait for its notice instead of running it again. To run a second copy at the same time on purpose, give it a different -m." };
160
+ }