@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
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
// The disk report (dibs disk): the biggest users of this machine's disk, dibs's
|
|
2
|
+
// or not, each with what it is, who made it when dibs knows, when it was last
|
|
3
|
+
// used, whether it's safe to delete and the command a person would use. dibs
|
|
4
|
+
// itself deletes none of it (the rules in src/diskuse.mjs and src/diskwarn.mjs
|
|
5
|
+
// stand); it names it, so agents stop hunting by hand.
|
|
6
|
+
//
|
|
7
|
+
// On 2026-11-28 dibs's cleaner freed 0 bytes while the space sat in simulators,
|
|
8
|
+
// agents' worktrees, swap, DerivedData and Claude Code's temp folders, none of
|
|
9
|
+
// which dibs named. The person's du also counted 27 GB in
|
|
10
|
+
// ~/Library/Developer/CoreDevice, but DeviceFS in there is a mount of the
|
|
11
|
+
// connected iPhones' own storage, not this disk, and a du into it walks the
|
|
12
|
+
// phone (it ran past 20 s). So every du here stays on one file system (-x).
|
|
13
|
+
//
|
|
14
|
+
// Measuring is bounded: du on a known list of places, each capped, all within
|
|
15
|
+
// TOTAL_CAP, and kept in diskreport.json for FRESH. The hook never measures;
|
|
16
|
+
// the notices and the alert only read the last report.
|
|
17
|
+
import fs from 'node:fs';
|
|
18
|
+
import os from 'node:os';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
import { execFile } from 'node:child_process';
|
|
21
|
+
import { HOME } from './config.mjs';
|
|
22
|
+
import { claudeTmp } from './files.mjs';
|
|
23
|
+
import { sessionFiles, makeAlive } from './proc.mjs';
|
|
24
|
+
import { TRAIL, TRAIL_OLDER } from './trailnote.mjs';
|
|
25
|
+
import { gb, now } from './util.mjs';
|
|
26
|
+
|
|
27
|
+
const FILE = path.join(HOME, 'diskreport.json');
|
|
28
|
+
// A report younger than this is shown as it is: du over 100 GB of simulators takes a minute.
|
|
29
|
+
export const FRESH = 10 * 6e4;
|
|
30
|
+
// The notices and the alert name a report up to this old; older, the disk has moved on.
|
|
31
|
+
const HINT_FRESH = 3 * FRESH;
|
|
32
|
+
// One place's du gets this long; what it hasn't finished counts as not measured.
|
|
33
|
+
const DU_CAP = 30e3;
|
|
34
|
+
// The whole report gets this long, so dibs disk answers within a minute even on a slow disk.
|
|
35
|
+
// DIBS_DISK_CAP shortens it in tests.
|
|
36
|
+
const TOTAL_CAP = Number(process.env.DIBS_DISK_CAP) || 60e3;
|
|
37
|
+
const AT_ONCE = 4;
|
|
38
|
+
// A worktree with no git activity this long, its branch already in main, is likely done with.
|
|
39
|
+
const STALE = 3 * 864e5;
|
|
40
|
+
const GIT_CAP = 10e3;
|
|
41
|
+
// What a gigabyte is to the cutoffs below. DIBS_DISK_GB stands in for it in tests, as in src/diskuse.mjs.
|
|
42
|
+
const UNIT = Number(process.env.DIBS_DISK_GB) || 1024 ** 3;
|
|
43
|
+
// Smaller than this doesn't make the list: deleting it wouldn't get anyone's job started.
|
|
44
|
+
const SHOW_MIN = UNIT / 10;
|
|
45
|
+
const ROWS = 15, TOP = 3;
|
|
46
|
+
|
|
47
|
+
// Sizes as a person reads them: 340 MB, not 0.3 GB.
|
|
48
|
+
const GB = b => (b >= 1024 ** 3 ? gb(b).replace(/G$/, ' GB') : b >= 1024 ** 2 ? `${Math.round(b / 1024 ** 2)} MB` : `${Math.round(b / 1024)} KB`);
|
|
49
|
+
const home = () => os.homedir();
|
|
50
|
+
const tilde = p => (p === home() || p.startsWith(home() + '/') ? '~' + p.slice(home().length) : p);
|
|
51
|
+
const quote = p => (/^[\w@%+=:,./~-]+$/.test(p) ? p : `'${p.replace(/'/g, `'\\''`)}'`);
|
|
52
|
+
const shown = p => (/[\s'"]/.test(p) ? quote(p) : tilde(p));
|
|
53
|
+
// How long ago, as a person says it about a folder: 12 min, 5 hours, 4 days.
|
|
54
|
+
const span = ms => (ms < 36e5 ? `${Math.max(1, Math.round(ms / 6e4))} min` : ms < 2 * 864e5 ? `${Math.round(ms / 36e5)} hours` : `${Math.round(ms / 864e5)} days`);
|
|
55
|
+
const ago = ms => (ms < 6e4 ? 'just now' : `${span(ms)} ago`);
|
|
56
|
+
const real = p => { try { return fs.realpathSync(p); } catch { return null; } };
|
|
57
|
+
const inside = (p, dir) => p === dir || p.startsWith(dir.endsWith('/') ? dir : dir + '/');
|
|
58
|
+
|
|
59
|
+
// The places that fill a disk, with what each is and how a person frees it. safe: yes (it rebuilds or
|
|
60
|
+
// comes back on its own), ask (someone's work may be in it), no (not files anyone should delete).
|
|
61
|
+
// DIBS_DISK_PLACES ([{ path, name, what, safe, why, cmd }]) stands in for them in tests.
|
|
62
|
+
function places() {
|
|
63
|
+
if (process.env.DIBS_DISK_PLACES) return JSON.parse(process.env.DIBS_DISK_PLACES);
|
|
64
|
+
const h = home(), dev = path.join(h, 'Library/Developer');
|
|
65
|
+
const common = [
|
|
66
|
+
{ path: path.join(h, '.npm/_cacache'), name: 'npm cache', what: "npm's download cache", safe: 'yes', why: 'npm downloads again what it needs', cmd: 'npm cache clean --force' },
|
|
67
|
+
{ path: path.join(HOME, 'work'), name: "dibs's job copies", what: "dibs's copies of other machines' jobs", safe: 'yes', why: 'dibs deletes each when its job ends; what is left is from jobs that crashed', cmd: `rm -rf ${tilde(path.join(HOME, 'work'))}/*`, who: 'dibs' },
|
|
68
|
+
];
|
|
69
|
+
if (process.platform !== 'darwin') return [...common,
|
|
70
|
+
{ path: path.join(h, '.cache'), name: 'caches', what: "programs' caches", safe: 'yes', why: 'each program makes its own again', cmd: 'rm -rf ~/.cache/<the program>' }];
|
|
71
|
+
return [...common,
|
|
72
|
+
{ path: path.join(dev, 'CoreSimulator/Devices'), name: 'simulators', what: 'every simulator, with its installed apps and their data', safe: 'ask', why: 'each simulator keeps its apps and data; ones no installed Xcode can run are always safe', cmd: 'xcrun simctl delete unavailable' },
|
|
73
|
+
{ path: path.join(dev, 'CoreSimulator/Caches'), name: 'simulator caches', what: "the simulators' shared caches", safe: 'yes', why: 'they rebuild when a simulator next boots', cmd: 'rm -rf ~/Library/Developer/CoreSimulator/Caches/*' },
|
|
74
|
+
{ path: path.join(dev, 'XCTestDevices'), name: 'test run simulators', what: 'simulators xcodebuild cloned for parallel test runs', safe: 'yes', why: 'the next parallel test run clones them again; not while one runs', cmd: 'rm -rf ~/Library/Developer/XCTestDevices/*' },
|
|
75
|
+
{ path: path.join(dev, 'Xcode/UserData/Previews'), name: 'SwiftUI preview simulators', what: "the simulators Xcode's previews run in", safe: 'yes', why: 'Xcode makes them again for the next preview', cmd: 'xcrun simctl --set previews delete all' },
|
|
76
|
+
{ path: path.join(dev, 'Xcode/DerivedData'), name: 'DerivedData', what: 'Xcode build output', safe: 'yes', why: 'Xcode builds it again (the first build after is slower); dibs already clears folders unused for a day', cmd: 'rm -rf ~/Library/Developer/Xcode/DerivedData/*' },
|
|
77
|
+
{ path: path.join(dev, 'Xcode/iOS DeviceSupport'), name: 'iOS DeviceSupport', what: 'debug symbols Xcode copied from iPhones', safe: 'yes', why: 'Xcode copies them again when a phone on that iOS version connects', cmd: "rm -rf ~/Library/Developer/Xcode/'iOS DeviceSupport'/*" },
|
|
78
|
+
{ path: path.join(dev, 'Xcode/Archives'), name: 'Xcode archives', what: 'app builds archived for TestFlight and the App Store', safe: 'ask', why: "they're what reads crash reports from those builds; delete old ones in Xcode's Organizer", cmd: null },
|
|
79
|
+
{ path: path.join(dev, 'CoreDevice'), name: 'CoreDevice', what: "Xcode's files for connected iPhones and iPads (DeviceFS in it is the phones' own storage, not this disk, and isn't counted)", safe: 'ask', why: 'only what is outside DeviceFS is on this disk', cmd: null },
|
|
80
|
+
{ path: path.join(h, 'Library/Caches'), name: 'app caches', what: "apps' caches, Homebrew's among them", safe: 'yes', why: 'each app makes its own again; quit it first', cmd: 'brew cleanup -s, or rm -rf ~/Library/Caches/<the app>' },
|
|
81
|
+
{ path: path.join(h, '.cache'), name: 'tool caches', what: "command line tools' caches", safe: 'yes', why: 'each tool makes its own again', cmd: 'rm -rf ~/.cache/<the tool>' },
|
|
82
|
+
{ path: path.join(h, '.Trash'), name: 'the Trash', what: 'files already deleted, waiting in the Trash', safe: 'yes', why: 'they were deleted on purpose', cmd: 'empty the Trash in Finder' },
|
|
83
|
+
];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Folders whose repositories' worktrees are listed: their own folders and one level in.
|
|
87
|
+
// DIBS_DISK_ROOTS (a JSON list) stands in for the home folder in tests.
|
|
88
|
+
const roots = () => (process.env.DIBS_DISK_ROOTS ? JSON.parse(process.env.DIBS_DISK_ROOTS) : [home()]);
|
|
89
|
+
|
|
90
|
+
const read = () => { try { return JSON.parse(fs.readFileSync(FILE, 'utf8')); } catch { return null; } };
|
|
91
|
+
function write(x) {
|
|
92
|
+
const tmp = `${FILE}.${process.pid}.tmp`;
|
|
93
|
+
fs.writeFileSync(tmp, JSON.stringify(x));
|
|
94
|
+
fs.renameSync(tmp, FILE);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// { path: bytes } for each folder, each du on one file system, AT_ONCE at a time, none past
|
|
98
|
+
// DU_CAP or the deadline. A folder not finished in time is left out.
|
|
99
|
+
async function sizes(dirs, deadline) {
|
|
100
|
+
const out = {}, todo = [...dirs];
|
|
101
|
+
const one = dir => new Promise(resolve => {
|
|
102
|
+
const left = Math.min(DU_CAP, deadline - Date.now());
|
|
103
|
+
if (left <= 0) return resolve();
|
|
104
|
+
execFile('du', ['-sxk', dir], { timeout: left, maxBuffer: 1 << 20 }, (e, stdout) => {
|
|
105
|
+
const m = !e && /^(\d+)\t/.exec(String(stdout || ''));
|
|
106
|
+
if (m) out[dir] = +m[1] * 1024;
|
|
107
|
+
resolve();
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
await Promise.all(Array.from({ length: AT_ONCE }, async () => { while (todo.length) await one(todo.shift()); }));
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const git = (args, cap = GIT_CAP) => new Promise(resolve =>
|
|
115
|
+
execFile('git', args, { timeout: cap, maxBuffer: 1 << 20 }, (e, stdout) => resolve({ ok: !e, code: e ? (typeof e.code === 'number' ? e.code : -1) : 0, out: String(stdout || '') })));
|
|
116
|
+
|
|
117
|
+
// A worktree's own git folder, from its .git file, or null when p isn't a worktree.
|
|
118
|
+
function gitdirOf(p) {
|
|
119
|
+
try {
|
|
120
|
+
const m = /^gitdir:\s*(.+)$/m.exec(fs.readFileSync(path.join(p, '.git'), 'utf8'));
|
|
121
|
+
return m ? path.resolve(p, m[1].trim()) : null;
|
|
122
|
+
} catch { return null; }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Every git worktree (from git worktree add) of the repositories in roots(), and worktrees
|
|
126
|
+
// sitting there themselves, outside Claude Code's temp folders (each session's own, counted there).
|
|
127
|
+
// Read from git's files: no git process.
|
|
128
|
+
export function worktrees() {
|
|
129
|
+
const found = new Map();
|
|
130
|
+
const tmp = claudeTmp().map(real).filter(Boolean);
|
|
131
|
+
const add = (wt, gd) => {
|
|
132
|
+
const p = real(wt);
|
|
133
|
+
if (!p || found.has(p) || tmp.some(r => inside(p, r))) return;
|
|
134
|
+
let cd = gd;
|
|
135
|
+
try { cd = path.resolve(gd, fs.readFileSync(path.join(gd, 'commondir'), 'utf8').trim()); } catch {}
|
|
136
|
+
const repo = path.basename(cd) === '.git' ? path.dirname(cd) : cd;
|
|
137
|
+
let branch = null;
|
|
138
|
+
try { branch = /^ref: refs\/heads\/(.+)/.exec(fs.readFileSync(path.join(gd, 'HEAD'), 'utf8').trim())?.[1] || null; } catch {}
|
|
139
|
+
// Last git activity: a checkout, commit or status refresh touches one of these.
|
|
140
|
+
const times = ['index', 'HEAD', 'logs/HEAD'].map(f => { try { return fs.statSync(path.join(gd, f)).mtimeMs; } catch { return 0; } });
|
|
141
|
+
let made = null;
|
|
142
|
+
try { made = fs.statSync(gd).birthtimeMs || null; } catch {}
|
|
143
|
+
found.set(p, { path: p, gd, repo, branch, usedAt: Math.max(...times) || null, made });
|
|
144
|
+
};
|
|
145
|
+
for (const root of roots()) {
|
|
146
|
+
let list = [];
|
|
147
|
+
try { list = fs.readdirSync(root, { withFileTypes: true }); } catch { continue; }
|
|
148
|
+
for (const e of [{ name: '.', isDirectory: () => true }, ...list]) {
|
|
149
|
+
if (!e.isDirectory() || (e.name.startsWith('.') && e.name !== '.')) continue;
|
|
150
|
+
const dir = path.join(root, e.name);
|
|
151
|
+
let gst;
|
|
152
|
+
try { gst = fs.statSync(path.join(dir, '.git')); } catch { continue; }
|
|
153
|
+
if (gst.isFile()) { const gd = gitdirOf(dir); if (gd && path.basename(path.dirname(gd)) === 'worktrees') add(dir, gd); continue; }
|
|
154
|
+
let wts = [];
|
|
155
|
+
try { wts = fs.readdirSync(path.join(dir, '.git', 'worktrees')); } catch {}
|
|
156
|
+
for (const w of wts) {
|
|
157
|
+
const gd = path.join(dir, '.git', 'worktrees', w);
|
|
158
|
+
try { add(path.dirname(fs.readFileSync(path.join(gd, 'gitdir'), 'utf8').trim()), gd); } catch {}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return [...found.values()];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Who made or used each worktree, from what dibs saw: the folders agents marked (dibs disk add), the
|
|
166
|
+
// trail's notes of commits and pushes made there (src/trailnote.mjs), and running sessions and jobs in it.
|
|
167
|
+
function witnesses(wts) {
|
|
168
|
+
const by = new Map(wts.map(w => [w.path, { first: null, last: null, marked: null, inUse: null }]));
|
|
169
|
+
const at = p => { for (const [k, v] of by) if (inside(p, k)) return v; return null; };
|
|
170
|
+
try {
|
|
171
|
+
for (const x of JSON.parse(fs.readFileSync(path.join(HOME, 'disposable.json'), 'utf8')).dirs || []) {
|
|
172
|
+
const v = by.get(x.path);
|
|
173
|
+
if (v) v.marked = x.who;
|
|
174
|
+
}
|
|
175
|
+
} catch {}
|
|
176
|
+
for (const f of [TRAIL_OLDER, TRAIL]) {
|
|
177
|
+
let text = '';
|
|
178
|
+
try { text = fs.readFileSync(f, 'utf8'); } catch { continue; }
|
|
179
|
+
for (const line of text.split('\n')) {
|
|
180
|
+
if (!line) continue;
|
|
181
|
+
let n;
|
|
182
|
+
try { n = JSON.parse(line); } catch { continue; }
|
|
183
|
+
const v = n.dir && n.who && by.get(real(n.dir) || n.dir);
|
|
184
|
+
if (!v) continue;
|
|
185
|
+
v.first ||= { who: n.who, at: n.at };
|
|
186
|
+
v.last = { who: n.who, at: n.at };
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const files = sessionFiles();
|
|
190
|
+
const up = makeAlive(files.map(f => f.pid));
|
|
191
|
+
for (const s of files) {
|
|
192
|
+
const v = s.cwd && up(s.pid) && at(real(s.cwd) || s.cwd);
|
|
193
|
+
if (v) v.inUse ||= s.name || `session ${s.sessionId.slice(0, 8)}`;
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
const st = JSON.parse(fs.readFileSync(path.join(HOME, 'state.json'), 'utf8'));
|
|
197
|
+
for (const l of [...(st.leases || []), ...(st.queue || [])]) {
|
|
198
|
+
const v = l.cwd && at(real(l.cwd) || l.cwd);
|
|
199
|
+
if (v) v.inUse ||= `${l.who}'s dibs job #${l.id}`;
|
|
200
|
+
}
|
|
201
|
+
} catch {}
|
|
202
|
+
return by;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Whether a worktree's branch is all in main (or master) and nothing in it is uncommitted.
|
|
206
|
+
// Asked only of worktrees untouched for STALE, so a report runs a few git calls, not one per worktree.
|
|
207
|
+
async function merged(w) {
|
|
208
|
+
if (!w.branch) return null;
|
|
209
|
+
let main = null;
|
|
210
|
+
for (const b of ['main', 'master']) if ((await git(['-C', w.repo, 'rev-parse', '--verify', '-q', `refs/heads/${b}`])).ok) { main = b; break; }
|
|
211
|
+
if (!main || main === w.branch) return null;
|
|
212
|
+
const anc = await git(['-C', w.repo, 'merge-base', '--is-ancestor', `refs/heads/${w.branch}`, `refs/heads/${main}`]);
|
|
213
|
+
if (!anc.ok) return anc.code === 1 ? { main, merged: false } : null;
|
|
214
|
+
const st = await git(['-C', w.path, 'status', '--porcelain']);
|
|
215
|
+
return { main, merged: true, dirty: !st.ok || st.out.trim() !== '' };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// A worktree as a row: whose, when, and whether it can go.
|
|
219
|
+
async function worktreeRow(w, bytes, seen, t) {
|
|
220
|
+
const repo = tilde(w.repo), name = tilde(w.path);
|
|
221
|
+
const who = seen.marked || seen.first?.who || null;
|
|
222
|
+
const used = Math.max(w.usedAt || 0, seen.last?.at || 0) || null;
|
|
223
|
+
const row = { kind: 'worktree', path: w.path, bytes, name, what: `git worktree of ${repo}${w.branch ? ` on branch ${w.branch}` : ''}`,
|
|
224
|
+
who, whoFrom: seen.marked ? 'marked' : seen.first ? 'trail' : null, made: w.made, usedAt: used, repo: w.repo, branch: w.branch };
|
|
225
|
+
const cmd = `git -C ${shown(w.repo)} worktree remove ${shown(w.path)}`;
|
|
226
|
+
if (seen.inUse) return { ...row, safe: 'no', why: `in use now by ${seen.inUse}` };
|
|
227
|
+
const m = used && t - used >= STALE ? await merged(w) : null;
|
|
228
|
+
if (m?.merged && !m.dirty) return { ...row, safe: 'likely', why: `its branch is all in ${m.main}, nothing is uncommitted, and nobody has touched it in ${span(t - used)}`, cmd, likely: true };
|
|
229
|
+
const open = m?.merged ? `its branch is in ${m.main} but it has uncommitted changes` : m ? `its branch has commits ${m.main} doesn't` : null;
|
|
230
|
+
return { ...row, safe: 'ask', why: `${open ? `${open}; ` : ''}ask ${who || 'whoever made it'} first`, cmd };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// One report: the places, the agents' scratch folders, swap and the worktrees, biggest first.
|
|
234
|
+
export async function measure(m = {}, t = now()) {
|
|
235
|
+
const deadline = Date.now() + TOTAL_CAP;
|
|
236
|
+
const ps = places().map(p => ({ ...p, path: real(p.path) || p.path })).filter(p => fs.existsSync(p.path));
|
|
237
|
+
const tmp = [...new Set(claudeTmp().map(real).filter(Boolean))];
|
|
238
|
+
const wts = worktrees();
|
|
239
|
+
const all = [...ps.map(p => p.path), ...tmp, ...wts.map(w => w.path)];
|
|
240
|
+
const size = await sizes(all, deadline);
|
|
241
|
+
const rows = [];
|
|
242
|
+
for (const p of ps) if (size[p.path] != null)
|
|
243
|
+
rows.push({ kind: 'place', path: p.path, bytes: size[p.path], name: p.name || tilde(p.path), what: p.what || null, who: p.who || null, safe: p.safe || 'ask', why: p.why || null, cmd: p.cmd || null });
|
|
244
|
+
for (const r of tmp) if (size[r] != null)
|
|
245
|
+
rows.push({ kind: 'scratch', path: r, bytes: size[r], name: "agents' scratch folders", what: "Claude Code's temp folder: each agent session's scratch files", who: 'agent sessions (each below)', safe: 'no', why: 'only the session that made each folder deletes in it; ask that agent', cmd: null });
|
|
246
|
+
if (m.swapUsed) rows.push({ kind: 'swap', path: null, bytes: m.swapUsed, name: 'swap', what: 'memory written out to disk', safe: 'no', why: "it isn't files: it shrinks as apps holding memory close, or at a restart", cmd: null });
|
|
247
|
+
const seen = witnesses(wts);
|
|
248
|
+
for (const w of wts) if (size[w.path] != null) rows.push(await worktreeRow(w, size[w.path], seen.get(w.path), t));
|
|
249
|
+
const missed = all.filter(p => size[p] == null).length;
|
|
250
|
+
const list = rows.filter(r => r.bytes >= SHOW_MIN).sort((a, b) => b.bytes - a.bytes);
|
|
251
|
+
return { at: t, rows: list.slice(0, ROWS), likely: list.filter(r => r.likely).map(r => r.path), missed, ms: TOTAL_CAP - Math.max(0, deadline - Date.now()) };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// The last report, measured again when older than fresh.
|
|
255
|
+
export async function report(m, { fresh = FRESH } = {}) {
|
|
256
|
+
const x = read();
|
|
257
|
+
if (x && now() - x.at < fresh) return x;
|
|
258
|
+
const y = await measure(m);
|
|
259
|
+
try { write(y); } catch {}
|
|
260
|
+
return y;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// One pass, from the throttle watcher: under ZONE floors, a report every FRESH, so the notices and
|
|
264
|
+
// the alert can name the biggest. Never throws.
|
|
265
|
+
const ZONE = 2;
|
|
266
|
+
let busy = false, tried = 0;
|
|
267
|
+
export async function reportOnce(floor, m) {
|
|
268
|
+
if (busy || !floor || m.diskFree == null || m.diskFree >= ZONE * floor) return;
|
|
269
|
+
// Tried within FRESH: on a full disk the report can't be saved, and must not du again every pass.
|
|
270
|
+
if (Date.now() - tried < FRESH) return;
|
|
271
|
+
busy = true;
|
|
272
|
+
tried = Date.now();
|
|
273
|
+
try { await report(m); } catch {} finally { busy = false; }
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// For the low disk notices and the disk alert: the three biggest, as words to add, or ''.
|
|
277
|
+
export function reportHint(t = now()) {
|
|
278
|
+
const x = read();
|
|
279
|
+
if (!x || t - x.at >= HINT_FRESH) return '';
|
|
280
|
+
const top = x.rows.slice(0, TOP);
|
|
281
|
+
if (!top.length) return '';
|
|
282
|
+
const can = r => (r.safe === 'yes' ? ', can go' : r.safe === 'likely' ? ', likely done with' : '');
|
|
283
|
+
return `. Biggest on this machine: ${top.map(r => `${r.kind === 'worktree' ? `worktree ${r.name}` : r.name} ${GB(r.bytes)}${can(r)}`).join('; ')}. dibs disk says who made each and how to free it`;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const SAFE = { yes: 'safe to delete', likely: 'likely removable', ask: 'ask first', no: 'keep' };
|
|
287
|
+
|
|
288
|
+
// The report as lines for dibs disk.
|
|
289
|
+
export function lines(x, { dim = s => s, bold = s => s } = {}, t = now()) {
|
|
290
|
+
const out = [bold('Biggest on this machine') + dim(` (measured ${ago(t - x.at)}; dibs deletes none of these)`)];
|
|
291
|
+
if (!x.rows.length) out.push(' Nothing dibs knows to look at holds more than a few megabytes.');
|
|
292
|
+
const w = Math.max(...x.rows.map(r => GB(r.bytes).length), 0);
|
|
293
|
+
for (const r of x.rows) {
|
|
294
|
+
out.push(` ${GB(r.bytes).padStart(w)} ${r.name}${r.path && tilde(r.path) !== r.name ? dim(` ${tilde(r.path)}`) : ''}`);
|
|
295
|
+
const pad = ' '.repeat(w + 4);
|
|
296
|
+
const about = [r.what, r.kind === 'worktree' && r.made && `made ${ago(t - r.made)}`,
|
|
297
|
+
r.who && (r.whoFrom === 'marked' ? `marked by ${r.who} (dibs disk add)` : r.whoFrom === 'trail' ? `first commit there by ${r.who}` : `made by ${r.who}`),
|
|
298
|
+
r.usedAt && `last used ${ago(t - r.usedAt)}`].filter(Boolean).join(', ');
|
|
299
|
+
if (about) out.push(dim(pad + about));
|
|
300
|
+
out.push(pad + `${SAFE[r.safe] || r.safe}${r.why ? `: ${r.why}` : ''}`);
|
|
301
|
+
if (r.cmd) out.push(pad + dim('$ ') + r.cmd);
|
|
302
|
+
}
|
|
303
|
+
if (x.likely?.length) out.push('', `Likely removable worktrees (merged, nothing uncommitted, untouched for days): ${x.likely.map(tilde).join(', ')}. dibs never removes them.`);
|
|
304
|
+
if (x.missed) out.push(dim(`${x.missed} folder${x.missed === 1 ? '' : 's'} took too long to measure and ${x.missed === 1 ? "isn't" : "aren't"} listed.`));
|
|
305
|
+
return out;
|
|
306
|
+
}
|