benignswarm 0.12.95 → 0.12.97

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.
@@ -1,11 +1,15 @@
1
1
  import { spawnSync } from "node:child_process";
2
- import { accessSync, constants, existsSync, readFileSync, readdirSync, realpathSync } from "node:fs";
3
- import { delimiter, join } from "node:path";
2
+ import { accessSync, constants, existsSync, readFileSync, readdirSync, realpathSync, statfsSync } from "node:fs";
3
+ import { basename, delimiter, join } from "node:path";
4
+ import { homedir, platform } from "node:os";
4
5
  import { bool } from "../args.js";
5
6
  import { CliError } from "../errors.js";
6
7
  import { binVersion, ensureCommandLinks, ensureInstall, latestPublished, lingerEnabled, readInstall, restartInstallServices, watcherServices } from "../install.js";
7
8
  import { CLI_VERSION } from "../version.js";
8
9
  import { refreshInstalledHooks } from "../harness.js";
10
+ import { configRoot } from "../config.js";
11
+ import { defaultWorkspaceWritableRoots } from "../start-options.js";
12
+ import { machineEnvironmentChecks } from "../environment-checks.js";
9
13
  /**
10
14
  * b9s install [<version> | --latest] make or upgrade this machine's one b9s install,
11
15
  * repair PATH aliases and restart services that run it
@@ -42,6 +46,79 @@ export async function install(p, out) {
42
46
  lines.push(`Not restarted:\n ${elsewhere.join("\n ")}`);
43
47
  out.print(lines.join("\n"), { install: rec, restarted, not_restarted: elsewhere });
44
48
  }
49
+ function diagnosticSpace(path) {
50
+ try {
51
+ const stat = statfsSync(path);
52
+ const total = stat.blocks * stat.bsize, free = stat.bavail * stat.bsize;
53
+ return Number.isSafeInteger(total) && Number.isSafeInteger(free) ? { free, total } : null;
54
+ }
55
+ catch {
56
+ return null;
57
+ }
58
+ }
59
+ export function doctorStateCheck(root = join(configRoot(), "state")) {
60
+ const corrupt = [];
61
+ const unreadable = [];
62
+ const inspect = (dir) => {
63
+ let entries;
64
+ try {
65
+ entries = readdirSync(dir, { withFileTypes: true });
66
+ }
67
+ catch {
68
+ unreadable.push(dir);
69
+ return;
70
+ }
71
+ for (const entry of entries) {
72
+ const path = join(dir, entry.name);
73
+ if (entry.isDirectory())
74
+ inspect(path);
75
+ else if (entry.isFile() && entry.name.endsWith(".json")) {
76
+ let contents;
77
+ try {
78
+ contents = readFileSync(path, "utf8");
79
+ }
80
+ catch {
81
+ unreadable.push(path);
82
+ continue;
83
+ }
84
+ try {
85
+ JSON.parse(contents);
86
+ }
87
+ catch {
88
+ corrupt.push(path);
89
+ }
90
+ }
91
+ }
92
+ };
93
+ if (existsSync(root))
94
+ inspect(root);
95
+ const status = corrupt.length ? "failed" : unreadable.length ? "unknown" : "healthy";
96
+ return {
97
+ check: "local_state_files", category: "state_file", component: "b9s local state", status,
98
+ observation: corrupt.length
99
+ ? `local state JSON file(s) cannot be parsed: ${corrupt.map((path) => basename(path)).slice(0, 5).join(", ")}`
100
+ : unreadable.length ? `local state could not be inspected (${unreadable.map((path) => basename(path)).slice(0, 5).join(", ")})` : "local state JSON files can be read",
101
+ remedy: corrupt.length
102
+ ? "Preserve the original file and a redacted parse diagnostic, check free disk space, then use the state owner's documented recovery; doctor does not reset state."
103
+ : unreadable.length ? "Check permissions and storage for the named local state path; preserve its contents and use the state owner's documented recovery." : null,
104
+ recovery_verified: status === "healthy",
105
+ };
106
+ }
107
+ function doctorEnvironmentChecks(pathValue, writableRoots) {
108
+ const codexRun = spawnSync("codex", ["sandbox", "--", "true"], { encoding: "utf8", timeout: 10_000 });
109
+ const codexProbe = codexRun.error?.message.includes("ENOENT") ? undefined : { ok: codexRun.status === 0, error: codexRun.signal === "SIGTERM" ? "sandbox check timed out" : `exit ${codexRun.status ?? "unknown"}` };
110
+ const bwrapRun = platform() === "linux" ? spawnSync("bwrap", ["--ro-bind", "/", "/", "--dev", "/dev", "--proc", "/proc", "--unshare-all", "--die-with-parent", "true"], { encoding: "utf8", timeout: 10_000 }) : null;
111
+ const bwrapProbe = bwrapRun ? (bwrapRun.error?.message.includes("ENOENT") ? undefined : { ok: bwrapRun.status === 0, error: bwrapRun.signal === "SIGTERM" ? "sandbox check timed out" : `exit ${bwrapRun.status ?? "unknown"}${bwrapRun.stderr ? `: ${bwrapRun.stderr.slice(0, 200)}` : ""}` }) : undefined;
112
+ const home = diagnosticSpace(homedir()), work = diagnosticSpace(join(homedir(), "bschat"));
113
+ const facts = {
114
+ daemon_path: pathValue,
115
+ codex: codexProbe ? { sandbox: codexProbe } : null,
116
+ "claude-code": bwrapProbe ? { sandbox: bwrapProbe } : null,
117
+ disk_free_bytes: home?.free ?? null, disk_total_bytes: home?.total ?? null,
118
+ workdir_free_bytes: work?.free ?? null, workdir_total_bytes: work?.total ?? null,
119
+ };
120
+ return [...machineEnvironmentChecks(facts, writableRoots), doctorStateCheck()];
121
+ }
45
122
  /**
46
123
  * b9s doctor every b9s on this machine and who uses which: the recorded install, copies on
47
124
  * PATH, npx caches, this repo's hook and instructions, and each watcher (plist versus
@@ -150,8 +227,9 @@ export async function doctor(_p, out) {
150
227
  checks.push({ ok: true, what: `watcher ${short}: running ${args[0]} (${ver ?? "?"})` });
151
228
  }
152
229
  const problems = checks.filter((c) => !c.ok);
230
+ const environmentChecks = doctorEnvironmentChecks(process.env.PATH ?? "", defaultWorkspaceWritableRoots());
153
231
  const text = checks.map((c) => `${c.ok ? "ok " : "PROBLEM "} ${c.what}${c.fix ? `\n fix: ${c.fix}` : ""}`).join("\n") + `\n${problems.length ? `${problems.length} problem${problems.length === 1 ? "" : "s"}.` : "No problems."}`;
154
- out.print(text, { install: rec, checks, problems: problems.length });
232
+ out.print(text, { install: rec, checks, environment_checks: environmentChecks, problems: problems.length });
155
233
  if (problems.length)
156
234
  process.exitCode = 1;
157
235
  }
@@ -106,6 +106,8 @@ export async function list(p, out) {
106
106
  const l = await resolveList(c, base, p.positionals[2]);
107
107
  let body = str(p.flags, "body") ?? "";
108
108
  const file = str(p.flags, "body-file");
109
+ if (file && str(p.flags, "body") !== undefined)
110
+ throw new CliError("usage", "list add accepts --body or --body-file, not both");
109
111
  if (file)
110
112
  body = readFileSync(file, "utf8");
111
113
  const req = { title: p.positionals.slice(3).join(" "), body };
@@ -172,6 +174,8 @@ export async function item(p, out) {
172
174
  req[key] = v;
173
175
  }
174
176
  const file = str(p.flags, "body-file");
177
+ if (file && str(p.flags, "body") !== undefined)
178
+ throw new CliError("usage", "item set accepts --body or --body-file, not both");
175
179
  if (file)
176
180
  req.body = readFileSync(file, "utf8");
177
181
  const ev = str(p.flags, "evidence");