benignswarm 0.12.164 → 0.12.166

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/install.js CHANGED
@@ -11,14 +11,32 @@ import { watcherMemoryLimits } from "./watcher-memory.js";
11
11
  export const installFile = () => join(configRoot(), "install.json");
12
12
  export function readInstall() {
13
13
  const p = installFile();
14
- if (!existsSync(p))
15
- return null;
16
14
  try {
17
- return JSON.parse(readFileSync(p, "utf8"));
15
+ lstatSync(p);
16
+ }
17
+ catch (error) {
18
+ if (error.code === "ENOENT")
19
+ return null;
20
+ throw error;
21
+ }
22
+ let contents;
23
+ try {
24
+ contents = readFileSync(p, "utf8");
18
25
  }
19
26
  catch {
20
- throw new CliError("bad_install_record", `${p} is not valid JSON`, `Run: npx -y benignswarm@${CLI_VERSION} install (rewrites it)`);
27
+ throw new CliError("bad_install_record", `${p} cannot be read`, "Ask the machine operator to repair the recorded install");
28
+ }
29
+ let value;
30
+ try {
31
+ value = JSON.parse(contents);
32
+ }
33
+ catch {
34
+ throw new CliError("bad_install_record", `${p} is not valid JSON`, "Ask the machine operator to repair the recorded install");
35
+ }
36
+ if (!value || typeof value !== "object" || Array.isArray(value) || typeof value.bin !== "string" || !isAbsolute(value.bin)) {
37
+ throw new CliError("bad_install_record", `${p} does not contain an absolute bin path`, "Ask the machine operator to repair the recorded install");
21
38
  }
39
+ return value;
22
40
  }
23
41
  /** The recorded install, checked to still exist; what watchers and hooks are pointed at. */
24
42
  export function requireInstall() {
@@ -0,0 +1,50 @@
1
+ import { join } from "node:path";
2
+ import { configRoot } from "./config.js";
3
+ import { readJsonFile, updateJsonFile } from "./shared-file.js";
4
+ const statePath = () => join(configRoot(), "state", "network-outages.json");
5
+ const MAX_OUTAGES = 12;
6
+ const MAX_CALLS_PER_OUTAGE = 12;
7
+ function validOutages(value) {
8
+ return Array.isArray(value) && value.every((item) => Boolean(item)
9
+ && typeof item === "object"
10
+ && typeof item.started_at === "string"
11
+ && (item.ended_at === null || typeof item.ended_at === "string")
12
+ && Array.isArray(item.failing_calls)
13
+ && item.failing_calls.every((call) => typeof call === "string"));
14
+ }
15
+ function readOutages(path = statePath()) {
16
+ const value = readJsonFile(path);
17
+ if (value === undefined)
18
+ return [];
19
+ if (!validOutages(value))
20
+ throw new Error(`${path} contains invalid machine network outage state`);
21
+ return value;
22
+ }
23
+ /** Record request failures without retaining request bodies, headers, credentials, or query strings. */
24
+ export function recordMachineNetworkRequest(event, now = new Date().toISOString(), path = statePath()) {
25
+ if (event.phase !== "end")
26
+ return;
27
+ const call = `${event.method.toUpperCase()} ${event.path.split("?", 1)[0]}${event.attempt ? ` (attempt ${event.attempt})` : ""}`.slice(0, 160);
28
+ updateJsonFile(path, (stored) => {
29
+ const outages = stored ?? [];
30
+ if (!validOutages(outages))
31
+ throw new Error(`${path} contains invalid machine network outage state`);
32
+ const current = outages.at(-1);
33
+ if (event.error) {
34
+ if (!current || current.ended_at !== null)
35
+ outages.push({ started_at: now, ended_at: null, failing_calls: [call] });
36
+ else if (!current.failing_calls.includes(call) && current.failing_calls.length < MAX_CALLS_PER_OUTAGE)
37
+ current.failing_calls.push(call);
38
+ }
39
+ else if (current && current.ended_at === null) {
40
+ current.ended_at = now;
41
+ }
42
+ else {
43
+ return undefined;
44
+ }
45
+ return outages.slice(-MAX_OUTAGES);
46
+ }, { mode: 0o600, serialize: (outages) => `${JSON.stringify(outages)}\n` });
47
+ }
48
+ export function machineNetworkOutages(path = statePath()) {
49
+ return readOutages(path);
50
+ }
@@ -269,7 +269,7 @@ function linuxListenersForPid(pid) {
269
269
  }
270
270
  return [...new Set(out)];
271
271
  }
272
- function readLinuxProcessOrigin(pid, ppid) {
272
+ export function readLinuxProcessOrigin(pid, ppid) {
273
273
  let parentUid = null;
274
274
  let cgroup = null;
275
275
  try {
@@ -283,7 +283,7 @@ function readLinuxProcessOrigin(pid, ppid) {
283
283
  return { parentPid: ppid, parentUid, cgroup };
284
284
  }
285
285
  /** True when the process no longer runs: /proc/<pid> is gone, or its state is Z (zombie) or X (dead). */
286
- function linuxProcessEnded(pid) {
286
+ export function linuxProcessEnded(pid) {
287
287
  let stat;
288
288
  try {
289
289
  stat = readFileSync(`/proc/${pid}/stat`, "utf8");
@@ -3,7 +3,7 @@ const PRUNE_PATH_LIMIT = 5;
3
3
  const NESTED_LIST_LIMIT = 5;
4
4
  const REPORT_TEXT_BYTE_LIMIT = 40;
5
5
  const DIAGNOSTIC_FIELDS = new Set([
6
- "error", "reason", "message", "diagnosis", "detail", "stderr", "stdout", "output", "log",
6
+ "error", "reason", "aborted_reason", "message", "diagnosis", "detail", "stderr", "stdout", "output", "log",
7
7
  "path", "repo", "dirty_closed_paths", "paths", "skipped_protected", "cleaned_items",
8
8
  "processes", "uninspected_processes", "active_processes", "readiness_refusals",
9
9
  ]);