@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.
- package/CHANGELOG.md +111 -1
- package/README.md +10 -8
- package/bin/dibs.mjs +379 -74
- package/docs/agents.md +303 -0
- package/docs/council.md +88 -0
- package/docs/files.md +3 -3
- package/docs/guard.md +86 -11
- package/docs/json.md +211 -0
- package/docs/later.md +5 -5
- package/docs/machines.md +6 -6
- package/docs/questions.md +104 -3
- package/docs/reference.md +124 -30
- package/docs/relay.md +135 -11
- package/docs/releasing.md +7 -1
- package/docs/scheduling.md +28 -6
- package/docs/secrets.md +53 -0
- package/docs/simulators.md +3 -3
- package/docs/windows.md +3 -3
- package/package.json +1 -1
- package/skills/dibs/SKILL.md +79 -149
- package/src/about.mjs +341 -0
- package/src/acquire.mjs +113 -28
- package/src/after.mjs +127 -0
- package/src/agents.mjs +2 -1
- package/src/alerts.mjs +109 -48
- package/src/answers.mjs +2 -2
- package/src/board.mjs +21 -16
- package/src/broker.mjs +23 -7
- package/src/carry.mjs +287 -0
- package/src/codex.mjs +132 -0
- package/src/commands.mjs +254 -86
- package/src/config.mjs +85 -14
- package/src/connect.mjs +4 -2
- package/src/cool.mjs +28 -12
- package/src/council.mjs +482 -0
- package/src/cwd.mjs +9 -0
- package/src/decisions.mjs +250 -0
- package/src/declare.mjs +96 -0
- package/src/diskreport.mjs +306 -0
- package/src/diskuse.mjs +420 -0
- package/src/diskwarn.mjs +68 -13
- package/src/doctor.mjs +149 -0
- package/src/doors.mjs +547 -0
- package/src/durable.mjs +143 -0
- package/src/envs.mjs +372 -0
- package/src/events.mjs +7 -2
- package/src/files.mjs +121 -7
- package/src/guard.mjs +288 -105
- package/src/guardd.mjs +8 -1
- package/src/halt.mjs +63 -0
- package/src/handoffs.mjs +362 -0
- package/src/history.mjs +34 -4
- package/src/hogs.mjs +36 -8
- package/src/jobprogress.mjs +283 -0
- package/src/later.mjs +21 -10
- package/src/lock.mjs +51 -11
- package/src/measure.mjs +60 -14
- package/src/media.mjs +240 -0
- package/src/news.mjs +31 -0
- package/src/notes.mjs +49 -8
- package/src/notices.mjs +126 -43
- package/src/ondemand.mjs +25 -2
- package/src/outcome.mjs +163 -0
- package/src/overlap.mjs +2 -2
- package/src/override.mjs +149 -79
- package/src/owner.mjs +15 -2
- package/src/people.mjs +236 -0
- package/src/pickup.mjs +72 -0
- package/src/picture.mjs +321 -0
- package/src/plain.mjs +84 -18
- package/src/platform.mjs +29 -7
- package/src/preempt.mjs +123 -15
- package/src/primer.mjs +91 -20
- package/src/prints.mjs +99 -0
- package/src/proc.mjs +11 -1
- package/src/progress.mjs +49 -3
- package/src/proof.mjs +102 -0
- package/src/push.mjs +271 -0
- package/src/questions.mjs +328 -57
- package/src/quota.mjs +1 -1
- package/src/recap.mjs +205 -0
- package/src/relay.mjs +262 -26
- package/src/remote.mjs +23 -10
- package/src/reviewgate.mjs +268 -0
- package/src/reviews.mjs +377 -0
- package/src/route.mjs +263 -0
- package/src/rules.mjs +216 -0
- package/src/schedule.mjs +238 -44
- package/src/schema.mjs +62 -0
- package/src/scratch.mjs +2 -2
- package/src/serve.mjs +3 -2
- package/src/service.mjs +5 -1
- package/src/signed.mjs +97 -0
- package/src/simpool.mjs +46 -1
- package/src/spend.mjs +375 -0
- package/src/state.mjs +43 -17
- package/src/talk.mjs +1082 -134
- package/src/thin.sh +2 -0
- package/src/trail.mjs +290 -0
- package/src/trailnote.mjs +209 -0
- package/src/util.mjs +7 -0
- package/src/vault.mjs +435 -0
- package/src/vendors.mjs +327 -0
- package/src/version.mjs +1 -1
- package/src/web.mjs +354 -60
- package/src/work.mjs +487 -0
- package/src/worker.mjs +7 -1
package/src/override.mjs
CHANGED
|
@@ -22,17 +22,19 @@ import { withState, history } from './state.mjs';
|
|
|
22
22
|
import { ownerPause, pauseLabel } from './preempt.mjs';
|
|
23
23
|
import { PRIORITIES, BACKGROUND, NEEDS_REASON, priority, byRank, roomFor } from './schedule.mjs';
|
|
24
24
|
import { actor } from './owner.mjs';
|
|
25
|
-
import { event } from './events.mjs';
|
|
26
|
-
import { die, now, say, sleepSync, unpolled } from './util.mjs';
|
|
25
|
+
import { event, readEvents } from './events.mjs';
|
|
26
|
+
import { die, now, say, sleepSync, unpolled, dur, clock } from './util.mjs';
|
|
27
27
|
import { takeAgent, callKey } from './files.mjs';
|
|
28
28
|
import { quoted } from './style.mjs';
|
|
29
|
+
import { described } from './declare.mjs';
|
|
30
|
+
import { notify } from './talk.mjs';
|
|
29
31
|
|
|
30
32
|
const rankOf = p => PRIORITIES.indexOf(p || 'normal');
|
|
31
33
|
const USAGE = {
|
|
32
|
-
boost: 'dibs boost <id or agent> <urgent|high|normal|low|idle> [-m "why"]
|
|
33
|
-
pause: 'dibs pause <id> [--force] [-m "why"]
|
|
34
|
-
resume: 'dibs resume <id> [-m "why"]
|
|
35
|
-
stop: 'dibs stop <id> [-m "why"]
|
|
34
|
+
boost: 'dibs boost <id or agent> <urgent|high|normal|low|idle> [-m "why"]',
|
|
35
|
+
pause: 'dibs pause <id> [--force] [-m "why"]',
|
|
36
|
+
resume: 'dibs resume <id> [-m "why"]',
|
|
37
|
+
stop: 'dibs stop [<id> ...|<words>|mine] [-m "why"]',
|
|
36
38
|
cap: 'dibs cap <agent> <urgent|high|normal|low|idle|off>',
|
|
37
39
|
};
|
|
38
40
|
const IDS = 'Ids are the #numbers on the board (dibs), or "id" in dibs --json.';
|
|
@@ -45,18 +47,12 @@ const recordAll = (act, changes, who, reason = '') => {
|
|
|
45
47
|
// What goes on the job: the agent's name, the person's paired browser, or nothing for the person here.
|
|
46
48
|
const byOf = who => (who.agent || who.via ? { by: who.name } : {});
|
|
47
49
|
|
|
50
|
+
// --owner <session> was once needed on another agent's job. It guarded nothing (any agent
|
|
51
|
+
// could pass what dibs printed), so it's gone (2026-11-29); old callers still pass it, and
|
|
52
|
+
// it's taken and ignored.
|
|
48
53
|
function argsOf(verb, args, flags = []) {
|
|
49
54
|
const o = args.indexOf('--owner');
|
|
50
|
-
|
|
51
|
-
if (o >= 0) {
|
|
52
|
-
if (!args[o + 1] || args[o + 1].startsWith('-')) die(`--owner needs the job's owner: its session, or session/agent, as the board shows it. Use: ${USAGE[verb] || verb}`);
|
|
53
|
-
owner = args[o + 1];
|
|
54
|
-
args = [...args.slice(0, o), ...args.slice(o + 2)];
|
|
55
|
-
}
|
|
56
|
-
const parsed = argsOf1(verb, args, flags);
|
|
57
|
-
return { ...parsed, owner };
|
|
58
|
-
}
|
|
59
|
-
function argsOf1(verb, args, flags = []) {
|
|
55
|
+
if (o >= 0) args = [...args.slice(0, o), ...args.slice(args[o + 1] && !args[o + 1].startsWith('-') ? o + 2 : o + 1)];
|
|
60
56
|
const m = args.findIndex(a => a === '-m' || a === '--msg');
|
|
61
57
|
let reason = '';
|
|
62
58
|
if (m >= 0) {
|
|
@@ -77,7 +73,7 @@ function prioArg(verb, p) {
|
|
|
77
73
|
|
|
78
74
|
const byName = name => x => x.agent === name || x.who === name;
|
|
79
75
|
|
|
80
|
-
// Whose a job is, as
|
|
76
|
+
// Whose a job is, as the board names it: its session, then /agent for a subagent's.
|
|
81
77
|
export const ownerOf = x => `${x.agent || x.who}${x.label ? `/${x.label}` : ''}`;
|
|
82
78
|
// The calling agent: its session, and the subagent the guard hook saw make this
|
|
83
79
|
// call (src/files.mjs takeAgent), so siblings in one session aren't each other.
|
|
@@ -86,20 +82,12 @@ function caller(verb) {
|
|
|
86
82
|
return { ...identity({ as: a?.agentType || undefined }), agentId: a?.agentId || null };
|
|
87
83
|
}
|
|
88
84
|
// Its own: the same session, and the same subagent when either side was stamped
|
|
89
|
-
// (else the same label).
|
|
90
|
-
//
|
|
85
|
+
// (else the same label). For what dibs stop's words and mine pick from, and
|
|
86
|
+
// whose agent to tell. Nothing is refused for being someone else's: an id typed
|
|
87
|
+
// from memory can be (on 2026-10-19 an agent boosted #10167 and only learned
|
|
88
|
+
// whose it was afterwards), so every answer names whose job it is first, the
|
|
89
|
+
// history keeps who did it, and a stopped job's agent is told.
|
|
91
90
|
const own = (x, me) => isMine(x, me) && (x.agentId || me.agentId ? x.agentId === me.agentId : (x.label || '') === (me.label || ''));
|
|
92
|
-
// A job that isn't the calling agent's own takes --owner naming it, or nothing
|
|
93
|
-
// changes (exit 2). An id typed from memory or a board can be someone else's:
|
|
94
|
-
// on 2026-10-19 an agent boosted #10167 and only learned from the output whose
|
|
95
|
-
// it was, and crews sharing a session stopped each other's jobs. The person is
|
|
96
|
-
// never asked.
|
|
97
|
-
function notYours(verb, x, who, me, want) {
|
|
98
|
-
if (!who.agent || own(x, me)) return null;
|
|
99
|
-
if (want && (want === ownerOf(x) || want === (x.agent || x.who))) return null;
|
|
100
|
-
return `#${x.id} is ${ownerOf(x)}'s ${x.res.join(',')} job${x.purpose ? ` ("${x.purpose}")` : ''}${want ? `, not ${want}'s` : ', not yours'}. Nothing changed.`
|
|
101
|
-
+ ` If you meant it: dibs ${quoted([verb, ...process.argv.slice(3).filter((a, i, all) => a !== '--owner' && all[i - 1] !== '--owner')])} --owner ${ownerOf(x)}`;
|
|
102
|
-
}
|
|
103
91
|
// The first line names whose job it is, before anything else is said about it.
|
|
104
92
|
const naming = (verbing, x) => `${verbing} #${x.id}: ${ownerOf(x)} ${x.res.join(',')}${x.purpose ? ` '${x.purpose}'` : ''}`;
|
|
105
93
|
const what = (s, x) => (s.queue.includes(x) ? 'wait' : x.kind === 'hold' ? 'hold' : 'run');
|
|
@@ -142,10 +130,9 @@ function requeue(l, from) {
|
|
|
142
130
|
// only low and idle runs) and background QoS follow the new priority.
|
|
143
131
|
export function boost(args) {
|
|
144
132
|
const who = actor();
|
|
145
|
-
const me = caller('boost');
|
|
146
133
|
const parsed = argsOf('boost', args);
|
|
147
134
|
let { words: [target, p] } = parsed;
|
|
148
|
-
const { reason
|
|
135
|
+
const { reason } = parsed;
|
|
149
136
|
// The priority before the id (dibs boost high 17727) reads the same.
|
|
150
137
|
if (target && p && !/^\d+$/.test(target) && PRIORITIES.includes(target) && /^\d+$/.test(p)) [target, p] = [p, target];
|
|
151
138
|
if (!target) die(`boost which job? Use: ${USAGE.boost}. ${IDS}`);
|
|
@@ -158,7 +145,6 @@ export function boost(args) {
|
|
|
158
145
|
const hist = history();
|
|
159
146
|
const r = withState(s => {
|
|
160
147
|
const items = /^\d+$/.test(target) ? [...s.leases, ...s.queue].filter(x => String(x.id) === target) : [...s.leases, ...s.queue].filter(byName(target));
|
|
161
|
-
for (const x of items) { const no = notYours('boost', x, who, me, owner); if (no) return { refused: no }; }
|
|
162
148
|
const changes = [];
|
|
163
149
|
for (const x of items) {
|
|
164
150
|
const from = x.prio || 'normal';
|
|
@@ -170,7 +156,6 @@ export function boost(args) {
|
|
|
170
156
|
for (const c of changes) if (c.kind === 'wait') c.place = order.findIndex(q => q.id === c.id) + 1;
|
|
171
157
|
return changes;
|
|
172
158
|
});
|
|
173
|
-
if (r.refused) die(r.refused, 2);
|
|
174
159
|
if (!r.length) die(`nothing matched ${target}: no lease or waiter with that id or agent name. ${IDS}`, 1);
|
|
175
160
|
for (const c of r) {
|
|
176
161
|
out(naming('boosting', c.x));
|
|
@@ -180,7 +165,8 @@ export function boost(args) {
|
|
|
180
165
|
if (c.place) line += `, now #${c.place} in the queue`;
|
|
181
166
|
if (c.qos) {
|
|
182
167
|
// One that exited meanwhile has nothing left to move, a zombie its parent hasn't reaped included.
|
|
183
|
-
|
|
168
|
+
// Where ps can't run, the move counts as failed.
|
|
169
|
+
const gone = pid => { const r = spawnSync('ps', ['-o', 'stat=', '-p', String(pid)], { encoding: 'utf8' }); return r.stdout != null && !/^[^Z]/.test(r.stdout.trim()); };
|
|
184
170
|
const move = pid => (c.qos.bg ? lower(pid) : raise(pid)) || gone(pid);
|
|
185
171
|
// Whether each process was moved, the last time it was tried.
|
|
186
172
|
const done = new Map(c.qos.pids.map(pid => [pid, move(pid)]));
|
|
@@ -214,6 +200,9 @@ export function inversions(s, cfg) {
|
|
|
214
200
|
if (l.kind !== 'run' || !l.pgid || !(l.background ?? BACKGROUND.has(l.prio))) continue;
|
|
215
201
|
const stuck = s.queue.find(q => q.pid && !BACKGROUND.has(q.prio || 'normal') && q.res.some(r => l.res.includes(r) && roomFor(s, q, r, cfg).open <= 0));
|
|
216
202
|
if (stuck && !l.lifted) { l.lifted = stuck.id; moves.push({ id: l.id, res: l.res, who: l.who, pgid: l.pgid, pgstart: l.pgstart, groups: l.groups, bg: false, for: stuck.id }); }
|
|
203
|
+
// Paused, it stays up until it resumes: lowered now, a stop that hasn't landed yet waits on a core
|
|
204
|
+
// it may not get for 45 s (preempt.test, 2026-11-26). The resume lowers it (src/preempt.mjs sync).
|
|
205
|
+
else if (!stuck && l.lifted && l.pausedAt) { l.raisedToStop = true; delete l.lifted; }
|
|
217
206
|
else if (!stuck && l.lifted) { moves.push({ id: l.id, res: l.res, who: l.who, pgid: l.pgid, pgstart: l.pgstart, groups: l.groups, bg: true, for: l.lifted }); delete l.lifted; }
|
|
218
207
|
}
|
|
219
208
|
return moves;
|
|
@@ -250,7 +239,7 @@ function unpausable(l, cfg, force) {
|
|
|
250
239
|
function leaseArg(verb, args, flags) {
|
|
251
240
|
const a = argsOf(verb, args, flags);
|
|
252
241
|
if (a.words.length !== 1 || !/^\d+$/.test(a.words[0])) die(`Use: ${USAGE[verb]}. ${IDS}`);
|
|
253
|
-
return { id: a.words[0], flags: a.flags, reason: a.reason
|
|
242
|
+
return { id: a.words[0], flags: a.flags, reason: a.reason };
|
|
254
243
|
}
|
|
255
244
|
const missing = (s, id) => `no lease #${id}${s.queue.some(q => String(q.id) === id) ? ' (it\'s waiting, not running)' : ''}. ${IDS}`;
|
|
256
245
|
|
|
@@ -258,13 +247,10 @@ const missing = (s, id) => `no lease #${id}${s.queue.some(q => String(q.id) ===
|
|
|
258
247
|
// lease and its slot until dibs resume, or until it ends.
|
|
259
248
|
export function pause(args, cfg) {
|
|
260
249
|
const who = actor();
|
|
261
|
-
const
|
|
262
|
-
const { id, flags, reason, owner } = leaseArg('pause', args, ['--force']);
|
|
250
|
+
const { id, flags, reason } = leaseArg('pause', args, ['--force']);
|
|
263
251
|
const r = withState(s => {
|
|
264
252
|
const l = s.leases.find(x => String(x.id) === id);
|
|
265
253
|
if (!l) return { err: s.queue.some(q => String(q.id) === id) ? `#${id} is waiting, not running: there's nothing to pause yet. To hold it back, lower it: dibs boost ${id} idle` : missing(s, id), code: 1 };
|
|
266
|
-
const no = notYours('pause', l, who, me, owner);
|
|
267
|
-
if (no) return { err: no, code: 2 };
|
|
268
254
|
out(naming('pausing', l));
|
|
269
255
|
const why = unpausable(l, cfg, flags.has('--force'));
|
|
270
256
|
if (why) return { err: why, code: 2 };
|
|
@@ -283,13 +269,10 @@ export function pause(args, cfg) {
|
|
|
283
269
|
// urgent job, for memory) stay until their reason ends.
|
|
284
270
|
export function resume(args) {
|
|
285
271
|
const who = actor();
|
|
286
|
-
const
|
|
287
|
-
const { id, reason, owner } = leaseArg('resume', args);
|
|
272
|
+
const { id, reason } = leaseArg('resume', args);
|
|
288
273
|
const r = withState(s => {
|
|
289
274
|
const l = s.leases.find(x => String(x.id) === id);
|
|
290
275
|
if (!l) return { err: missing(s, id) };
|
|
291
|
-
const no = notYours('resume', l, who, me, owner);
|
|
292
|
-
if (no) return { err: no, code: 2 };
|
|
293
276
|
out(naming('resuming', l));
|
|
294
277
|
if (!l.pauses?.some(p => p.owner))
|
|
295
278
|
return { err: `#${id} isn't paused by you${l.pausedAt ? ` (it's ${pauseLabel(l)}, which dibs undoes by itself)` : ''}. Nothing changed.` };
|
|
@@ -304,48 +287,135 @@ export function resume(args) {
|
|
|
304
287
|
recordAll('resume', [{ id: c.id, res: c.res, who: c.who }], who, reason);
|
|
305
288
|
}
|
|
306
289
|
|
|
307
|
-
// dibs stop
|
|
308
|
-
// would kill it, and its dibs process says
|
|
309
|
-
// the lease back. A waiter's dibs process leaves the line
|
|
310
|
-
// released.
|
|
290
|
+
// dibs stop: the kill switch, for the person and every agent alike. A run's
|
|
291
|
+
// tree is killed as a crash would kill it, and its dibs process says who
|
|
292
|
+
// stopped it and gives the lease back. A waiter's dibs process leaves the line
|
|
293
|
+
// and exits. A hold is released. Anyone's job: the first line names whose it
|
|
294
|
+
// is, the history keeps who stopped it, and its agent is told (src/talk.mjs notify).
|
|
295
|
+
// dibs stop your running and waiting jobs, with their numbers
|
|
296
|
+
// dibs stop 42 [43 ...] those jobs, anyone's
|
|
297
|
+
// dibs stop login tests your job whose -m or command has those words
|
|
298
|
+
// dibs stop mine every job of yours (--all-mine says the same)
|
|
299
|
+
// Words match only your own jobs (a session's main agent: its subagents' too):
|
|
300
|
+
// words are vaguer than a number, and a phrase that happens to fit another
|
|
301
|
+
// agent's job shouldn't end it. A number is said on purpose.
|
|
311
302
|
export function stop(args) {
|
|
312
303
|
const who = actor();
|
|
313
304
|
const me = caller('stop');
|
|
314
|
-
const {
|
|
305
|
+
const { words, reason, flags } = argsOf('stop', args, ['--all-mine']);
|
|
306
|
+
const all = flags.has('--all-mine') || (words.length === 1 && words[0] === 'mine');
|
|
307
|
+
const ids = words.length && words.every(w => /^#?\d+$/.test(w)) ? words.map(w => w.replace(/^#/, '')) : null;
|
|
308
|
+
// An agent's own jobs, its subagents' included when it's the session's main agent; the person's,
|
|
309
|
+
// by name. A bare dibs stop from the person lists every job: any of them is theirs to stop.
|
|
310
|
+
const mineOf = s => liveOf(s).filter(x => (who.agent ? yours(x, me) : isMine(x, me)));
|
|
311
|
+
if (!words.length && !all) return listJobs(peekState(), who.agent ? mineOf : liveOf, who);
|
|
315
312
|
const r = withState(s => {
|
|
316
|
-
|
|
317
|
-
if (
|
|
318
|
-
|
|
319
|
-
if (no) return { refused: no };
|
|
320
|
-
out(naming('stopping', x));
|
|
321
|
-
const c = brief(s, x);
|
|
322
|
-
if (c.kind === 'hold') s.leases = s.leases.filter(l => l !== x);
|
|
323
|
-
else if (c.kind === 'wait' && !x.pid) s.queue = s.queue.filter(q => q !== x); // a kept place: nobody is waiting on it
|
|
313
|
+
let targets;
|
|
314
|
+
if (ids) targets = ids.map(id => jobsOf(s).find(y => String(y.id) === id) || { missing: id });
|
|
315
|
+
else if (all) targets = mineOf(s);
|
|
324
316
|
else {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
if (
|
|
328
|
-
if (
|
|
329
|
-
// Recorded apart from the place, so its waiter can say why once the place is gone. A place
|
|
330
|
-
// its dibs is still deciding for stays until that dibs sees the stop (its waiter says so and
|
|
331
|
-
// leaves; src/state.mjs reap takes it after STOP_GRACE); one nothing decides for goes now.
|
|
332
|
-
// On 2026-11-04 "took #19783 out of line" left #19783 at the head of heavy, twice.
|
|
333
|
-
(s.stopped ||= {})[x.id] = { at: x.ownerStop, by: x.stopBy, reason: x.stopReason };
|
|
334
|
-
if (unpolled(x, now())) s.queue = s.queue.filter(q => q !== x);
|
|
335
|
-
}
|
|
336
|
-
// SIGKILL ends stopped processes too, so a paused run needs no SIGCONT first.
|
|
337
|
-
// The lock is still held, so the run's own dibs can't remove the lease
|
|
338
|
-
// (and its group number can't be handed out again) in between.
|
|
339
|
-
if (c.kind === 'run') for (const g of groupsOf(x)) stopTree(g, 'SIGKILL');
|
|
317
|
+
const want = words.join(' ').toLowerCase();
|
|
318
|
+
targets = mineOf(s).filter(x => `${x.purpose || ''}\n${x.cmd || ''}`.toLowerCase().includes(want));
|
|
319
|
+
if (targets.length > 1) return { many: targets.map(x => ({ ...brief(s, x), x: { ...x } })), want };
|
|
320
|
+
if (!targets.length) return { none: want, theirs: liveOf(s).filter(x => `${x.purpose || ''}\n${x.cmd || ''}`.toLowerCase().includes(want)).map(x => ({ ...brief(s, x), x: { ...x } })) };
|
|
340
321
|
}
|
|
341
|
-
return
|
|
322
|
+
return { done: targets.map(x => (x.missing ? x : stopOne(s, x, who, reason, !own(x, me)))) };
|
|
342
323
|
});
|
|
343
|
-
if (
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
324
|
+
if (r.many) {
|
|
325
|
+
out(`${r.many.length} of your jobs match "${r.want}". Say which by number:`);
|
|
326
|
+
for (const c of r.many) out(jobLine(c));
|
|
327
|
+
return die(`nothing stopped. dibs stop <number> stops one; dibs stop mine stops all of yours.`, 2);
|
|
328
|
+
}
|
|
329
|
+
if (r.none != null) {
|
|
330
|
+
if (!r.theirs.length) return die(`none of your running or waiting jobs match "${r.none}". dibs stop with nothing after it lists yours.`, 1);
|
|
331
|
+
out(`none of your jobs match "${r.none}", but other agents' do:`);
|
|
332
|
+
for (const c of r.theirs) out(jobLine(c));
|
|
333
|
+
return die(`nothing stopped: words only pick from your own jobs. To stop one of these, give its number: dibs stop ${r.theirs[0].id}`, 1);
|
|
334
|
+
}
|
|
335
|
+
if (all && !r.done.length) return out("you have no running or waiting jobs. Nothing to stop.");
|
|
336
|
+
let missed = 0;
|
|
337
|
+
for (const c of r.done) {
|
|
338
|
+
if (c.missing) { missed++; console.error(`dibs: ${endedText(c.missing) || `no running or waiting job #${c.missing}, and none by that number in the history. dibs stop with nothing after it lists yours. ${IDS}`}`); continue; }
|
|
339
|
+
out(naming('stopping', c.x));
|
|
340
|
+
out(c.kind === 'hold' ? `released #${c.id} ${c.who}'s hold on ${c.res.join(',')}. A hold has no process to kill.${c.theirs ? ' Its agent is told.' : ''}`
|
|
341
|
+
: c.kind === 'wait' ? `took #${c.id} ${c.who} out of line for ${c.res.join(',')}. Its command never started.${c.theirs ? ' Its agent is told.' : ''}`
|
|
342
|
+
: `stopped #${c.id} ${c.who}'s ${c.res.join(',')} run and killed its processes. Its dibs tells the agent it was stopped by ${who.agent || who.via ? who.name : 'the owner'}.`);
|
|
343
|
+
if (c.theirs) tellOwner(c, who, reason);
|
|
344
|
+
}
|
|
345
|
+
const done = r.done.filter(c => !c.missing);
|
|
346
|
+
recordAll('stop', done.map(c => ({ id: c.id, res: c.res, who: c.who })), who, reason);
|
|
347
|
+
if (missed) process.exitCode = 1;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Every job a number can name, and the running and waiting ones (a kept place has nobody on it).
|
|
351
|
+
const jobsOf = s => [...s.leases, ...s.queue];
|
|
352
|
+
const liveOf = s => [...s.leases, ...s.queue.filter(q => q.pid)];
|
|
353
|
+
// A session's main agent owns its subagents' jobs too (a stamped subagent, or one --as names, owns its own).
|
|
354
|
+
// The guard stamps the main agent's calls 'main'; without the guard, none.
|
|
355
|
+
const yours = (x, me) => own(x, me) || (isMine(x, me) && (!me.agentId || me.agentId === 'main') && !me.label);
|
|
356
|
+
|
|
357
|
+
// A number that names no job now: how and when it ended, from the history, or null.
|
|
358
|
+
const ENDS = new Set(['end', 'left', 'timeout', 'requeue']);
|
|
359
|
+
function endedText(id) {
|
|
360
|
+
const n = Number(id), t = now();
|
|
361
|
+
const evs = readEvents(t).filter(e => e.id === n);
|
|
362
|
+
const e = evs.filter(x => ENDS.has(x.kind)).pop();
|
|
363
|
+
if (!e) return null;
|
|
364
|
+
const stopped = evs.find(x => x.kind === 'override' && x.act === 'stop');
|
|
365
|
+
const how = e.kind === 'timeout' ? 'it gave up waiting (exit 75) and never ran'
|
|
366
|
+
: e.kind === 'left' ? `it left the line (${e.why}) and never ran`
|
|
367
|
+
: e.kind === 'requeue' ? 'it was stopped and put back in line under a new number (dibs stop lists yours)'
|
|
368
|
+
: e.code == null ? 'it was stopped and put back in line under a new number (dibs stop lists yours)'
|
|
369
|
+
: e.code === 0 ? 'it finished (exit 0)' : `it ended with exit ${e.code}`;
|
|
370
|
+
const whose = `${e.who || 'someone'}'s${e.purpose ? ` "${e.purpose}"` : ''}`;
|
|
371
|
+
return `#${id} (${whose}) already ended ${dur(t - e.at)} ago, at ${clock(e.at)}: ${how}${stopped ? `, after ${stopped.by} stopped it` : ''}. Nothing to stop.`;
|
|
372
|
+
}
|
|
373
|
+
const peekState = () => withState(s => ({ leases: s.leases.map(l => ({ ...l })), queue: s.queue.map(q => ({ ...q })) }));
|
|
374
|
+
const jobLine = c => ` #${c.id} ${c.kind === 'wait' ? 'waiting' : c.kind === 'hold' ? 'holding' : 'running'} ${c.res.join(',')} ${c.who} "${described(c.x)}"${c.x.cmd ? ` (${c.x.cmd.slice(0, 60)})` : ''}`;
|
|
375
|
+
|
|
376
|
+
// Bare dibs stop: what there is to stop, so nobody digs a number out of the board's JSON.
|
|
377
|
+
function listJobs(s, mineOf, who) {
|
|
378
|
+
const mine = mineOf(s).map(x => ({ ...brief(s, x), x }));
|
|
379
|
+
if (!mine.length) return out(`${who.agent ? 'You have' : 'There are'} no running or waiting jobs. Nothing to stop.`);
|
|
380
|
+
out(`${who.agent ? 'Your' : 'Every'} running and waiting job${mine.length > 1 ? (who.agent ? 's' : '') : ''}:`);
|
|
381
|
+
for (const c of mine) out(jobLine(c));
|
|
382
|
+
const words = quoted([described(mine[0].x).split(/\s+/).slice(0, 3).join(' ')]);
|
|
383
|
+
out(`Stop one: dibs stop ${mine[0].id}${who.agent ? `${mine.length > 1 ? '; all of yours: dibs stop mine' : ''}; or by its words: dibs stop ${words}` : ''}`);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// One job stopped under the lock. theirs: another agent's, whose agent is told after.
|
|
387
|
+
function stopOne(s, x, who, reason, theirs) {
|
|
388
|
+
const c = { ...brief(s, x), x: { ...x }, theirs: theirs && (!!x.sessionId || !!x.who) };
|
|
389
|
+
if (c.kind === 'hold') s.leases = s.leases.filter(l => l !== x);
|
|
390
|
+
else if (c.kind === 'wait' && !x.pid) s.queue = s.queue.filter(q => q !== x); // a kept place: nobody is waiting on it
|
|
391
|
+
else {
|
|
392
|
+
x.ownerStop = now();
|
|
393
|
+
if (who.agent || who.via) x.stopBy = who.name;
|
|
394
|
+
if (reason) x.stopReason = reason;
|
|
395
|
+
if (c.kind === 'wait') {
|
|
396
|
+
// Recorded apart from the place, so its waiter can say why once the place is gone. A place
|
|
397
|
+
// its dibs is still deciding for stays until that dibs sees the stop (its waiter says so and
|
|
398
|
+
// leaves; src/state.mjs reap takes it after STOP_GRACE); one nothing decides for goes now.
|
|
399
|
+
// On 2026-11-04 "took #19783 out of line" left #19783 at the head of heavy, twice.
|
|
400
|
+
(s.stopped ||= {})[x.id] = { at: x.ownerStop, by: x.stopBy, reason: x.stopReason };
|
|
401
|
+
if (unpolled(x, now())) s.queue = s.queue.filter(q => q !== x);
|
|
402
|
+
}
|
|
403
|
+
// SIGKILL ends stopped processes too, so a paused run needs no SIGCONT first.
|
|
404
|
+
// The lock is still held, so the run's own dibs can't remove the lease
|
|
405
|
+
// (and its group number can't be handed out again) in between.
|
|
406
|
+
if (c.kind === 'run') for (const g of groupsOf(x)) stopTree(g, 'SIGKILL');
|
|
407
|
+
}
|
|
408
|
+
return c;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// The stopped job's agent hears it beside its next tool call, even when the call that
|
|
412
|
+
// ran the job went to the background, and for a hold, which has no dibs of its own to say it.
|
|
413
|
+
function tellOwner(c, who, reason) {
|
|
414
|
+
const x = c.x;
|
|
415
|
+
const by = who.agent || who.via ? who.name : 'the person at this machine';
|
|
416
|
+
const what = c.kind === 'hold' ? `released your hold #${x.id} on ${x.res.join(',')}` : c.kind === 'wait' ? `took your job #${x.id} out of line` : `stopped your job #${x.id} and killed its processes`;
|
|
417
|
+
notify({ sessionId: x.sessionId, who: x.who, agentId: x.agentId }, `stop:#${x.id}`,
|
|
418
|
+
`${by} ${what} ("${described(x)}") with dibs stop${reason ? `: "${reason}"` : ''}.${c.kind === 'hold' ? '' : ' It did not finish.'} Ask ${who.agent ? by : 'them'} before running it again.`);
|
|
349
419
|
}
|
|
350
420
|
|
|
351
421
|
// Lower a waiter to its agent's cap, or back up toward what it asked for when
|
package/src/owner.mjs
CHANGED
|
@@ -4,21 +4,34 @@
|
|
|
4
4
|
// event log and on the board, so the person can see which agent did what.
|
|
5
5
|
import { identity } from './proc.mjs';
|
|
6
6
|
import { event } from './events.mjs';
|
|
7
|
+
import { readPeople, several, personOf } from './people.mjs';
|
|
7
8
|
|
|
8
9
|
// Set in the environment of an agent's tool calls: Claude Code, Codex, Cursor,
|
|
9
10
|
// Gemini CLI, opencode, and the AI_AGENT convention several of them share.
|
|
10
11
|
export const AGENT_ENV = ['CLAUDECODE', 'CLAUDE_CODE_CHILD_SESSION', 'CLAUDE_CODE_SESSION_ID', 'CLAUDE_CODE_ENTRYPOINT', 'AI_AGENT',
|
|
11
12
|
'CODEX_SANDBOX', 'CODEX_THREAD_ID', 'CURSOR_AGENT', 'GEMINI_CLI', 'OPENCODE'];
|
|
12
13
|
|
|
13
|
-
// The caller: { name, agent, via }. An agent goes by its session name (the one the
|
|
14
|
+
// The caller: { name, agent, via, person, proof }. An agent goes by its session name (the one the
|
|
14
15
|
// board and SendMessage use); anyone else is the person. DIBS_VIA names the paired
|
|
15
16
|
// browser a page action came through (src/web.mjs sets it for relay calls), so the
|
|
16
17
|
// record says "the person via Safari on iPhone (remote ...)". An agent can't borrow it.
|
|
18
|
+
// With two or more people (src/people.mjs), a person goes by their name ("sam via ..."), and
|
|
19
|
+
// person is that name, or '' when dibs can't tell which of them is typing.
|
|
17
20
|
export function actor(env = process.env) {
|
|
18
21
|
const id = identity();
|
|
19
22
|
const agent = AGENT_ENV.some(k => env[k]) || !!id.spid;
|
|
20
23
|
const via = !agent && typeof env.DIBS_VIA === 'string' && env.DIBS_VIA ? env.DIBS_VIA : null;
|
|
21
|
-
|
|
24
|
+
const p = agent ? {} : readPeople();
|
|
25
|
+
const person = several(p) ? personOf({ env, p })?.name || '' : null;
|
|
26
|
+
const as = person ? person : person === '' ? 'a person here' : 'the person';
|
|
27
|
+
return { agent, name: agent ? id.agent || id.who : via ? `${as} via ${via}` : as, ...(via && { via }), ...(person != null && { person }), ...(!agent && proofIn(env)) };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// DIBS_PROOF: the phone's signature on the call that runs this command (src/web.mjs sets it for a
|
|
31
|
+
// call src/relay.mjs checked). It's kept with the message or answer, and checked again where it's
|
|
32
|
+
// read (src/signed.mjs), so anyone can set it and only a real one counts.
|
|
33
|
+
function proofIn(env) {
|
|
34
|
+
try { const x = JSON.parse(env.DIBS_PROOF || 'null'); return x && typeof x === 'object' && !Array.isArray(x) ? { proof: x } : {}; } catch { return {}; }
|
|
22
35
|
}
|
|
23
36
|
|
|
24
37
|
// One line in the event log for a change to the person's settings or the
|
package/src/people.mjs
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// More than one person. Until someone runs dibs people add twice, dibs knows one person, "the
|
|
2
|
+
// person", and nothing here changes a word it says. With two or more in config.json's "people",
|
|
3
|
+
// dibs knows them by name: which agents are whose, whose phone a question buzzes, who answered
|
|
4
|
+
// it and who a message came from.
|
|
5
|
+
//
|
|
6
|
+
// "people": { "sam": { "users": ["sam"], "devices": ["<paired device id>"], "hours": {...}, "notify": "all" } }
|
|
7
|
+
//
|
|
8
|
+
// An agent's owner, first match wins: DIBS_PERSON in its environment (two people on one macOS
|
|
9
|
+
// account each export their own), the project's .dibs.json ("owner": "sam", or "owners":
|
|
10
|
+
// {"ios-app": "sam"} in a folder above several repos), the macOS user running it, and with one
|
|
11
|
+
// person configured, that person. A person typing a command is known by DIBS_PERSON (the page
|
|
12
|
+
// sets it from the paired phone a call came through) or their macOS user; a project's owner says
|
|
13
|
+
// nothing about who's typing.
|
|
14
|
+
//
|
|
15
|
+
// Read without a lock, straight from config.json: actor() calls this on every change it records,
|
|
16
|
+
// and loadConfig() would die on a config another field broke.
|
|
17
|
+
import fs from 'node:fs';
|
|
18
|
+
import os from 'node:os';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { paths } from './config.mjs';
|
|
21
|
+
import { rulesAbove } from './trust.mjs';
|
|
22
|
+
|
|
23
|
+
export const NOTIFY = ['all', 'asks', 'off'];
|
|
24
|
+
// Words that already mean something where a person's name goes (dibs tell, dibs say --to).
|
|
25
|
+
const RESERVED = new Set(['person', 'people', 'everyone', 'all', 'desk', 'me', 'nobody', 'someone']);
|
|
26
|
+
const NAME = /^[A-Za-z][\w.-]{0,31}$/;
|
|
27
|
+
|
|
28
|
+
export function readPeople(file = paths.config) {
|
|
29
|
+
try {
|
|
30
|
+
const p = JSON.parse(fs.readFileSync(file, 'utf8')).people;
|
|
31
|
+
return p && typeof p === 'object' && !Array.isArray(p) ? p : {};
|
|
32
|
+
} catch { return {}; }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Two or more: the only time names show.
|
|
36
|
+
export const several = (p = readPeople()) => Object.keys(p).length > 1;
|
|
37
|
+
export const find = (p, name) => (name ? Object.keys(p).find(k => k.toLowerCase() === String(name).toLowerCase()) || null : null);
|
|
38
|
+
|
|
39
|
+
export function checkName(name, p) {
|
|
40
|
+
if (!NAME.test(name || '')) return `a person's name is a word of letters, digits, dots, dashes and underscores, starting with a letter: dibs people add sam. Not "${name}".`;
|
|
41
|
+
if (RESERVED.has(name.toLowerCase())) return `"${name}" already means something to dibs (dibs say --to person, dibs tell --all). Pick another name.`;
|
|
42
|
+
if (find(p, name)) return `${find(p, name)} is already here. Change them: dibs people set ${find(p, name)} ...`;
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function macUser() {
|
|
47
|
+
try { return os.userInfo().username; } catch { return process.env.USER || null; }
|
|
48
|
+
}
|
|
49
|
+
const byUser = (p, user) => Object.keys(p).find(k => Array.isArray(p[k].users) && p[k].users.includes(user)) || null;
|
|
50
|
+
export const byDevice = (p, did) => (did ? Object.keys(p).find(k => Array.isArray(p[k].devices) && p[k].devices.includes(did)) || null : null);
|
|
51
|
+
const only = p => (Object.keys(p).length === 1 ? Object.keys(p)[0] : null);
|
|
52
|
+
|
|
53
|
+
// The project's named owner for a folder: the nearest .dibs.json that names one. A name nobody
|
|
54
|
+
// here goes by (the repo is shared with people on other machines) is passed over.
|
|
55
|
+
export function projectOwner(cwd, p) {
|
|
56
|
+
for (const f of rulesAbove(cwd)) {
|
|
57
|
+
let r;
|
|
58
|
+
try { r = JSON.parse(fs.readFileSync(f, 'utf8')); } catch { continue; }
|
|
59
|
+
const dir = path.dirname(f);
|
|
60
|
+
if (r.owners && typeof r.owners === 'object') {
|
|
61
|
+
for (const [sub, who] of Object.entries(r.owners)) {
|
|
62
|
+
const root = path.resolve(dir, sub);
|
|
63
|
+
const rel = path.relative(root, path.resolve(cwd));
|
|
64
|
+
if (!rel.startsWith('..') && !path.isAbsolute(rel) && find(p, who)) return { name: find(p, who), why: `${f} names ${who} for ${sub}` };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (typeof r.owner === 'string' && find(p, r.owner)) return { name: find(p, r.owner), why: `${f} names ${r.owner}` };
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Whose agent this is: { name, why }, or null when dibs can't tell.
|
|
73
|
+
export function ownerOf({ cwd = process.cwd(), env = process.env, p = readPeople(), user = macUser() } = {}) {
|
|
74
|
+
const set = find(p, env.DIBS_PERSON);
|
|
75
|
+
if (set) return { name: set, why: `DIBS_PERSON is ${env.DIBS_PERSON}` };
|
|
76
|
+
const proj = projectOwner(cwd, p);
|
|
77
|
+
if (proj) return proj;
|
|
78
|
+
const u = byUser(p, user);
|
|
79
|
+
if (u) return { name: u, why: `macOS user ${user} is ${u}'s` };
|
|
80
|
+
if (only(p)) return { name: only(p), why: 'the only person here' };
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Which person is typing (not an agent): { name, why }, or null.
|
|
85
|
+
export function personOf({ env = process.env, p = readPeople(), user = macUser() } = {}) {
|
|
86
|
+
const set = find(p, env.DIBS_PERSON);
|
|
87
|
+
if (set) return { name: set, why: `DIBS_PERSON is ${env.DIBS_PERSON}` };
|
|
88
|
+
const u = byUser(p, user);
|
|
89
|
+
if (u) return { name: u, why: `macOS user ${user} is ${u}'s` };
|
|
90
|
+
if (only(p)) return { name: only(p), why: 'the only person here' };
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// A person's hours, or everyone's (dibs hours) when they set none: a config-shaped object for offHours().
|
|
95
|
+
export const hoursCfg = (cfg, name, p = readPeople()) => {
|
|
96
|
+
const h = name && p[name]?.hours;
|
|
97
|
+
return h ? { ...cfg, hours: h } : cfg;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// A phone paired for a person (dibs connect --phone --for sam) is theirs from its first connection.
|
|
101
|
+
// Taken from whoever had it; nothing happens if that person was removed meanwhile.
|
|
102
|
+
export function giveDevice(name, did) {
|
|
103
|
+
const p = readPeople();
|
|
104
|
+
const who = find(p, name);
|
|
105
|
+
if (!who) return false;
|
|
106
|
+
for (const k of Object.keys(p)) p[k] = { ...p[k], devices: (p[k].devices || []).filter(d => d !== did) };
|
|
107
|
+
p[who].devices.push(did);
|
|
108
|
+
writePeople(p);
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function writePeople(p) {
|
|
113
|
+
const c = JSON.parse(fs.readFileSync(paths.config, 'utf8'));
|
|
114
|
+
c.people = p;
|
|
115
|
+
const tmp = `${paths.config}.${process.pid}`;
|
|
116
|
+
fs.writeFileSync(tmp, JSON.stringify(c, null, 2) + '\n');
|
|
117
|
+
fs.renameSync(tmp, paths.config);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const USE = 'dibs people [--json] | add <name> | set <name> | remove <name> | me [--json]. add and set take --user <macOS user>, --device <paired device id>, --hours 09:00-18:00|off, --notify all|asks|off; set also --forget-user, --forget-device';
|
|
121
|
+
|
|
122
|
+
function parseFlags(args, die) {
|
|
123
|
+
const o = { users: [], devices: [], forgetUsers: [], forgetDevices: [], words: [] };
|
|
124
|
+
for (let i = 0; i < args.length; i++) {
|
|
125
|
+
const a = args[i], next = () => args[++i] ?? die(`${a} needs a value. ${USE}`);
|
|
126
|
+
if (a === '--user') o.users.push(next());
|
|
127
|
+
else if (a === '--device') o.devices.push(next());
|
|
128
|
+
else if (a === '--forget-user') o.forgetUsers.push(next());
|
|
129
|
+
else if (a === '--forget-device') o.forgetDevices.push(next());
|
|
130
|
+
else if (a === '--hours') o.hours = next();
|
|
131
|
+
else if (a === '--notify') { o.notify = next(); if (!NOTIFY.includes(o.notify)) die(`--notify is all (questions, notes, finished and stuck tasks), asks (only questions that need them) or off, not ${o.notify}`); }
|
|
132
|
+
else if (a === '--json') o.json = true;
|
|
133
|
+
else if (a.startsWith('-')) die(`unknown flag ${a}. ${USE}`);
|
|
134
|
+
else o.words.push(a);
|
|
135
|
+
}
|
|
136
|
+
return o;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const userText = x => (x.users?.length ? `macOS user ${x.users.join(', ')}` : 'no macOS user (DIBS_PERSON or a project\'s .dibs.json only)');
|
|
140
|
+
const notifyText = x => ({ all: 'everything', asks: 'questions only', off: 'no notifications' }[x.notify || 'all']);
|
|
141
|
+
|
|
142
|
+
// dibs people: who's here, and whose agents are whose.
|
|
143
|
+
export async function peopleCmd(args) {
|
|
144
|
+
const { die, say } = await import('./util.mjs');
|
|
145
|
+
const { logChange, actor } = await import('./owner.mjs');
|
|
146
|
+
const [verb, ...rest] = args;
|
|
147
|
+
const o = parseFlags(verb && !verb.startsWith('-') ? rest : args, die);
|
|
148
|
+
let p = readPeople();
|
|
149
|
+
if (!verb || verb.startsWith('-')) return list(p, o, actor);
|
|
150
|
+
if (verb === 'me') return me(p, o, actor);
|
|
151
|
+
const name = o.words[0] || die(`which person? ${USE}`);
|
|
152
|
+
if (o.words.length > 1) die(`one name at a time, not ${o.words.join(' ')}. ${USE}`);
|
|
153
|
+
if (verb === 'add') {
|
|
154
|
+
const bad = checkName(name, p);
|
|
155
|
+
if (bad) die(bad);
|
|
156
|
+
// The first person is usually whoever is setting dibs up, at their own Mac.
|
|
157
|
+
const users = o.users.length || Object.keys(p).length ? o.users : [macUser()].filter(Boolean);
|
|
158
|
+
p = { ...p, [name]: { users: [], devices: [], hours: null, notify: 'all', added: Date.now() } };
|
|
159
|
+
await change(p, name, { ...o, users }, die);
|
|
160
|
+
writePeople(p);
|
|
161
|
+
logChange('people add', { person: name });
|
|
162
|
+
console.log(`Added ${name}: ${userText(p[name])}${p[name].devices.length ? `, ${p[name].devices.length} paired device${p[name].devices.length > 1 ? 's' : ''}` : ''}.`);
|
|
163
|
+
if (Object.keys(p).length === 2) say('two people now: dibs names who asked, answered and sent each message, and sends each question to its agent\'s owner. dibs people lists them.');
|
|
164
|
+
else if (Object.keys(p).length === 1) say('one person: dibs still says "the person" everywhere. A second (dibs people add <name>) is when names start to show.');
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const who = find(p, name) || die(`nobody called ${name} here. ${Object.keys(p).length ? `People: ${Object.keys(p).join(', ')}` : 'Add them: dibs people add ' + name}`, 1);
|
|
168
|
+
if (verb === 'remove') {
|
|
169
|
+
const { [who]: gone, ...left } = p;
|
|
170
|
+
writePeople(left);
|
|
171
|
+
logChange('people remove', { person: who });
|
|
172
|
+
return console.log(`Removed ${who}. Their agents' questions go to ${Object.keys(left).length === 1 ? Object.keys(left)[0] : 'whoever answers'} now.`);
|
|
173
|
+
}
|
|
174
|
+
if (verb === 'set') {
|
|
175
|
+
p = { ...p, [who]: { ...p[who] } };
|
|
176
|
+
await change(p, who, o, die);
|
|
177
|
+
writePeople(p);
|
|
178
|
+
logChange('people set', { person: who });
|
|
179
|
+
return list(p, { json: false }, actor, who);
|
|
180
|
+
}
|
|
181
|
+
die(`unknown ${verb}. ${USE}`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Apply add's and set's flags to p[name], in place.
|
|
185
|
+
async function change(p, name, o, die) {
|
|
186
|
+
const x = p[name];
|
|
187
|
+
x.users = [...new Set([...(x.users || []).filter(u => !o.forgetUsers.includes(u)), ...o.users])];
|
|
188
|
+
for (const u of o.users) {
|
|
189
|
+
const other = byUser(p, u);
|
|
190
|
+
if (other && other !== name) die(`macOS user ${u} is ${other}'s already: dibs people set ${other} --forget-user ${u}, then try again. Nothing was changed.`);
|
|
191
|
+
}
|
|
192
|
+
if (o.devices.length || o.forgetDevices.length) {
|
|
193
|
+
const { devices } = await import('./relay.mjs');
|
|
194
|
+
const all = devices();
|
|
195
|
+
const pick = id => {
|
|
196
|
+
const hits = all.filter(d => d.id.startsWith(id));
|
|
197
|
+
if (hits.length !== 1) die(`${hits.length ? `${hits.length} paired devices start with ${id}` : `no paired device starts with ${id}`}. Their ids: dibs connect --list. Nothing was changed.`);
|
|
198
|
+
return hits[0].id;
|
|
199
|
+
};
|
|
200
|
+
const add = o.devices.map(pick), forget = new Set(o.forgetDevices.map(id => (x.devices || []).find(d => d.startsWith(id)) || pick(id)));
|
|
201
|
+
// A phone is one person's: giving it to someone takes it from whoever had it.
|
|
202
|
+
for (const k of Object.keys(p)) if (k !== name && p[k].devices?.some(d => add.includes(d))) p[k] = { ...p[k], devices: p[k].devices.filter(d => !add.includes(d)) };
|
|
203
|
+
x.devices = [...new Set([...(x.devices || []).filter(d => !forget.has(d)), ...add])];
|
|
204
|
+
}
|
|
205
|
+
if (o.hours != null) {
|
|
206
|
+
const { parseHours } = await import('./questions.mjs');
|
|
207
|
+
const h = parseHours(o.hours);
|
|
208
|
+
if (h == null) die(`can't read hours "${o.hours}". Use --hours 09:00-18:00, or --hours off for everyone's (dibs hours)`);
|
|
209
|
+
x.hours = h || null;
|
|
210
|
+
}
|
|
211
|
+
if (o.notify) x.notify = o.notify;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function list(p, o, actor, only = null) {
|
|
215
|
+
const names = only ? [only] : Object.keys(p);
|
|
216
|
+
const you = actor();
|
|
217
|
+
if (o.json) return console.log(JSON.stringify({ people: names.map(n => ({ name: n, users: p[n].users || [], devices: p[n].devices || [], hours: p[n].hours || null, notify: p[n].notify || 'all' })), you: you.agent ? null : you.person ?? personOf({ p })?.name ?? null }));
|
|
218
|
+
if (!names.length) return console.log('Nobody is named here, so dibs speaks of "the person". Two people sharing this Mac: dibs people add <name> for each.');
|
|
219
|
+
if (!only) console.log(names.length === 1 ? `1 person, ${names[0]}: dibs still says "the person" everywhere until there are two.` : `${names.length} people. Each agent's questions go to its owner, and messages say who sent them.`);
|
|
220
|
+
for (const n of names) {
|
|
221
|
+
const x = p[n];
|
|
222
|
+
const hours = x.hours ? `hours ${x.hours.start} to ${x.hours.end}` : 'everyone\'s hours';
|
|
223
|
+
const ds = x.devices || [];
|
|
224
|
+
const devices = ds.length ? `paired device${ds.length > 1 ? 's' : ''} ${ds.map(d => d.slice(0, 8)).join(', ')}` : 'no paired device';
|
|
225
|
+
console.log(` ${n.padEnd(12)} ${userText(x)}, ${devices}, ${hours}, ${notifyText(x)}`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function me(p, o, actor) {
|
|
230
|
+
const a = actor();
|
|
231
|
+
const r = a.agent ? ownerOf({ p }) : personOf({ p });
|
|
232
|
+
if (o.json) return console.log(JSON.stringify({ agent: a.agent, person: r?.name || null, why: r?.why || null, several: several(p) }));
|
|
233
|
+
if (!Object.keys(p).length) return console.log(a.agent ? 'You work for the person: nobody else is named here.' : 'You\'re the person: nobody else is named here.');
|
|
234
|
+
if (!r) return console.log(`dibs can't tell ${a.agent ? 'whose agent you are' : 'who you are'}. ${a.agent ? 'Your questions reach everyone.' : ''} Set DIBS_PERSON=<name>, or dibs people set <name> --user ${macUser()}. People: ${Object.keys(p).join(', ')}`);
|
|
235
|
+
console.log(a.agent ? `You work for ${r.name} (${r.why}).` : `You're ${r.name} (${r.why}).`);
|
|
236
|
+
}
|