benignswarm 0.12.68 → 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.
- package/dist/commands/codex-start.js +17 -1
- package/dist/commands/machine.js +64 -2
- package/dist/commands/operator.js +1 -1
- package/dist/commands/watch.js +10 -2
- package/dist/state.js +24 -3
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -57,7 +57,7 @@ export async function harnessStart(p, out, harness) {
|
|
|
57
57
|
throw new CliError("usage", "--writable takes absolute or ~/ paths, comma separated");
|
|
58
58
|
// Refuses a sandbox the harness cannot honour before anything is enrolled.
|
|
59
59
|
const settings = harness === "codex"
|
|
60
|
-
? codexSettings(sandbox, model, effort, process.cwd(), configRoot(), writable)
|
|
60
|
+
? codexSettings(sandbox, model, effort, process.cwd(), configRoot(), [...writable, ...ownWorktreeGitDir(process.cwd())])
|
|
61
61
|
: claudeSettings(sandbox, model, effort, process.cwd(), configRoot(), writable);
|
|
62
62
|
// 1. Who: enroll with the invite token, or use credentials already on this machine.
|
|
63
63
|
const lines = [];
|
|
@@ -187,6 +187,22 @@ export function codexSettings(sandbox, model, effort, cwd, bschatHome, extraWrit
|
|
|
187
187
|
: []),
|
|
188
188
|
];
|
|
189
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* The agent's own linked-worktree git folder (<clone>/.git/worktrees/<name>), when it runs in one.
|
|
192
|
+
* Codex's sandbox makes the git folder of the directory it runs in read-only, and for a linked
|
|
193
|
+
* worktree that is this folder alone: the clone's common .git under the writable parent stays
|
|
194
|
+
* writable. So every reviewer on a machine failed `git fetch` from its home folder ("cannot open
|
|
195
|
+
* .../.git/worktrees/sol-reviewer/FETCH_HEAD: Read-only file system") while workers, committing in
|
|
196
|
+
* unit worktrees, did not. Listing it restores ordinary git there and opens nothing the agent could
|
|
197
|
+
* not already change through the common .git.
|
|
198
|
+
*/
|
|
199
|
+
export function ownWorktreeGitDir(cwd) {
|
|
200
|
+
const r = spawnSync("git", ["rev-parse", "--absolute-git-dir", "--path-format=absolute", "--git-common-dir"], { cwd, encoding: "utf8" });
|
|
201
|
+
if (r.status !== 0)
|
|
202
|
+
return [];
|
|
203
|
+
const [gitDir, common] = r.stdout.trim().split("\n");
|
|
204
|
+
return gitDir && common && gitDir !== common ? [gitDir] : [];
|
|
205
|
+
}
|
|
190
206
|
/** Starts a headless Codex session in its own process group and returns its thread id from the JSON event stream. */
|
|
191
207
|
async function startThread(codexBin, args, logPath, agent) {
|
|
192
208
|
const fd = openSync(logPath, "a");
|
package/dist/commands/machine.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync, statfsSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, statSync, statfsSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { arch, cpus, freemem, homedir, hostname, loadavg, platform, totalmem, userInfo } from "node:os";
|
|
4
4
|
import { dirname, join } from "node:path";
|
|
5
5
|
import { str } from "../args.js";
|
|
@@ -26,7 +26,12 @@ function writeMachineState(s) {
|
|
|
26
26
|
writeFileSync(stateFile(), JSON.stringify(s, null, 2) + "\n");
|
|
27
27
|
}
|
|
28
28
|
/** What a session's watcher is started with; a change means re-running codex start. The thread is compared separately. */
|
|
29
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Bumped when `bschat start` builds a session's settings differently, so every session is started
|
|
31
|
+
* again once with them. 2: the agent's own worktree git folder is writable under workspace-write.
|
|
32
|
+
*/
|
|
33
|
+
const START_REV = 2;
|
|
34
|
+
export const sessionFingerprint = (s, writable = []) => JSON.stringify([s.harness, s.dir, s.sandbox, s.model, s.effort, s.turn_timeout, ...(writable.length ? [writable] : []), ...(START_REV > 1 ? [`rev${START_REV}`] : [])]);
|
|
30
35
|
export const watchLabelOf = (ref) => `${WATCH_LABEL_PREFIX}${ref.replace("/", ".")}`;
|
|
31
36
|
/** The `bschat start` arguments that bring a session up (adopting its thread or session when known). */
|
|
32
37
|
export function startArgs(s, thread, writable = []) {
|
|
@@ -99,6 +104,13 @@ export async function reconcile(m, log) {
|
|
|
99
104
|
}
|
|
100
105
|
const w = svc();
|
|
101
106
|
const th = state.sessions[s.id].thread;
|
|
107
|
+
// A watcher that exits at every start (a corrupt state file after the disk filled) is
|
|
108
|
+
// restarted by systemd forever and looked "running" here; report it as the error it is.
|
|
109
|
+
const fault = watcherFault(s.agent);
|
|
110
|
+
if (fault) {
|
|
111
|
+
out.push({ id: s.id, state: "error", dir: s.dir, thread: th, watcher: w && serviceActive(w) ? "active" : "inactive", error: `its watcher keeps exiting: ${fault}` });
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
102
114
|
// The operator lifted a usage-limit pause: what bschat codex unpause does, here.
|
|
103
115
|
if (s.unpause_at && state.sessions[s.id].unpause_applied !== s.unpause_at) {
|
|
104
116
|
writeState(s.agent, { ...readState(s.agent), turn_unpaused_at: Date.parse(s.unpause_at) });
|
|
@@ -302,6 +314,56 @@ export function turnProcess(thread, harness = "codex", psOutput) {
|
|
|
302
314
|
}
|
|
303
315
|
return null;
|
|
304
316
|
}
|
|
317
|
+
/**
|
|
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.
|
|
324
|
+
*/
|
|
325
|
+
const CRASH_STARTS = 5;
|
|
326
|
+
const CRASH_WINDOW_MS = 5 * 60_000;
|
|
327
|
+
export function watcherFault(ref, logText, nowMs = Date.now()) {
|
|
328
|
+
let text = logText;
|
|
329
|
+
if (text === undefined) {
|
|
330
|
+
const f = join(homedir(), ".bschat", "logs", `watch.${ref.replace("/", ".")}.err.log`);
|
|
331
|
+
if (!existsSync(f))
|
|
332
|
+
return null;
|
|
333
|
+
const size = statSync(f).size;
|
|
334
|
+
const fd = openSync(f, "r");
|
|
335
|
+
const buf = Buffer.alloc(Math.min(size, 65_536));
|
|
336
|
+
readSync(fd, buf, 0, buf.length, size - buf.length);
|
|
337
|
+
closeSync(fd);
|
|
338
|
+
text = buf.toString("utf8");
|
|
339
|
+
}
|
|
340
|
+
const lines = text.split("\n").filter((l) => l.trim());
|
|
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--)
|
|
353
|
+
if (lines[i].startsWith('{"error"')) {
|
|
354
|
+
err = i;
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
if (err < 0)
|
|
358
|
+
return `it restarted ${recent.length} times in ${CRASH_WINDOW_MS / 60_000} min with no error logged`;
|
|
359
|
+
try {
|
|
360
|
+
const e = JSON.parse(lines[err]);
|
|
361
|
+
return `${e.error ?? "error"}: ${(e.message ?? "").split("\n")[0].slice(0, 300)}`;
|
|
362
|
+
}
|
|
363
|
+
catch {
|
|
364
|
+
return lines[err].slice(0, 300);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
305
367
|
/** The watcher's latest turn line (started, finished, FAILED, STOPPED, paused), for the report. */
|
|
306
368
|
function lastTurnLine(ref) {
|
|
307
369
|
const f = join(homedir(), ".bschat", "logs", `watch.${ref.replace("/", ".")}.err.log`);
|
|
@@ -243,7 +243,7 @@ export async function needsAdmin(p, out) {
|
|
|
243
243
|
return out.print("Nothing is waiting on you.", r);
|
|
244
244
|
const ind = (t) => t.replace(/\n/g, "\n ");
|
|
245
245
|
out.print(r.needs.map((n, i) => [
|
|
246
|
-
`${i + 1}. [${n.project}] ${n.kind === "unit_question" ? `WU #${n.unit} question` : n.kind === "wake_broken" ? "wake path" : n.kind === "notifier" ? "your alerts" : "decision"} · ${n.asked_by} · since ${n.since.slice(0, 16).replace("T", " ")} UTC`,
|
|
246
|
+
`${i + 1}. [${n.project}] ${n.kind === "unit_question" ? `WU #${n.unit} question` : n.kind === "wake_broken" ? "wake path" : n.kind === "notifier" ? "your alerts" : n.kind === "machine_session" ? "machine" : "decision"} · ${n.asked_by} · since ${n.since.slice(0, 16).replace("T", " ")} UTC`,
|
|
247
247
|
n.plain ? ` In plain words: ${ind(n.plain)}` : n.unexplained ? ` In plain words: (${n.asked_by} did not explain this; bschat-admin explain ${n.key})` : null,
|
|
248
248
|
n.blocks ? ` What's stuck: ${ind(n.blocks)}` : null,
|
|
249
249
|
` ${n.kind === "ask" ? "What I need from you: " : ""}${ind(n.text)}`,
|
package/dist/commands/watch.js
CHANGED
|
@@ -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
|
-
|
|
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/state.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { CliError } from "./errors.js";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
import { configRoot, isRef, splitRef } from "./config.js";
|
|
4
5
|
/** State is keyed by agent ref (`<scope>/<name>`), like credentials. */
|
|
@@ -23,10 +24,30 @@ export function readState(ref, env = process.env) {
|
|
|
23
24
|
adoptLegacyState(ref, env);
|
|
24
25
|
if (!existsSync(p))
|
|
25
26
|
return { inbox_after: "", last_check: 0 };
|
|
26
|
-
|
|
27
|
+
const text = readFileSync(p, "utf8");
|
|
28
|
+
let saved;
|
|
29
|
+
try {
|
|
30
|
+
saved = JSON.parse(text);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// Never started over silently: cursors, wake numbers and follows live here.
|
|
34
|
+
throw new CliError("corrupt_state", `${p} is not valid JSON (${text.length} bytes): a write to it was cut short`, "Check free disk space on this machine, then repair the file or move it aside (the agent then starts from fresh cursors and its wake-up numbering restarts)");
|
|
35
|
+
}
|
|
36
|
+
return { inbox_after: "", last_check: 0, ...saved };
|
|
27
37
|
}
|
|
28
38
|
export function writeState(agent, state, env = process.env) {
|
|
29
39
|
const p = statePath(agent, env);
|
|
30
40
|
mkdirSync(join(p, ".."), { recursive: true });
|
|
31
|
-
|
|
41
|
+
// Written beside it and renamed over it: a full disk once truncated three agents' files to 0
|
|
42
|
+
// bytes mid-write, and their watchers failed on every start for 17 hours. A failed write now
|
|
43
|
+
// throws and leaves the previous file whole.
|
|
44
|
+
const tmp = `${p}.${process.pid}.tmp`;
|
|
45
|
+
try {
|
|
46
|
+
writeFileSync(tmp, JSON.stringify(state, null, 2) + "\n", { mode: 0o600 });
|
|
47
|
+
renameSync(tmp, p);
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
rmSync(tmp, { force: true });
|
|
51
|
+
throw e;
|
|
52
|
+
}
|
|
32
53
|
}
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const CLI_VERSION = "0.12.
|
|
1
|
+
export const CLI_VERSION = "0.12.71";
|