benignswarm 0.12.111 → 0.12.113
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/dist/antigravity.js +3 -1
- package/dist/args.js +29 -4
- package/dist/brief.js +11 -1
- package/dist/client.js +8 -2
- package/dist/commands/admin.js +3 -0
- package/dist/commands/agent.js +2 -0
- package/dist/commands/hook.js +57 -24
- package/dist/commands/install.js +77 -13
- package/dist/commands/join.js +3 -5
- package/dist/commands/lists.js +2 -2
- package/dist/commands/machine-admin.js +125 -18
- package/dist/commands/machine.js +175 -33
- package/dist/commands/standing-rules.js +129 -0
- package/dist/commands/watch.js +3 -3
- package/dist/commands/worktrees.js +1 -1
- package/dist/commands/wu.js +10 -2
- package/dist/git.js +57 -4
- package/dist/harnesses.js +1 -1
- package/dist/help.js +28 -8
- package/dist/index.js +2 -0
- package/dist/install.js +122 -24
- package/dist/lock.js +2 -1
- package/dist/log-rotation.js +65 -0
- package/dist/standing-rules.js +78 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/install.js
CHANGED
|
@@ -134,14 +134,20 @@ export function binVersion(bin) {
|
|
|
134
134
|
return m ? m[1] : null;
|
|
135
135
|
}
|
|
136
136
|
/** The npm belonging to the Node running this process: the same runtime installs and runs b9s. */
|
|
137
|
-
function ownNpm() {
|
|
138
|
-
const npm = join(dirname(
|
|
137
|
+
export function ownNpm(execPath = process.execPath) {
|
|
138
|
+
const npm = join(dirname(execPath), "npm");
|
|
139
139
|
if (!existsSync(npm))
|
|
140
|
-
throw new CliError("no_npm", `no npm next to the Node running b9s (${
|
|
140
|
+
throw new CliError("no_npm", `no npm next to the Node running b9s (${execPath})`, "Install b9s with that Node's npm yourself (npm i -g benignswarm), then run: b9s install");
|
|
141
141
|
return npm;
|
|
142
142
|
}
|
|
143
|
+
/** npm scripts commonly use /usr/bin/env node, so PATH must select the Node beside this npm. */
|
|
144
|
+
function npmEnv(npm) {
|
|
145
|
+
const npmBin = dirname(resolve(npm));
|
|
146
|
+
const rest = (process.env.PATH ?? "").split(delimiter).filter((entry) => entry && resolve(entry) !== npmBin);
|
|
147
|
+
return { ...process.env, PATH: [npmBin, ...rest].join(delimiter) };
|
|
148
|
+
}
|
|
143
149
|
export function latestPublished(npm) {
|
|
144
|
-
const r = spawnSync(npm, ["view", "benignswarm", "version", "--prefer-online"], { encoding: "utf8", timeout: 60_000 });
|
|
150
|
+
const r = spawnSync(npm, ["view", "benignswarm", "version", "--prefer-online"], { encoding: "utf8", timeout: 60_000, env: npmEnv(npm) });
|
|
145
151
|
const v = (r.stdout ?? "").trim();
|
|
146
152
|
if (r.status !== 0 || !/^\d+\.\d+\.\d+$/.test(v))
|
|
147
153
|
throw new CliError("npm_view_failed", `could not ask npm for the latest benignswarm version (${npm})`, (r.stderr ?? "").trim().slice(0, 300));
|
|
@@ -154,7 +160,8 @@ export function latestPublished(npm) {
|
|
|
154
160
|
*/
|
|
155
161
|
export function ensureInstall(version, log, npmOverride) {
|
|
156
162
|
const npm = npmOverride ?? ownNpm();
|
|
157
|
-
const
|
|
163
|
+
const env = npmEnv(npm);
|
|
164
|
+
const pre = spawnSync(npm, ["prefix", "-g"], { encoding: "utf8", timeout: 30_000, env });
|
|
158
165
|
if (pre.status !== 0)
|
|
159
166
|
throw new CliError("npm_prefix_failed", `${npm} prefix -g failed`, (pre.stderr ?? "").trim().slice(0, 300));
|
|
160
167
|
const bin = join(pre.stdout.trim(), "bin", "b9s");
|
|
@@ -164,7 +171,7 @@ export function ensureInstall(version, log, npmOverride) {
|
|
|
164
171
|
}
|
|
165
172
|
else {
|
|
166
173
|
log(`${current ? `upgrading b9s ${current}` : "installing b9s"} to ${version} with ${npm}`);
|
|
167
|
-
const r = spawnSync(npm, ["i", "-g", `benignswarm@${version}`, "--prefer-online"], { stdio: ["ignore", 2, 2], timeout: 300_000 });
|
|
174
|
+
const r = spawnSync(npm, ["i", "-g", `benignswarm@${version}`, "--prefer-online"], { stdio: ["ignore", 2, 2], timeout: 300_000, env });
|
|
168
175
|
if (r.status !== 0)
|
|
169
176
|
throw new CliError("install_failed", `npm i -g benignswarm@${version} failed (exit ${r.status}) using ${npm}`, "Fix that npm's global prefix (permissions, network) or install it yourself, then run: b9s install");
|
|
170
177
|
const got = binVersion(bin);
|
|
@@ -179,6 +186,34 @@ export function ensureInstall(version, log, npmOverride) {
|
|
|
179
186
|
writeFileSync(installFile(), JSON.stringify(rec, null, 2) + "\n");
|
|
180
187
|
return rec;
|
|
181
188
|
}
|
|
189
|
+
/**
|
|
190
|
+
* Join/setup may refresh the machine install, but only within its recorded prefix. If the invoking
|
|
191
|
+
* CLI is older than the record, refuse before writing hooks instead of risking a downgrade. A first
|
|
192
|
+
* install uses this process's Node; changing the selected Node/npm is reserved for `b9s install`.
|
|
193
|
+
*/
|
|
194
|
+
export function ensureMachineInstall(version, log) {
|
|
195
|
+
const previous = readInstall();
|
|
196
|
+
if (!previous)
|
|
197
|
+
return ensureInstall(version, log, ownNpm());
|
|
198
|
+
if (!previous.npm || !existsSync(previous.npm)) {
|
|
199
|
+
throw new CliError("recorded_npm_missing", `the recorded install npm ${previous.npm || "(missing)"} is unavailable`, `Run ${previous.bin} install --latest from the Node installation you want this machine to use`);
|
|
200
|
+
}
|
|
201
|
+
const prefix = spawnSync(previous.npm, ["prefix", "-g"], { encoding: "utf8", timeout: 30_000, env: npmEnv(previous.npm) });
|
|
202
|
+
if (prefix.status !== 0)
|
|
203
|
+
throw new CliError("recorded_npm_prefix_failed", `${previous.npm} prefix -g failed`, (prefix.stderr ?? "").trim().slice(0, 300));
|
|
204
|
+
const expectedBin = join(dirname(previous.bin), "b9s");
|
|
205
|
+
const resolvedBin = join((prefix.stdout ?? "").trim(), "bin", "b9s");
|
|
206
|
+
if (resolve(expectedBin) !== resolve(resolvedBin)) {
|
|
207
|
+
throw new CliError("recorded_install_prefix_mismatch", `${previous.npm} resolves to ${resolvedBin}, but this machine records ${previous.bin}`, `Run ${previous.bin} install --latest from the Node installation you want this machine to use`);
|
|
208
|
+
}
|
|
209
|
+
// A join/setup invoked by an older npx or global copy must never write its own version back
|
|
210
|
+
// into a newer machine install. The operator can intentionally select or upgrade the install
|
|
211
|
+
// by invoking the explicit install command from the desired Node installation.
|
|
212
|
+
if (/^\d+\.\d+\.\d+$/.test(previous.version) && cmp(previous.version, version) > 0) {
|
|
213
|
+
throw new CliError("recorded_install_newer", `the recorded b9s ${previous.version} at ${previous.bin} is newer than this CLI ${version}`, `Run ${previous.bin} install --latest from the Node installation you want this machine to use`);
|
|
214
|
+
}
|
|
215
|
+
return ensureInstall(version, log, previous.npm);
|
|
216
|
+
}
|
|
182
217
|
/** Creates b9s-named shell links beside aliases of the same recorded install, without replacing another copy. */
|
|
183
218
|
export function ensureCommandLinks(installBin) {
|
|
184
219
|
const prefixBin = dirname(installBin);
|
|
@@ -245,11 +280,38 @@ export function watcherPlists() {
|
|
|
245
280
|
return [];
|
|
246
281
|
return readdirSync(dir).filter((f) => f.endsWith(".plist") && isOurService(f.slice(0, -".plist".length))).map((f) => ({ label: f.slice(0, -".plist".length), path: join(dir, f) }));
|
|
247
282
|
}
|
|
248
|
-
/**
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
283
|
+
/**
|
|
284
|
+
* The launchd domain for this user's services (WU #478). With a desktop session that is gui/<uid>, as
|
|
285
|
+
* always. A Mac with no one signed in at its desktop (FileVault unlocked over SSH, no auto-login) has no
|
|
286
|
+
* gui domain; there services are Background agents in user/<uid>, which load without a desktop session
|
|
287
|
+
* and keep running after the SSH session that loaded them ends. This is a deliberate mode, reported by
|
|
288
|
+
* doctor and machine status, never a silent substitute.
|
|
289
|
+
*/
|
|
290
|
+
export function launchdSessionNow() {
|
|
291
|
+
return spawnSync("launchctl", ["print", `gui/${process.getuid()}`], { stdio: "ignore" }).status === 0 ? "gui" : "headless";
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* The session-type key every LaunchAgent b9s writes carries on a Mac with no desktop session, so it loads
|
|
295
|
+
* as a Background agent in user/<uid> (WU #478). One function for every plist template: the watcher's
|
|
296
|
+
* own template in commands/watch.ts missed it at first, and mini1's first headless watcher was written for
|
|
297
|
+
* gui/<uid> and failed to load.
|
|
298
|
+
*/
|
|
299
|
+
export function launchdSessionTypeXml() {
|
|
300
|
+
return launchdSessionNow() === "headless" ? "\n <key>LimitLoadToSessionType</key><string>Background</string>" : "";
|
|
301
|
+
}
|
|
302
|
+
/** Which domain a written plist belongs to: a Background agent lives in user/<uid>, any other in gui/<uid>. */
|
|
303
|
+
export function plistLaunchdDomain(path) {
|
|
304
|
+
const r = spawnSync("plutil", ["-extract", "LimitLoadToSessionType", "raw", "-o", "-", path], { encoding: "utf8" });
|
|
305
|
+
const background = r.status === 0 && (r.stdout ?? "").trim() === "Background";
|
|
306
|
+
return `${background ? "user" : "gui"}/${process.getuid()}`;
|
|
307
|
+
}
|
|
308
|
+
export function launchdLoadedIn(label) {
|
|
309
|
+
const uid = process.getuid();
|
|
310
|
+
return [`gui/${uid}`, `user/${uid}`].filter((d) => spawnSync("launchctl", ["print", `${d}/${label}`], { stdio: "ignore" }).status === 0);
|
|
311
|
+
}
|
|
312
|
+
function launchdBootout(label, path) {
|
|
313
|
+
// A service may be loaded in either domain (a Mac that gained or lost a desktop session); unload it from each.
|
|
314
|
+
for (const domain of launchdLoadedIn(label)) {
|
|
253
315
|
const out = spawnSync("launchctl", ["bootout", `${domain}/${label}`], { encoding: "utf8" });
|
|
254
316
|
if (out.status !== 0)
|
|
255
317
|
throw new CliError("launchctl_failed", `launchctl bootout ${domain}/${label} failed`, (out.stderr ?? "").trim());
|
|
@@ -258,17 +320,60 @@ export function reloadLaunchAgent(label, path) {
|
|
|
258
320
|
const until = Date.now() + 15_000;
|
|
259
321
|
while (spawnSync("launchctl", ["print", `${domain}/${label}`], { stdio: "ignore" }).status === 0) {
|
|
260
322
|
if (Date.now() > until)
|
|
261
|
-
throw new CliError("launchctl_failed", `launchd still lists ${domain}/${label} 15 s after bootout; nothing was loaded`, `Load it yourself: launchctl bootstrap ${
|
|
323
|
+
throw new CliError("launchctl_failed", `launchd still lists ${domain}/${label} 15 s after bootout; nothing was loaded`, `Load it yourself: launchctl bootstrap ${plistLaunchdDomain(path)} ${path}`);
|
|
262
324
|
spawnSync("sleep", ["0.2"]);
|
|
263
325
|
}
|
|
264
326
|
}
|
|
327
|
+
}
|
|
328
|
+
/** The pid of a loaded LaunchAgent in whichever domain holds it, or null when it is not running. */
|
|
329
|
+
export function launchdPid(label) {
|
|
330
|
+
for (const domain of launchdLoadedIn(label)) {
|
|
331
|
+
const out = spawnSync("launchctl", ["print", `${domain}/${label}`], { encoding: "utf8" }).stdout ?? "";
|
|
332
|
+
const pid = Number(/^\s*pid = (\d+)$/m.exec(out)?.[1]);
|
|
333
|
+
if (Number.isInteger(pid) && pid > 0)
|
|
334
|
+
return pid;
|
|
335
|
+
}
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
/** What launchd mode this Mac's services use, for the machine report, machine status and doctor (WU #478). */
|
|
339
|
+
export function launchdFacts() {
|
|
340
|
+
const daemonPlist = join(launchAgentsDir(), `${MACHINE_LABEL}.plist`);
|
|
341
|
+
const watchers = {};
|
|
342
|
+
for (const w of watcherServices()) {
|
|
343
|
+
if (w.kind !== "launchd" || w.label === MACHINE_LABEL)
|
|
344
|
+
continue; // the daemon is reported on its own
|
|
345
|
+
const d = plistLaunchdDomain(w.path);
|
|
346
|
+
watchers[d] = (watchers[d] ?? 0) + 1;
|
|
347
|
+
}
|
|
348
|
+
return { session: launchdSessionNow(), daemon_domain: existsSync(daemonPlist) ? plistLaunchdDomain(daemonPlist) : null, watchers };
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* What an operator needs to know about this Mac's launchd mode when provisioning it (WU #478): which domain
|
|
352
|
+
* the services use and what a reboot or sign-out does to them.
|
|
353
|
+
*/
|
|
354
|
+
export function launchdGuidance(session = launchdSessionNow()) {
|
|
355
|
+
const uid = process.getuid();
|
|
356
|
+
return session === "headless"
|
|
357
|
+
? `launchd: headless Mac, services are Background agents in user/${uid}. After a reboot they start once someone logs in over SSH (that also unlocks FileVault); a desktop sign-in is not needed. Harness sign-ins kept in the login keychain (Claude Code's by default) are unreadable without a desktop session; Codex and Antigravity keep theirs in files. External volumes may be refused to processes started while no one is signed in at the desktop: keep the workdir on the internal disk.`
|
|
358
|
+
: `launchd: desktop session, services are LaunchAgents in gui/${uid}. They stop when that user signs out and start again at the next desktop sign-in; for a Mac meant to run unattended, use automatic sign-in, or install the services while no one is signed in so they run headless.`;
|
|
359
|
+
}
|
|
360
|
+
/** One line for status output. */
|
|
361
|
+
export function launchdFactsLine(f) {
|
|
362
|
+
const w = Object.entries(f.watchers).map(([d, n]) => `${n} in ${d}`).join(", ") || "none";
|
|
363
|
+
return `launchd: ${f.session === "headless" ? "headless Mac (no desktop session)" : "desktop session"}; daemon ${f.daemon_domain ?? "not installed"}; watchers ${w}`;
|
|
364
|
+
}
|
|
365
|
+
/** Loads (or reloads) a LaunchAgent from its plist, so what runs is what the file says. */
|
|
366
|
+
export function reloadLaunchAgent(label, path) {
|
|
367
|
+
launchdBootout(label, path);
|
|
368
|
+
const domain = plistLaunchdDomain(path);
|
|
265
369
|
const r = spawnSync("launchctl", ["bootstrap", domain, path], { encoding: "utf8" });
|
|
266
370
|
if (r.status !== 0)
|
|
267
371
|
throw new CliError("launchctl_failed", `launchctl bootstrap ${domain} ${path} failed`, (r.stderr ?? "").trim());
|
|
268
372
|
}
|
|
269
373
|
/** Restarts a loaded LaunchAgent in place (new code, same definition). */
|
|
270
374
|
export function restartLaunchAgent(label) {
|
|
271
|
-
|
|
375
|
+
const domain = launchdLoadedIn(label)[0];
|
|
376
|
+
return !!domain && spawnSync("launchctl", ["kickstart", "-k", `${domain}/${label}`], { stdio: "ignore" }).status === 0;
|
|
272
377
|
}
|
|
273
378
|
export const systemdUserDir = () => join(process.env.XDG_CONFIG_HOME ?? join(homedir(), ".config"), "systemd", "user");
|
|
274
379
|
/** Every watcher service on this machine. */
|
|
@@ -522,7 +627,7 @@ export function installService(label, args, cwd, env, logName) {
|
|
|
522
627
|
<key>WorkingDirectory</key><string>${esc(cwd)}</string>
|
|
523
628
|
<key>RunAtLoad</key><true/>
|
|
524
629
|
<key>KeepAlive</key><true/>
|
|
525
|
-
<key>AbandonProcessGroup</key><true
|
|
630
|
+
<key>AbandonProcessGroup</key><true/>${launchdSessionTypeXml()}
|
|
526
631
|
<key>StandardOutPath</key><string>${join(logDir, `${logName}.out.log`)}</string>
|
|
527
632
|
<key>StandardErrorPath</key><string>${join(logDir, `${logName}.err.log`)}</string>
|
|
528
633
|
</dict></plist>
|
|
@@ -540,19 +645,14 @@ export function removeService(s) {
|
|
|
540
645
|
spawnSync("systemctl", ["--user", "daemon-reload"], { stdio: "ignore" });
|
|
541
646
|
return;
|
|
542
647
|
}
|
|
543
|
-
|
|
544
|
-
if (spawnSync("launchctl", ["print", `${domain}/${s.label}`], { stdio: "ignore" }).status === 0) {
|
|
545
|
-
const r = spawnSync("launchctl", ["bootout", `${domain}/${s.label}`], { encoding: "utf8" });
|
|
546
|
-
if (r.status !== 0)
|
|
547
|
-
throw new CliError("launchctl_failed", `launchctl bootout ${domain}/${s.label} failed`, (r.stderr ?? "").trim());
|
|
548
|
-
}
|
|
648
|
+
launchdBootout(s.label, s.path);
|
|
549
649
|
rmSync(s.path, { force: true });
|
|
550
650
|
}
|
|
551
651
|
/** Whether a service's process is running now. */
|
|
552
652
|
export function serviceActive(s) {
|
|
553
653
|
if (s.kind === "systemd")
|
|
554
654
|
return (spawnSync("systemctl", ["--user", "is-active", `${s.label}.service`], { encoding: "utf8" }).stdout ?? "").trim() === "active";
|
|
555
|
-
return
|
|
655
|
+
return launchdPid(s.label) !== null;
|
|
556
656
|
}
|
|
557
657
|
/** Restarts a watcher service in place (new code, same definition); false when it is not loaded. */
|
|
558
658
|
export function restartWatcher(s) {
|
|
@@ -572,9 +672,7 @@ function watcherPid(service) {
|
|
|
572
672
|
const pid = Number((r.stdout ?? "").trim());
|
|
573
673
|
return r.status === 0 && Number.isInteger(pid) && pid > 0 ? pid : null;
|
|
574
674
|
}
|
|
575
|
-
|
|
576
|
-
const pid = Number(/"PID" = (\d+);/.exec(output)?.[1]);
|
|
577
|
-
return Number.isInteger(pid) && pid > 0 ? pid : null;
|
|
675
|
+
return launchdPid(service.label);
|
|
578
676
|
}
|
|
579
677
|
const watcherRestartDriver = {
|
|
580
678
|
pid: watcherPid,
|
package/dist/lock.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { launchdLoadedIn } from "./install.js";
|
|
1
2
|
import { mkdirSync, openSync, closeSync, readFileSync, unlinkSync, writeSync } from "node:fs";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
import { configRoot } from "./config.js";
|
|
@@ -44,7 +45,7 @@ export function acquireWatchLock(ref, env = process.env, pid = process.pid) {
|
|
|
44
45
|
throw e;
|
|
45
46
|
const holder = Number(readFileSync(p, "utf8").trim());
|
|
46
47
|
if (Number.isInteger(holder) && holder > 0 && alive(holder)) {
|
|
47
|
-
throw new CliError("watch_running", `a watcher for ${ref} is already running (pid ${holder})`, `Stop it first: kill ${holder}, or launchctl bootout gui/$(id -u)/org.benignswarm.watch.${ref.replace("/", ".")} if launchd started it. Lock file: ${p}`);
|
|
48
|
+
throw new CliError("watch_running", `a watcher for ${ref} is already running (pid ${holder})`, `Stop it first: kill ${holder}, or launchctl bootout ${process.platform === "darwin" ? (launchdLoadedIn(`org.benignswarm.watch.${ref.replace("/", ".")}`)[0] ?? `gui/$(id -u)`) : `gui/$(id -u)`}/org.benignswarm.watch.${ref.replace("/", ".")} if launchd started it. Lock file: ${p}`);
|
|
48
49
|
}
|
|
49
50
|
unlinkSync(p); // stale: the process that wrote it is gone
|
|
50
51
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { closeSync, fstatSync, openSync, readdirSync, readSync, renameSync, statSync, truncateSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Bounded service logs (WU #481). launchd (StandardErrorPath) and systemd (StandardError=append:)
|
|
5
|
+
* keep the watcher and daemon logs open for the life of the service and nothing rotated them: on
|
|
6
|
+
* 2026-11-18 mbp held 594 MB of watcher logs, one 190 MB file, and the daemon read them every pass.
|
|
7
|
+
*
|
|
8
|
+
* Rotation is copy-and-truncate because the service manager holds the open file: renaming it would
|
|
9
|
+
* leave the service writing to the renamed file. Both managers open these files for append (systemd
|
|
10
|
+
* "append:", launchd's Standard*Path), so after the truncate the next write lands at the new end
|
|
11
|
+
* instead of leaving a hole. The newest `keepBytes` (from a line start) become `<name>.1`, replacing
|
|
12
|
+
* the previous one. A line written between the copy and the truncate is lost; that window is the
|
|
13
|
+
* copy of at most `keepBytes`, once per rotation.
|
|
14
|
+
*/
|
|
15
|
+
export const LOG_ROTATE_BYTES = 20 * 1024 * 1024;
|
|
16
|
+
/** Rotates every `*.log` in `dir` larger than `maxBytes`. Files it cannot read or truncate throw: a failed rotation is never silent. */
|
|
17
|
+
export function rotateLogs(dir, maxBytes = LOG_ROTATE_BYTES, keepBytes = maxBytes) {
|
|
18
|
+
const done = [];
|
|
19
|
+
let names;
|
|
20
|
+
try {
|
|
21
|
+
names = readdirSync(dir);
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
if (e.code === "ENOENT")
|
|
25
|
+
return done;
|
|
26
|
+
throw e;
|
|
27
|
+
}
|
|
28
|
+
for (const name of names) {
|
|
29
|
+
if (!name.endsWith(".log"))
|
|
30
|
+
continue;
|
|
31
|
+
const path = join(dir, name);
|
|
32
|
+
const size = statSync(path).size;
|
|
33
|
+
if (size <= maxBytes)
|
|
34
|
+
continue;
|
|
35
|
+
done.push({ file: name, bytes_before: size, kept_bytes: rotateOne(path, keepBytes) });
|
|
36
|
+
}
|
|
37
|
+
return done;
|
|
38
|
+
}
|
|
39
|
+
function rotateOne(path, keepBytes) {
|
|
40
|
+
const fd = openSync(path, "r");
|
|
41
|
+
let tail;
|
|
42
|
+
try {
|
|
43
|
+
const size = fstatSync(fd).size;
|
|
44
|
+
const start = Math.max(0, size - keepBytes);
|
|
45
|
+
const buf = Buffer.alloc(size - start);
|
|
46
|
+
let got = 0;
|
|
47
|
+
while (got < buf.length) {
|
|
48
|
+
const n = readSync(fd, buf, got, buf.length - got, start + got);
|
|
49
|
+
if (n === 0)
|
|
50
|
+
break;
|
|
51
|
+
got += n;
|
|
52
|
+
}
|
|
53
|
+
// Start the kept copy at a line boundary unless the whole file fits.
|
|
54
|
+
const nl = start > 0 ? buf.indexOf(0x0a) : -1;
|
|
55
|
+
tail = buf.subarray(nl >= 0 ? nl + 1 : 0, got);
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
closeSync(fd);
|
|
59
|
+
}
|
|
60
|
+
const previous = `${path}.1`;
|
|
61
|
+
writeFileSync(`${previous}.tmp`, tail);
|
|
62
|
+
renameSync(`${previous}.tmp`, previous);
|
|
63
|
+
truncateSync(path, 0);
|
|
64
|
+
return tail.length;
|
|
65
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export function formatStandingRuleSet(set, label) {
|
|
2
|
+
if (!set || set.complete === false || !Array.isArray(set.rules)) {
|
|
3
|
+
return `[b9s] Standing rule delivery is incomplete${label ? ` for ${label}` : ""}. Run b9s process to retrieve the complete current set; it was not acknowledged.`;
|
|
4
|
+
}
|
|
5
|
+
const active = set.rules.filter((rule) => rule.status === "active");
|
|
6
|
+
const byId = new Map(active.map((rule) => [rule.id, rule]));
|
|
7
|
+
const state = set.acknowledged === true ? "acknowledged" : "acknowledgment pending";
|
|
8
|
+
const lines = [`Standing rules${label ? ` for ${label}` : ""}: ${active.length} active; ${state}${set.fingerprint ? ` (ruleset ${set.fingerprint})` : ""}.`];
|
|
9
|
+
if (!active.length)
|
|
10
|
+
lines.push(" (no active standing rules)");
|
|
11
|
+
for (const rule of active) {
|
|
12
|
+
const expires = rule.expires_at ? ` · expires ${rule.expires_at}` : " · no expiry";
|
|
13
|
+
lines.push(` [${rule.scope.kind}:${rule.scope.id} · revision ${rule.revision} · by ${rule.author.name}${expires}] ${rule.text}`);
|
|
14
|
+
for (const conflict of rule.conflicts ?? []) {
|
|
15
|
+
const other = byId.get(conflict.rule_id);
|
|
16
|
+
const source = other
|
|
17
|
+
? `[${other.scope.kind}:${other.scope.id} · revision ${other.revision} · by ${other.author.name}] ${other.text}`
|
|
18
|
+
: `rule ${conflict.rule_id} (not in this effective ruleset)`;
|
|
19
|
+
lines.push(` conflicts with ${conflict.rule_id} (${conflict.resolution}): ${source}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return lines.join("\n");
|
|
23
|
+
}
|
|
24
|
+
export function formatStandingRuleSummary(response) {
|
|
25
|
+
if (!response || response.complete !== true)
|
|
26
|
+
return "standing rules: unavailable; run b9s process to retrieve the complete set";
|
|
27
|
+
const summarize = (set, label) => {
|
|
28
|
+
if (set.complete === false || !Array.isArray(set.rules))
|
|
29
|
+
return `${label}: incomplete; run b9s process`;
|
|
30
|
+
const active = set.rules.filter((rule) => rule.status === "active");
|
|
31
|
+
const byId = new Map(active.map((rule) => [rule.id, rule]));
|
|
32
|
+
const lines = [`${label}: ${active.length} active rule${active.length === 1 ? "" : "s"}; ${set.acknowledged === true ? "acknowledged" : "acknowledgment pending"}`];
|
|
33
|
+
for (const rule of active) {
|
|
34
|
+
lines.push(` - [${rule.scope.kind}:${rule.scope.id}] ${rule.text.slice(0, 180)}${rule.text.length > 180 ? "…" : ""}`);
|
|
35
|
+
for (const conflict of rule.conflicts ?? []) {
|
|
36
|
+
const other = byId.get(conflict.rule_id);
|
|
37
|
+
const source = other
|
|
38
|
+
? `[${other.scope.kind}:${other.scope.id}] ${other.text.slice(0, 140)}${other.text.length > 140 ? "…" : ""}`
|
|
39
|
+
: `rule ${conflict.rule_id} (not in this effective ruleset)`;
|
|
40
|
+
lines.push(` conflicts with ${conflict.rule_id} (${conflict.resolution}): ${source}`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return lines.join("\n");
|
|
44
|
+
};
|
|
45
|
+
const sets = response.rulesets;
|
|
46
|
+
if (Array.isArray(sets)) {
|
|
47
|
+
if (!sets.length)
|
|
48
|
+
return "standing rules: no project rulesets are available";
|
|
49
|
+
return sets.map((set) => summarize(set, set.project ?? "account")).join("\n");
|
|
50
|
+
}
|
|
51
|
+
if (!Array.isArray(response.rules))
|
|
52
|
+
return "standing rules: unavailable; run b9s process to retrieve the complete set";
|
|
53
|
+
return summarize({ rules: response.rules, complete: true, acknowledged: response.acknowledged }, "standing rules");
|
|
54
|
+
}
|
|
55
|
+
export function ruleAcknowledgmentTargets(response, project) {
|
|
56
|
+
if (response.complete !== true)
|
|
57
|
+
return [];
|
|
58
|
+
const targets = [];
|
|
59
|
+
const add = (path, fingerprint, acknowledged) => {
|
|
60
|
+
if (fingerprint && acknowledged !== true)
|
|
61
|
+
targets.push({ path, fingerprint });
|
|
62
|
+
};
|
|
63
|
+
if (Array.isArray(response.rulesets)) {
|
|
64
|
+
for (const set of response.rulesets) {
|
|
65
|
+
if (set.complete === false)
|
|
66
|
+
continue;
|
|
67
|
+
const route = set.project_id
|
|
68
|
+
? `/projects/${encodeURIComponent(set.project_id)}/agents/me/standing-rules/ack`
|
|
69
|
+
: "/agents/me/standing-rules/ack";
|
|
70
|
+
add(route, set.fingerprint, set.acknowledged);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
const route = project ? `/projects/${encodeURIComponent(project)}/agents/me/standing-rules/ack` : "/agents/me/standing-rules/ack";
|
|
75
|
+
add(route, response.fingerprint, response.acknowledged);
|
|
76
|
+
}
|
|
77
|
+
return targets;
|
|
78
|
+
}
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const CLI_VERSION = "0.12.
|
|
1
|
+
export const CLI_VERSION = "0.12.113";
|