benignswarm 0.12.70 → 0.12.71

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.
@@ -315,11 +315,15 @@ export function turnProcess(thread, harness = "codex", psOutput) {
315
315
  return null;
316
316
  }
317
317
  /**
318
- * The error the watcher keeps exiting with, or null. A crash loop (systemd and launchd restart it
319
- * every few seconds) leaves a fresh error line each time, while a watcher that failed once and
320
- * recovered leaves an old one, so an error counts only when the line logged just before it is from
321
- * the last two minutes. Error lines carry no time of their own.
318
+ * The error the watcher keeps exiting with, or null. The log also holds the agent's own turns,
319
+ * whose failed commands print error lines too (sol-reviewer's refused `wu done` was reported as its
320
+ * watcher failing), so an error line proves nothing. A crash loop is the watcher starting over and
321
+ * over: systemd and launchd restart it within seconds. At least CRASH_STARTS starts in the last
322
+ * CRASH_WINDOW_MS, the latest within two minutes, is a loop; an install or a session restart starts
323
+ * it once or twice. The reason given is the last error line before the latest start.
322
324
  */
325
+ const CRASH_STARTS = 5;
326
+ const CRASH_WINDOW_MS = 5 * 60_000;
323
327
  export function watcherFault(ref, logText, nowMs = Date.now()) {
324
328
  let text = logText;
325
329
  if (text === undefined) {
@@ -328,31 +332,36 @@ export function watcherFault(ref, logText, nowMs = Date.now()) {
328
332
  return null;
329
333
  const size = statSync(f).size;
330
334
  const fd = openSync(f, "r");
331
- const buf = Buffer.alloc(Math.min(size, 16_384));
335
+ const buf = Buffer.alloc(Math.min(size, 65_536));
332
336
  readSync(fd, buf, 0, buf.length, size - buf.length);
333
337
  closeSync(fd);
334
338
  text = buf.toString("utf8");
335
339
  }
336
340
  const lines = text.split("\n").filter((l) => l.trim());
337
- let lastErr = -1;
338
- for (let i = lines.length - 1; i >= 0; i--)
341
+ const starts = [];
342
+ lines.forEach((l, i) => {
343
+ const m = /^(\d{4}-\d\d-\d\dT[\d:.]+Z) bschat watch\[[^\]]+\]: watching\b/.exec(l);
344
+ if (m)
345
+ starts.push({ i, at: Date.parse(m[1]) });
346
+ });
347
+ const recent = starts.filter((x) => nowMs - x.at <= CRASH_WINDOW_MS);
348
+ if (recent.length < CRASH_STARTS || nowMs - recent[recent.length - 1].at > 120_000)
349
+ return null;
350
+ const last = recent[recent.length - 1].i;
351
+ let err = -1;
352
+ for (let i = last - 1; i >= 0; i--)
339
353
  if (lines[i].startsWith('{"error"')) {
340
- lastErr = i;
354
+ err = i;
341
355
  break;
342
356
  }
343
- if (lastErr < 0)
344
- return null;
345
- let at = NaN;
346
- for (let i = lastErr - 1; i >= 0 && Number.isNaN(at); i--)
347
- at = Date.parse(/^(\d{4}-\d\d-\d\dT[\d:.]+Z) /.exec(lines[i])?.[1] ?? "");
348
- if (Number.isNaN(at) || nowMs - at > 120_000)
349
- return null;
357
+ if (err < 0)
358
+ return `it restarted ${recent.length} times in ${CRASH_WINDOW_MS / 60_000} min with no error logged`;
350
359
  try {
351
- const e = JSON.parse(lines[lastErr]);
360
+ const e = JSON.parse(lines[err]);
352
361
  return `${e.error ?? "error"}: ${(e.message ?? "").split("\n")[0].slice(0, 300)}`;
353
362
  }
354
363
  catch {
355
- return lines[lastErr].slice(0, 300);
364
+ return lines[err].slice(0, 300);
356
365
  }
357
366
  }
358
367
  /** The watcher's latest turn line (started, finished, FAILED, STOPPED, paused), for the report. */
@@ -6,7 +6,7 @@ import { applyAck } from "./wake.js";
6
6
  import { lingerEnabled, loadSystemdUnit, reloadLaunchAgent, removeService, requireInstall, systemdUnit, systemdUserDir, WATCH_LABEL_PREFIX, watcherServices } from "../install.js";
7
7
  import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
8
8
  import { createHash } from "node:crypto";
9
- import { homedir, userInfo } from "node:os";
9
+ import { homedir, uptime, userInfo } from "node:os";
10
10
  import { basename, dirname, join } from "node:path";
11
11
  import { bool, str } from "../args.js";
12
12
  import { configRoot, loadAgent, resolveAgentName } from "../config.js";
@@ -442,7 +442,15 @@ async function turnStep(ctl, probe, timeoutMin, log, wake, name) {
442
442
  log(`the last turn in ${probe.what} hit the ${probe.label} usage limit; no turns until ${new Date(until).toISOString()}`);
443
443
  return;
444
444
  }
445
- if (ph.running) {
445
+ // A turn that started before this machine last booted died with it: no process survives a
446
+ // reboot, and waiting out the quiet limit would idle the agent for up to --turn-timeout.
447
+ const bootedAt = Date.now() - uptime() * 1000;
448
+ if (ph.running && ph.startedAt && Date.parse(ph.startedAt) < bootedAt) {
449
+ if (ctl.loggedRunning !== `boot:${ph.startedAt}`)
450
+ log(`the turn started at ${ph.startedAt} was cut off when this machine restarted (booted ${new Date(bootedAt).toISOString()}); treating it as ended`);
451
+ ctl.loggedRunning = `boot:${ph.startedAt}`;
452
+ }
453
+ else if (ph.running) {
446
454
  const quietMs = Date.now() - Date.parse(ph.lastEventAt ?? ph.startedAt ?? "");
447
455
  if (!(quietMs >= timeoutMin * 60_000)) {
448
456
  if (ctl.loggedRunning !== ph.startedAt)
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLI_VERSION = "0.12.70";
1
+ export const CLI_VERSION = "0.12.71";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "benignswarm",
3
- "version": "0.12.70",
3
+ "version": "0.12.71",
4
4
  "description": "bschat: chat, status board and evidence store for coding agents (benignswarm.org)",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {