@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.
- package/CHANGELOG.md +103 -1
- package/README.md +10 -8
- package/bin/dibs.mjs +370 -73
- 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 +120 -9
- 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/package.json +1 -1
- package/skills/dibs/SKILL.md +79 -149
- package/src/about.mjs +341 -0
- package/src/acquire.mjs +111 -27
- package/src/after.mjs +127 -0
- package/src/agents.mjs +2 -1
- package/src/alerts.mjs +95 -42
- 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 +243 -82
- package/src/config.mjs +85 -14
- package/src/connect.mjs +4 -2
- package/src/cool.mjs +27 -11
- 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 +118 -6
- package/src/guard.mjs +239 -86
- 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 +33 -9
- package/src/measure.mjs +42 -11
- 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 +19 -0
- package/src/preempt.mjs +123 -15
- package/src/primer.mjs +91 -20
- package/src/prints.mjs +99 -0
- package/src/proc.mjs +1 -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 +139 -20
- 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/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 +1078 -133
- 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 +353 -60
- package/src/work.mjs +487 -0
- package/src/worker.mjs +7 -1
package/src/thin.sh
CHANGED
|
@@ -49,6 +49,8 @@ trap 'leave SIGTERM 143' TERM
|
|
|
49
49
|
trap 'leave SIGHUP 129' HUP
|
|
50
50
|
|
|
51
51
|
$N "$B" _enqueue $$ "$PPID" "$F" "$@" || { code=$?; trap - INT TERM HUP; [ $code = 70 ] && note 'getting in line crashed'; gone; exit $code; }
|
|
52
|
+
# The wait its first Node began is the broker's now (src/broker.mjs inlineCall); the command never sees it.
|
|
53
|
+
unset DIBS_THIN_WAIT
|
|
52
54
|
IFS= read -r r <&3
|
|
53
55
|
# From here on only the broker writes, so end of file means it isn't here.
|
|
54
56
|
exec 4>&-
|
package/src/trail.mjs
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
// The trail: what each agent got done, kept by dibs itself instead of by the agent remembering to
|
|
2
|
+
// post. A commit (with its subject), a push, a merge into main, a tag, a GitHub release or pull
|
|
3
|
+
// request, a build uploaded to App Store Connect, a publish: the guard hook notes each attempt
|
|
4
|
+
// (src/trailnote.mjs) and this checks afterwards that it happened, in the repo's reflogs or in the
|
|
5
|
+
// agent's own transcript. Test suites and deploys come from dibs run's own record of each run's end
|
|
6
|
+
// (src/events.mjs), with the -m purpose and the exit code. An attempt nothing confirms is left out:
|
|
7
|
+
// the trail says only what dibs can show happened.
|
|
8
|
+
//
|
|
9
|
+
// Read without any lock. Every source is appended to or read whole. Items carry auto: true, so no
|
|
10
|
+
// screen passes them off as the agent's own words.
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { TRAIL, TRAIL_OLDER } from './trailnote.mjs';
|
|
14
|
+
import { EVENTS, OLDER } from './events.mjs';
|
|
15
|
+
import { now, masked, die, dur, clock, deadline } from './util.mjs';
|
|
16
|
+
import { bold, dim, red } from './style.mjs';
|
|
17
|
+
|
|
18
|
+
// How long after an attempt its result may land in the reflog or the transcript: a commit's own
|
|
19
|
+
// hooks can run a test suite first. Past this, an attempt with no evidence is taken as failed.
|
|
20
|
+
const WINDOW = 15 * 6e4;
|
|
21
|
+
// Of a transcript, only what came after the attempt is read, and at most this much of it.
|
|
22
|
+
const TX_READ = 8 * 1024 ** 2;
|
|
23
|
+
// Of a reflog, only the end: a year of commits is a few hundred KB, and the trail looks at a day.
|
|
24
|
+
const REFLOG_TAIL = 256 * 1024;
|
|
25
|
+
// Of the event log, the end: runs that ended in the last day on a busy Mac fit in a few MB.
|
|
26
|
+
const EVENTS_TAIL = 4 * 1024 ** 2;
|
|
27
|
+
// The branches a merge into counts as shipping.
|
|
28
|
+
const MAINS = new Set(['main', 'master', 'trunk', 'production', 'release']);
|
|
29
|
+
const VERSION = /^v?\d+\.\d+/;
|
|
30
|
+
|
|
31
|
+
const cut = (s, n = 90) => { const x = masked(String(s ?? '')).replace(/\s+/g, ' ').trim(); return x.length > n ? `${x.slice(0, n - 3)}...` : x; };
|
|
32
|
+
const readJsonl = text => text.split('\n').flatMap(l => { if (!l) return []; try { return [JSON.parse(l)]; } catch { return []; } });
|
|
33
|
+
|
|
34
|
+
// Attempts noted since then, oldest first. A torn line is skipped.
|
|
35
|
+
export function readNotes(since = 0) {
|
|
36
|
+
let raw = '';
|
|
37
|
+
for (const f of [TRAIL_OLDER, TRAIL]) { try { raw += fs.readFileSync(f, 'utf8'); } catch {} }
|
|
38
|
+
return readJsonl(raw).filter(n => typeof n.at === 'number' && n.at > since && typeof n.kind === 'string' && typeof n.sid === 'string');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// The last bytes of a file, from its first whole line.
|
|
42
|
+
function tail(file, max) {
|
|
43
|
+
try {
|
|
44
|
+
const size = fs.statSync(file).size, from = Math.max(0, size - max);
|
|
45
|
+
const fd = fs.openSync(file, 'r');
|
|
46
|
+
try {
|
|
47
|
+
const buf = Buffer.alloc(size - from);
|
|
48
|
+
fs.readSync(fd, buf, 0, buf.length, from);
|
|
49
|
+
const text = buf.toString('utf8');
|
|
50
|
+
return from > 0 ? text.slice(text.indexOf('\n') + 1) : text;
|
|
51
|
+
} finally { fs.closeSync(fd); }
|
|
52
|
+
} catch { return ''; }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Runs that ended since then, from the end of the event log.
|
|
56
|
+
export function recentEnds(since) {
|
|
57
|
+
return readJsonl(tail(EVENTS, EVENTS_TAIL)).filter(e => e.kind === 'end' && e.at > since);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// A reflog's entries: { old, sha, at (ms), msg }. Git writes seconds.
|
|
61
|
+
function reflog(file) {
|
|
62
|
+
const out = [];
|
|
63
|
+
for (const l of tail(file, REFLOG_TAIL).split('\n')) {
|
|
64
|
+
const m = l.match(/^([0-9a-f]+) ([0-9a-f]+) .*? (\d+) [+-]\d{4}\t(.*)$/);
|
|
65
|
+
if (m) out.push({ old: m[1], sha: m[2], at: Number(m[3]) * 1000, msg: m[4] });
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// This call's result in the agent's transcript: { ok, at }, or null while it isn't there.
|
|
71
|
+
// Claude Code writes a tool_result with is_error when a Bash command exits non-zero.
|
|
72
|
+
function transcript(n) {
|
|
73
|
+
if (!n.tx || !n.tool) return null;
|
|
74
|
+
let text = '';
|
|
75
|
+
try {
|
|
76
|
+
const fd = fs.openSync(n.tx, 'r');
|
|
77
|
+
try {
|
|
78
|
+
const size = fs.fstatSync(fd).size, from = Math.min(n.txAt || 0, size);
|
|
79
|
+
const buf = Buffer.alloc(Math.min(size - from, TX_READ));
|
|
80
|
+
fs.readSync(fd, buf, 0, buf.length, from);
|
|
81
|
+
text = buf.toString('utf8');
|
|
82
|
+
} finally { fs.closeSync(fd); }
|
|
83
|
+
} catch { return null; }
|
|
84
|
+
const key = `"tool_use_id":"${n.tool}"`;
|
|
85
|
+
for (let i = text.indexOf(key); i >= 0; i = text.indexOf(key, i + 1)) {
|
|
86
|
+
const line = text.slice(text.lastIndexOf('\n', i) + 1, (text.indexOf('\n', i) + 1 || text.length + 1) - 1);
|
|
87
|
+
let j;
|
|
88
|
+
try { j = JSON.parse(line); } catch { continue; }
|
|
89
|
+
const r = (Array.isArray(j?.message?.content) ? j.message.content : []).find(c => c?.type === 'tool_result' && c.tool_use_id === n.tool);
|
|
90
|
+
if (r) return { ok: r.is_error !== true, at: Date.parse(j.timestamp) || null };
|
|
91
|
+
}
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function tagExists(cd, name) {
|
|
96
|
+
if (fs.existsSync(path.join(cd, 'refs', 'tags', name))) return true;
|
|
97
|
+
try { return fs.readFileSync(path.join(cd, 'packed-refs'), 'utf8').split('\n').some(l => l.endsWith(` refs/tags/${name}`)); } catch { return false; }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const repoName = n => path.basename(n.dir || '') || null;
|
|
101
|
+
|
|
102
|
+
// The reflog entries this attempt made: from the one that moved HEAD off where it stood when the
|
|
103
|
+
// hook saw the command (the same second can hold someone else's commit just before), each with the
|
|
104
|
+
// branch it landed on, following any checkout the command made on the way.
|
|
105
|
+
function afterHead(entries, n) {
|
|
106
|
+
const from = n.head ? entries.findIndex(e => e.old === n.head) : 0;
|
|
107
|
+
if (from < 0) return [];
|
|
108
|
+
let branch = n.branch || null;
|
|
109
|
+
return entries.slice(from).map(e => {
|
|
110
|
+
const to = e.msg.match(/^checkout: moving from .+ to (.+)$/)?.[1];
|
|
111
|
+
if (to) branch = /^[0-9a-f]{7,}$/.test(to) ? null : to;
|
|
112
|
+
return { ...e, branch };
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// What one attempt came to: { items, final }. final: nothing later can change it, so it's kept.
|
|
117
|
+
// until: when the next attempt in the same checkout began, which ends this one's window.
|
|
118
|
+
export function settle(n, t = now(), until = Infinity) {
|
|
119
|
+
const r = transcript(n);
|
|
120
|
+
if (r?.at && r.at < n.at - 1000) return { items: [], final: true }; // a result from before the attempt is someone else's
|
|
121
|
+
const end = Math.min(r?.at ? r.at + 1000 : n.at + WINDOW, until, t);
|
|
122
|
+
const over = !!r || t > n.at + WINDOW;
|
|
123
|
+
const inWindow = e => e.at >= Math.floor(n.at / 1000) * 1000 - 1000 && e.at <= end;
|
|
124
|
+
const who = { at: n.at, who: n.who || 'an agent', session: n.sid, ...(n.agentId && { agentId: n.agentId }), ...(n.label && { label: n.label }),
|
|
125
|
+
...(repoName(n) && { repo: repoName(n) }), ...(n.branch && { branch: n.branch }), auto: true };
|
|
126
|
+
const id = `${n.sid}:${n.tool || n.at}`;
|
|
127
|
+
const item = (kind, text, more = {}) => ({ id: `${id}:${kind}${more.sha ? `:${more.sha}` : ''}`, ...who, kind, ok: true, text, ...more });
|
|
128
|
+
const head = n.gd ? afterHead(reflog(path.join(n.gd, 'logs', 'HEAD')).filter(inWindow), n) : [];
|
|
129
|
+
if (n.kind === 'commit') {
|
|
130
|
+
const commits = head.filter(e => /^commit( \([\w ]+\))?: /.test(e.msg));
|
|
131
|
+
return { final: over, items: commits.map(e => item('commit', `committed "${cut(e.msg.replace(/^commit[^:]*: /, ''), 80)}"${e.branch ? ` to ${e.branch}` : ''}`,
|
|
132
|
+
{ at: Math.max(e.at, n.at), sha: e.sha.slice(0, 7), ...(e.branch && { branch: e.branch }) })) };
|
|
133
|
+
}
|
|
134
|
+
if (n.kind === 'merge') {
|
|
135
|
+
const m = head.find(e => /^(merge |commit \(merge\): )/.test(e.msg));
|
|
136
|
+
if (!m) return { final: over, items: [] };
|
|
137
|
+
const from = n.from || m.msg.match(/^merge ([^:]+):/)?.[1] || 'a branch';
|
|
138
|
+
const into = m.branch || 'its branch';
|
|
139
|
+
return { final: over, items: [item('merge', `merged ${cut(from, 40)} into ${into}`, { at: Math.max(m.at, n.at), sha: m.sha.slice(0, 7), ...(m.branch && { branch: m.branch }), ...(MAINS.has(into) && { ships: true }) })] };
|
|
140
|
+
}
|
|
141
|
+
if (n.kind === 'push') {
|
|
142
|
+
const refs = n.refs?.length ? n.refs.map(x => x.replace(/^\+/, '').split(':').pop().replace(/^refs\/(heads|tags)\//, '')) : n.branch ? [n.branch] : [];
|
|
143
|
+
const remote = n.remote || 'origin';
|
|
144
|
+
let ok = r ? r.ok : null;
|
|
145
|
+
// No transcript (another agent program): the remote branch's own reflog says a push landed.
|
|
146
|
+
if (ok == null && n.cd && refs.length) ok = refs.some(b => reflog(path.join(n.cd, 'logs', 'refs', 'remotes', remote, b)).some(e => inWindow(e) && /^update by push/.test(e.msg))) || null;
|
|
147
|
+
if (!ok) return { final: over, items: [] };
|
|
148
|
+
const what = n.tags ? 'its tags' : refs.length ? refs.map(x => cut(x, 40)).join(', ') : 'its branch';
|
|
149
|
+
return { final: true, items: [item('push', `pushed ${what} to ${cut(remote, 30)}`, { ...(refs.some(x => MAINS.has(x)) && { main: true }) })] };
|
|
150
|
+
}
|
|
151
|
+
if (n.kind === 'tag') {
|
|
152
|
+
// The tag itself is the evidence: a command that made it and then failed (the push after it) still made it.
|
|
153
|
+
if (n.had || !n.cd || !tagExists(n.cd, n.name)) return { final: over || !!n.had, items: [] };
|
|
154
|
+
return { final: true, items: [item('tag', `tagged ${cut(n.name, 40)}`, VERSION.test(n.name) ? { ships: true } : {})] };
|
|
155
|
+
}
|
|
156
|
+
// The rest leave no trace on this Mac: only the transcript says whether they worked.
|
|
157
|
+
if (!r) return { final: over, items: [] };
|
|
158
|
+
const sentence = {
|
|
159
|
+
release: ok => (ok ? `published release ${cut(n.name || '', 40)} on GitHub` : `tried to publish release ${cut(n.name || '', 40)} on GitHub, and it failed`),
|
|
160
|
+
pr: ok => (ok ? `opened a pull request${n.branch ? ` from ${n.branch}` : ''}` : null),
|
|
161
|
+
prmerge: ok => (ok ? `merged pull request ${cut(n.name || '', 20)}`.trim() : null),
|
|
162
|
+
upload: ok => (ok ? 'uploaded a build to App Store Connect' : 'tried to upload a build to App Store Connect, and it failed'),
|
|
163
|
+
publish: ok => (ok ? 'published a package' : 'tried to publish a package, and it failed'),
|
|
164
|
+
// A command the guard let through only because the person said yes (src/doors.mjs).
|
|
165
|
+
door: ok => `${ok ? 'ran' : 'tried, and failed at,'} ${cut(n.what || n.door || 'a one-way command', 80)} under ${n.yesBy || 'the person'}'s yes (${n.yes})`,
|
|
166
|
+
}[n.kind];
|
|
167
|
+
const text = sentence?.(r.ok);
|
|
168
|
+
if (!text) return { final: true, items: [] };
|
|
169
|
+
const ships = r.ok && ['release', 'upload', 'publish'].includes(n.kind);
|
|
170
|
+
return { final: true, items: [{ ...item(n.kind, text, ships ? { ships: true } : {}), ok: r.ok, at: r.at || n.at }] };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// A run's end, as a trail item: test suites and deploys only. Builds and simulator runs are the
|
|
174
|
+
// steps on the way, not news. A stopped run (128 and up) was stopped, not failed.
|
|
175
|
+
const label = e => {
|
|
176
|
+
const p = typeof e.purpose === 'string' ? e.purpose.replace(/^guard: /, '') : '';
|
|
177
|
+
return cut(p || e.cmd || (e.res || []).join(','), 80);
|
|
178
|
+
};
|
|
179
|
+
export function runItem(e) {
|
|
180
|
+
if (e.kind !== 'end' || e.type !== 'run' || e.code == null || e.code >= 128 || !Array.isArray(e.res)) return null;
|
|
181
|
+
const kind = e.res.some(r => r === 'test' || r === 'playwright' || r.startsWith('playwright:')) ? 'test' : e.res.some(r => r === 'deploy' || r.startsWith('deploy:')) ? 'deploy' : null;
|
|
182
|
+
if (!kind) return null;
|
|
183
|
+
const ok = e.code === 0;
|
|
184
|
+
const what = label(e);
|
|
185
|
+
const text = kind === 'test' ? (ok ? `tests passed: ${what}` : `tests failed (exit ${e.code}): ${what}`) : (ok ? `deployed: ${what}` : `deploy failed (exit ${e.code}): ${what}`);
|
|
186
|
+
return { id: `run:${e.id}:${e.at}`, at: e.at, who: e.who, ...(e.label && { label: e.label }), kind, ok, code: e.code, key: e.purpose ? `-m ${e.purpose}` : e.key || what, text, auto: true };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// The same test run (the same -m, or the same command), over and over, is one line: how it ended, and how often it failed on the way.
|
|
190
|
+
// Only when nothing else of that agent's came between: a failure, a commit, a pass is the story.
|
|
191
|
+
export function story(items) {
|
|
192
|
+
const out = [];
|
|
193
|
+
const lastOf = new Map();
|
|
194
|
+
for (const x of [...items].sort((a, b) => a.at - b.at)) {
|
|
195
|
+
const prev = lastOf.get(x.who);
|
|
196
|
+
if (prev && prev.kind === x.kind && x.kind === 'test' && prev.key === x.key) {
|
|
197
|
+
const fails = (prev.fails || 0) + (prev.ok ? 0 : 1);
|
|
198
|
+
const merged = { ...x, runs: (prev.runs || 1) + 1, ...(fails && { fails }) };
|
|
199
|
+
if (x.ok && fails) merged.text = `${x.text} (after ${fails} failed ${fails === 1 ? 'run' : 'runs'})`;
|
|
200
|
+
out[out.indexOf(prev)] = merged;
|
|
201
|
+
lastOf.set(x.who, merged);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
out.push(x);
|
|
205
|
+
lastOf.set(x.who, x);
|
|
206
|
+
}
|
|
207
|
+
return out;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Results that can't change any more, by attempt, so a page asking every few seconds reads each
|
|
211
|
+
// reflog and transcript only until its attempt is settled.
|
|
212
|
+
const settled = new Map();
|
|
213
|
+
const SETTLED_MAX = 5000;
|
|
214
|
+
|
|
215
|
+
// The trail since then, oldest first. events: run ends, when the caller has them already.
|
|
216
|
+
export function trailNow({ since, t = now(), events = null } = {}) {
|
|
217
|
+
const notes = readNotes(since - WINDOW);
|
|
218
|
+
const items = [];
|
|
219
|
+
// An attempt's window ends where the next one in the same checkout begins.
|
|
220
|
+
const next = new Map();
|
|
221
|
+
for (let i = notes.length - 1; i >= 0; i--) {
|
|
222
|
+
const n = notes[i];
|
|
223
|
+
n.until = next.get(n.gd || n.dir) ?? Infinity;
|
|
224
|
+
if (['commit', 'merge'].includes(n.kind)) next.set(n.gd || n.dir, n.at);
|
|
225
|
+
}
|
|
226
|
+
for (const n of notes) {
|
|
227
|
+
const key = `${n.sid}|${n.tool || ''}|${n.at}|${n.kind}|${n.name || ''}`;
|
|
228
|
+
let r = settled.get(key);
|
|
229
|
+
if (!r) {
|
|
230
|
+
try { r = settle(n, t, n.until); } catch { r = { items: [], final: false }; }
|
|
231
|
+
if (r.final) { settled.set(key, r); if (settled.size > SETTLED_MAX) settled.delete(settled.keys().next().value); }
|
|
232
|
+
}
|
|
233
|
+
items.push(...r.items);
|
|
234
|
+
}
|
|
235
|
+
// Git keeps whole seconds, so a commit can fall in the window of the attempt just before it too
|
|
236
|
+
// (one that failed, or merged nothing): it's the latest attempt's.
|
|
237
|
+
const bySha = new Map();
|
|
238
|
+
for (const x of items) if (x.sha) bySha.set(`${x.kind}:${x.sha}`, x);
|
|
239
|
+
const runs = (events || recentEnds(since)).map(runItem).filter(Boolean);
|
|
240
|
+
const all = [...items.filter(x => !x.sha || bySha.get(`${x.kind}:${x.sha}`) === x), ...runs];
|
|
241
|
+
return story(all.filter(x => x.at > since && x.at <= t));
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Per agent, what the trail says, as one line: the big things first, then how many commits.
|
|
245
|
+
export function summary(items) {
|
|
246
|
+
const by = new Map();
|
|
247
|
+
for (const x of items) by.set(x.who, [...(by.get(x.who) || []), x]);
|
|
248
|
+
return [...by].map(([who, xs]) => {
|
|
249
|
+
const commits = xs.filter(x => x.kind === 'commit');
|
|
250
|
+
const rest = xs.filter(x => x.kind !== 'commit' && x.kind !== 'push');
|
|
251
|
+
const pushes = xs.filter(x => x.kind === 'push');
|
|
252
|
+
const parts = [...rest.filter(x => x.ships || !x.ok), ...rest.filter(x => !x.ships && x.ok)].map(x => x.text);
|
|
253
|
+
if (commits.length === 1) parts.push(commits[0].text);
|
|
254
|
+
else if (commits.length) parts.push(`${commits.length} commits, the last "${commits.at(-1).text.match(/"(.*)"/)?.[1] || ''}"`);
|
|
255
|
+
if (pushes.length) parts.push(pushes.at(-1).text);
|
|
256
|
+
return { who, at: Math.max(...xs.map(x => x.at)), text: parts.join('; '), n: xs.length };
|
|
257
|
+
}).sort((a, b) => b.at - a.at);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// dibs trail [--since 2h|09:00] [--agent name] [--json]
|
|
261
|
+
export function trail(args) {
|
|
262
|
+
let since = now() - 864e5 / 2, json = false, agent = null;
|
|
263
|
+
for (let i = 0; i < args.length; i++) {
|
|
264
|
+
const a = args[i];
|
|
265
|
+
if (a === '--json') json = true;
|
|
266
|
+
else if (a === '--agent') agent = args[++i] ?? die('--agent needs a name, as on the board');
|
|
267
|
+
else if (a === '--since') {
|
|
268
|
+
const v = args[++i] ?? die('--since needs a value: how long ago (2h, 30m) or a clock time (09:00)');
|
|
269
|
+
const m = String(v).match(/^(\d+(?:\.\d+)?)([mhd])$/);
|
|
270
|
+
if (m) since = now() - parseFloat(m[1]) * { m: 6e4, h: 36e5, d: 864e5 }[m[2]];
|
|
271
|
+
else {
|
|
272
|
+
const at = deadline(v);
|
|
273
|
+
if (!at) die(`--since takes how long ago (2h, 30m) or a clock time (09:00), not ${v}`);
|
|
274
|
+
since = at > now() ? at - 864e5 : at;
|
|
275
|
+
}
|
|
276
|
+
} else die(`dibs trail takes --since, --agent and --json, not ${a}`);
|
|
277
|
+
}
|
|
278
|
+
const t = now();
|
|
279
|
+
const items = trailNow({ since, t }).filter(x => !agent || x.who === agent);
|
|
280
|
+
if (json) return console.log(JSON.stringify(items));
|
|
281
|
+
if (!items.length) return console.log(`dibs saw no commits, pushes, merges, releases, uploads, test suites or deploys${agent ? ` from ${agent}` : ''} since ${clock(since)}.`);
|
|
282
|
+
console.log(bold(`What dibs saw agents do since ${clock(since)} (${dur(t - since)} ago), from git, their transcripts and dibs run:`));
|
|
283
|
+
for (const { who } of summary(items)) {
|
|
284
|
+
console.log(`\n ${bold(who)}`);
|
|
285
|
+
for (const x of items.filter(y => y.who === who)) {
|
|
286
|
+
const line = `${x.text}${x.repo ? dim(` in ${x.repo}`) : ''}${x.label ? dim(` (${x.label})`) : ''}`;
|
|
287
|
+
console.log(` ${dim(clock(x.at))} ${x.ok ? line : red(line)}`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// The hook's half of the trail (src/trail.mjs). When an agent's Bash call looks like a milestone (a
|
|
2
|
+
// commit, a push, a merge, a tag, a GitHub release or pull request, an upload to App Store Connect,
|
|
3
|
+
// an npm publish), one line goes into trail.jsonl: who tried what, where, and where to look later to
|
|
4
|
+
// see whether it worked. The hook runs before the command, so it can't know that; src/trail.mjs
|
|
5
|
+
// checks afterwards, in the repo's reflogs and in the agent's own transcript.
|
|
6
|
+
//
|
|
7
|
+
// src/guard.mjs loads this only for a command its TRAIL_HINT matches, so every other Bash call pays
|
|
8
|
+
// for one regex. Appends only, no lock: a line this short goes down whole. Never the command itself
|
|
9
|
+
// (it can hold secrets): only the words that name what it did.
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import os from 'node:os';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
|
|
14
|
+
// The same folder as config.mjs's HOME, worked out here because the hook loads no other module.
|
|
15
|
+
const DIBS_HOME = process.env.DIBS_HOME || path.join(os.homedir(), '.dibs');
|
|
16
|
+
export const TRAIL = path.join(DIBS_HOME, 'trail.jsonl');
|
|
17
|
+
export const TRAIL_OLDER = path.join(DIBS_HOME, 'trail.1.jsonl');
|
|
18
|
+
const SESSIONS = process.env.DIBS_SESSIONS || path.join(os.homedir(), '.claude', 'sessions');
|
|
19
|
+
// About 3,000 notes a file, two files kept: weeks of milestones, and reading stays quick.
|
|
20
|
+
const MAX_FILE = 1024 ** 2;
|
|
21
|
+
const WORD_MAX = 80;
|
|
22
|
+
|
|
23
|
+
const base = s => s.slice(s.lastIndexOf('/') + 1);
|
|
24
|
+
const ASSIGN = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
25
|
+
// Words that run the command after them, which is the one that counts.
|
|
26
|
+
const LEAD = new Set(['env', 'sudo', 'nice', 'nohup', 'time', 'command', 'exec', 'caffeinate', 'then', 'do', 'if', 'elif', 'else', '!']);
|
|
27
|
+
const word = s => String(s).replace(/[\u0000-\u001f\u007f]/g, ' ').slice(0, WORD_MAX);
|
|
28
|
+
// A plist that sends the exported build to App Store Connect instead of leaving it on disk.
|
|
29
|
+
const UPLOADS = /<key>destination<\/key>\s*<string>upload<\/string>/;
|
|
30
|
+
|
|
31
|
+
// The words that matter in one simple command, after what only runs it.
|
|
32
|
+
function argvOf(seg) {
|
|
33
|
+
const t = seg.filter(w => !w.redir).map(w => w.text);
|
|
34
|
+
let i = 0;
|
|
35
|
+
while (i < t.length && (ASSIGN.test(t[i]) || LEAD.has(t[i]) || t[i].startsWith('-') && i > 0 && LEAD.has(t[i - 1]))) i++;
|
|
36
|
+
let argv = t.slice(i);
|
|
37
|
+
if (base(argv[0] || '') === 'xcrun') argv = argv.slice(1);
|
|
38
|
+
// dibs run <res> ... -- cmd: what it runs. dibs's own record covers the run; an upload inside
|
|
39
|
+
// one is still noted, since only the transcript says it reached App Store Connect.
|
|
40
|
+
if (['dibs', 'dibs.mjs'].includes(base(argv[0] || '')) && argv[1] === 'run') argv = argv.slice(argv.indexOf('--') + 1 || argv.length);
|
|
41
|
+
return argv;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const nonFlags = (args, valued = []) => {
|
|
45
|
+
const out = [];
|
|
46
|
+
for (let i = 0; i < args.length; i++) {
|
|
47
|
+
if (valued.includes(args[i])) { i++; continue; }
|
|
48
|
+
if (!args[i].startsWith('-')) out.push(args[i]);
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// What one command is, as a milestone, or null. src is the whole Bash command (heredocs included).
|
|
54
|
+
export function milestone(argv, src, cwd) {
|
|
55
|
+
const cmd = base(argv[0] || '');
|
|
56
|
+
if (cmd === 'git') {
|
|
57
|
+
let i = 1, dir = cwd;
|
|
58
|
+
for (; argv[i]?.startsWith('-') || argv[i - 1] === '-C' || argv[i - 1] === '-c'; i++) if (argv[i - 1] === '-C') dir = path.resolve(cwd, argv[i]);
|
|
59
|
+
const sub = argv[i], rest = argv.slice(i + 1);
|
|
60
|
+
if (rest.includes('--dry-run') || rest.includes('-h') || rest.includes('--help')) return null;
|
|
61
|
+
if (sub === 'commit') return { kind: 'commit', dir };
|
|
62
|
+
if (sub === 'merge') {
|
|
63
|
+
if (rest.some(a => ['--abort', '--quit'].includes(a))) return null;
|
|
64
|
+
return { kind: 'merge', dir, ...(nonFlags(rest, ['-m', '-s', '-X', '-F'])[0] && { from: word(nonFlags(rest, ['-m', '-s', '-X', '-F'])[0]) }) };
|
|
65
|
+
}
|
|
66
|
+
if (sub === 'push') {
|
|
67
|
+
if (rest.some(a => ['-n', '--delete', '-d'].includes(a))) return null;
|
|
68
|
+
const [remote, ...refs] = nonFlags(rest, ['-o', '--push-option', '--repo', '--receive-pack', '--exec']);
|
|
69
|
+
return { kind: 'push', dir, ...(remote && { remote: word(remote) }), ...(refs.length && { refs: refs.slice(0, 4).map(word) }), ...(rest.includes('--tags') && { tags: true }) };
|
|
70
|
+
}
|
|
71
|
+
if (sub === 'tag') {
|
|
72
|
+
if (rest.some(a => ['-d', '--delete', '-l', '--list', '-v', '--verify', '-n', '--contains', '--no-contains', '--points-at', '--merged', '--no-merged'].includes(a))) return null;
|
|
73
|
+
const name = nonFlags(rest, ['-m', '--message', '-F', '--file', '-u', '--local-user', '--cleanup', '--sort', '--format'])[0];
|
|
74
|
+
return name ? { kind: 'tag', dir, name: word(name) } : null;
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
if (cmd === 'gh') {
|
|
79
|
+
if (argv[1] === 'release' && argv[2] === 'create') {
|
|
80
|
+
const name = nonFlags(argv.slice(3), ['-t', '--title', '-n', '--notes', '-F', '--notes-file', '--target', '-R', '--repo', '--discussion-category', '--notes-start-tag'])[0];
|
|
81
|
+
return { kind: 'release', dir: cwd, ...(name && { name: word(name) }) };
|
|
82
|
+
}
|
|
83
|
+
if (argv[1] === 'pr' && argv[2] === 'create') return { kind: 'pr', dir: cwd };
|
|
84
|
+
if (argv[1] === 'pr' && argv[2] === 'merge') {
|
|
85
|
+
const n = nonFlags(argv.slice(3), ['-b', '--body', '-F', '--body-file', '-t', '--subject', '-R', '--repo', '--match-head-commit', '-A', '--author-email'])[0];
|
|
86
|
+
return { kind: 'prmerge', dir: cwd, ...(n && { name: word(n) }) };
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
// A build on its way to TestFlight or the App Store, by any of the usual tools.
|
|
91
|
+
if (cmd === 'xcodebuild' && argv.includes('-exportArchive')) {
|
|
92
|
+
const plist = argv[argv.indexOf('-exportOptionsPlist') + 1];
|
|
93
|
+
let opts = '';
|
|
94
|
+
if (plist && !plist.startsWith('/dev/')) { try { opts = fs.readFileSync(path.resolve(cwd, plist), 'utf8'); } catch {} }
|
|
95
|
+
return UPLOADS.test(opts) || (plist?.startsWith('/dev/') && UPLOADS.test(src)) ? { kind: 'upload', dir: cwd } : null;
|
|
96
|
+
}
|
|
97
|
+
if ((cmd === 'altool' && argv.some(a => /^--upload-(app|package)$/.test(a))) || (cmd === 'asc' && (argv[1] === 'publish' || (argv[1] === 'builds' && argv[2] === 'upload')))
|
|
98
|
+
|| (cmd === 'fastlane' && argv.some(a => /^(pilot|deliver|upload_to_testflight|upload_to_app_store)$/.test(a))) || (cmd === 'eas' && argv[1] === 'submit'))
|
|
99
|
+
return { kind: 'upload', dir: cwd };
|
|
100
|
+
if (['npm', 'pnpm', 'yarn'].includes(cmd) && argv.slice(1).filter(a => !a.startsWith('-'))[0] === 'publish' && !argv.includes('--dry-run'))
|
|
101
|
+
return { kind: 'publish', dir: cwd };
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Where a branch points, from its file or packed-refs: the commit the next one in the reflog starts from.
|
|
106
|
+
function shaOf(cd, ref) {
|
|
107
|
+
try { return fs.readFileSync(path.join(cd, ref), 'utf8').trim(); } catch {}
|
|
108
|
+
try { return fs.readFileSync(path.join(cd, 'packed-refs'), 'utf8').split('\n').find(l => l.endsWith(` ${ref}`))?.split(' ')[0] || null; } catch { return null; }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The repo a folder is in, from its .git, read straight from the files (no git process):
|
|
112
|
+
// the worktree's own git folder (its HEAD and reflog), the shared one (refs, remotes), the branch.
|
|
113
|
+
export function repoOf(dir) {
|
|
114
|
+
for (let d = dir, i = 0; i < 40; i++, d = path.dirname(d)) {
|
|
115
|
+
const g = path.join(d, '.git');
|
|
116
|
+
let gd = null;
|
|
117
|
+
try {
|
|
118
|
+
if (fs.statSync(g).isFile()) gd = path.resolve(d, fs.readFileSync(g, 'utf8').match(/gitdir: (.+)/)[1].trim());
|
|
119
|
+
else gd = g;
|
|
120
|
+
} catch {}
|
|
121
|
+
if (gd) {
|
|
122
|
+
let cd = gd;
|
|
123
|
+
try { cd = path.resolve(gd, fs.readFileSync(path.join(gd, 'commondir'), 'utf8').trim()); } catch {}
|
|
124
|
+
let branch = null, head = null;
|
|
125
|
+
try {
|
|
126
|
+
const h = fs.readFileSync(path.join(gd, 'HEAD'), 'utf8').trim();
|
|
127
|
+
branch = h.match(/^ref: refs\/heads\/(.+)/)?.[1] || null;
|
|
128
|
+
head = branch ? shaOf(cd, `refs/heads/${branch}`) : /^[0-9a-f]{40,64}$/.test(h) ? h : null;
|
|
129
|
+
} catch {}
|
|
130
|
+
return { root: d, gd, cd, ...(branch && { branch }), ...(head && { head }) };
|
|
131
|
+
}
|
|
132
|
+
if (d === path.dirname(d)) break;
|
|
133
|
+
}
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// The session's name, as Claude Code keeps it (src/proc.mjs sessionFiles), and its pid.
|
|
138
|
+
function sessionOf(sid) {
|
|
139
|
+
let names = [];
|
|
140
|
+
try { names = fs.readdirSync(SESSIONS).filter(n => /^\d+\.json$/.test(n)); } catch { return null; }
|
|
141
|
+
for (const n of names) {
|
|
142
|
+
let text = '';
|
|
143
|
+
try { text = fs.readFileSync(path.join(SESSIONS, n), 'utf8'); } catch { continue; }
|
|
144
|
+
if (!text.includes(`"${sid}"`)) continue;
|
|
145
|
+
const name = text.match(/"name"\s*:\s*"((?:[^"\\]|\\.)*)"/)?.[1] || null;
|
|
146
|
+
return { name: name && JSON.parse(`"${name}"`), pid: Number(n.slice(0, -5)) };
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Where the transcript that will hold this call's result is, and how long it is now, so the
|
|
152
|
+
// look afterwards reads only what came after. A subagent's calls land in its own file.
|
|
153
|
+
function transcriptOf(input) {
|
|
154
|
+
const main = typeof input.transcript_path === 'string' ? input.transcript_path : null;
|
|
155
|
+
const sub = typeof input.agent_transcript_path === 'string' ? input.agent_transcript_path
|
|
156
|
+
: main && input.agent_id ? path.join(main.replace(/\.jsonl$/, ''), 'subagents', `agent-${input.agent_id}.jsonl`) : null;
|
|
157
|
+
for (const f of [sub, main]) {
|
|
158
|
+
if (!f) continue;
|
|
159
|
+
try { return { tx: f, txAt: fs.statSync(f).size }; } catch {}
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// The hook's call: note each milestone in this Bash command. split is src/guard.mjs's.
|
|
165
|
+
export function note(input, split) {
|
|
166
|
+
const src = input?.tool_input?.command;
|
|
167
|
+
if (typeof src !== 'string' || typeof input.session_id !== 'string') return;
|
|
168
|
+
const segs = split(src);
|
|
169
|
+
if (!segs) return;
|
|
170
|
+
let cwd = typeof input.cwd === 'string' ? input.cwd : process.cwd();
|
|
171
|
+
const found = [];
|
|
172
|
+
for (const seg of segs) {
|
|
173
|
+
if (seg.some(w => w.opaque && !w.redir)) continue;
|
|
174
|
+
const argv = argvOf(seg);
|
|
175
|
+
// cd for the commands after it (a cd inside ( ) ends with it, but agents rarely write that).
|
|
176
|
+
if (base(argv[0] || '') === 'cd' && seg.depth === 0) { cwd = path.resolve(cwd, (argv[1] || os.homedir()).replace(/^~(?=\/|$)/, os.homedir())); continue; }
|
|
177
|
+
const m = milestone(argv, src, cwd);
|
|
178
|
+
if (m) found.push(m);
|
|
179
|
+
}
|
|
180
|
+
if (!found.length) return;
|
|
181
|
+
const se = sessionOf(input.session_id);
|
|
182
|
+
const tx = transcriptOf(input);
|
|
183
|
+
const at = Date.now();
|
|
184
|
+
append(found.map(({ dir, ...m }) => entry(input, se, tx, at, m, dir)));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// A door the guard let through under the person's yes (src/doors.mjs), with that yes: { door, what, yes, yesBy, dir }.
|
|
188
|
+
export function noteDoor(input, { dir, ...m }) {
|
|
189
|
+
if (typeof input?.session_id !== 'string') return;
|
|
190
|
+
append([entry(input, sessionOf(input.session_id), transcriptOf(input), Date.now(), { kind: 'door', ...m, what: word(m.what) }, dir)]);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function entry(input, se, tx, at, m, dir) {
|
|
194
|
+
const repo = repoOf(dir);
|
|
195
|
+
return JSON.stringify({
|
|
196
|
+
at, sid: input.session_id, ...(input.agent_id && { agentId: String(input.agent_id) }), ...(input.agent_type && { label: word(input.agent_type) }),
|
|
197
|
+
...(se?.name ? { who: se.name } : se?.pid ? { who: `${path.basename(dir)}#${se.pid}` } : {}),
|
|
198
|
+
...m, dir: repo?.root || dir, ...(repo && { gd: repo.gd, cd: repo.cd, ...(repo.branch && { branch: repo.branch }), ...(repo.head && { head: repo.head }) }),
|
|
199
|
+
// A tag that's there already isn't this command's.
|
|
200
|
+
...(m.kind === 'tag' && repo && shaOf(repo.cd, `refs/tags/${m.name}`) && { had: true }),
|
|
201
|
+
...(typeof input.tool_use_id === 'string' && { tool: input.tool_use_id }), ...tx,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function append(lines) {
|
|
206
|
+
fs.mkdirSync(DIBS_HOME, { recursive: true });
|
|
207
|
+
fs.appendFileSync(TRAIL, lines.join('\n') + '\n');
|
|
208
|
+
try { if (fs.statSync(TRAIL).size > MAX_FILE) fs.renameSync(TRAIL, TRAIL_OLDER); } catch {}
|
|
209
|
+
}
|
package/src/util.mjs
CHANGED
|
@@ -28,6 +28,13 @@ export const inClaudeCode = () => !!(process.env.CLAUDECODE || process.env.CLAUD
|
|
|
28
28
|
export const sleep = ms => new Promise(r => setTimeout(r, ms));
|
|
29
29
|
export const sleepSync = ms => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
30
30
|
|
|
31
|
+
// The Codex thread a call comes from (Codex sets it in its commands' environment), so the
|
|
32
|
+
// person's screens can read that agent's own session file (src/codex.mjs). Kept on its runs and posts.
|
|
33
|
+
export const codexThread = () => (/^[\w-]{8,64}$/.test(process.env.CODEX_THREAD_ID || '') ? process.env.CODEX_THREAD_ID : undefined);
|
|
34
|
+
// The version of the documented --json layout (docs/json.md, src/schema.mjs). Renaming or dropping a
|
|
35
|
+
// documented field bumps it; adding one doesn't.
|
|
36
|
+
export const SCHEMA = 1;
|
|
37
|
+
|
|
31
38
|
export function say(msg) { process.stderr.write(`dibs: ${msg}\n`); }
|
|
32
39
|
// Exit 1 (nothing matched) and 75 (not your turn yet) aren't errors, so only the rest are red.
|
|
33
40
|
// Red as in src/style.mjs, kept here so the guard hook loads nothing more.
|