benignswarm 0.12.62 → 0.12.64

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.
@@ -9,6 +9,7 @@ import { requireInstall } from "../install.js";
9
9
  import { bindSession, codexArchivedRollout, codexRollout } from "../sessions.js";
10
10
  import { codexHooksPath } from "./codex.js";
11
11
  import { CODEX_MANAGED_MARK } from "../harness.js";
12
+ import { readState, writeState } from "../state.js";
12
13
  import { join } from "./join.js";
13
14
  import { installWatcherService } from "./watch.js";
14
15
  const SANDBOXES = ["read-only", "workspace-write", "danger-full-access"];
@@ -88,6 +89,11 @@ export async function codexStart(p, out) {
88
89
  }
89
90
  // 3. Bind it, so the hooks in that thread deliver to this agent and nobody else.
90
91
  bindSession("codex", thread, ref);
92
+ // A thread adopted here may end in a usage-limit error from the machine it came from, written in
93
+ // that machine's local time ("3:32 PM" Central read as UTC on ronald: six hours off). Only errors
94
+ // from turns on this machine pause it; if the account is still limited, the next turn says so here.
95
+ if (adopt)
96
+ writeState(ref, { ...readState(ref), turn_unpaused_at: Date.now() });
91
97
  // 4. The watcher, in turn mode: it runs each later turn itself.
92
98
  const exec = [codexBin, "exec", "resume", thread, ...settings].map(shq).join(" ") + " {text}";
93
99
  const watcherOut = [];
@@ -1,6 +1,6 @@
1
1
  import { spawn, spawnSync } from "node:child_process";
2
2
  import { existsSync, mkdirSync, readFileSync, statfsSync, writeFileSync } from "node:fs";
3
- import { arch, cpus, homedir, hostname, platform, totalmem, userInfo } from "node:os";
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";
6
6
  import { Client } from "../client.js";
@@ -8,6 +8,9 @@ import { configRoot, loadAgent, saveAgent } from "../config.js";
8
8
  import { CliError } from "../errors.js";
9
9
  import { CODEX_APPARMOR_PATH, CODEX_MANAGED_DIR, CODEX_MANAGED_MARK, codexAppArmorProfile, codexManagedHooks } from "../harness.js";
10
10
  import { applySparse } from "../git.js";
11
+ import { codexRollout } from "../sessions.js";
12
+ import { readState, writeState } from "../state.js";
13
+ import { readTurnPhase, usageLimitUntil } from "../turn.js";
11
14
  import { installService, lingerEnabled, MACHINE_LABEL, removeService, requireInstall, restartWatcher, serviceActive, WATCH_LABEL_PREFIX, watcherServices } from "../install.js";
12
15
  import { CLI_VERSION } from "../version.js";
13
16
  export const machineFile = () => join(configRoot(), "machine.json");
@@ -96,7 +99,14 @@ export async function reconcile(m, log) {
96
99
  }
97
100
  }
98
101
  const w = svc();
99
- out.push({ id: s.id, state: "running", dir: s.dir, thread: state.sessions[s.id].thread, watcher: w && serviceActive(w) ? "active" : "inactive", last_turn: lastTurnLine(s.agent) });
102
+ const th = state.sessions[s.id].thread;
103
+ // The operator lifted a usage-limit pause: what bschat codex unpause does, here.
104
+ if (s.unpause_at && state.sessions[s.id].unpause_applied !== s.unpause_at) {
105
+ writeState(s.agent, { ...readState(s.agent), turn_unpaused_at: Date.parse(s.unpause_at) });
106
+ state.sessions[s.id].unpause_applied = s.unpause_at;
107
+ log(`${s.agent}: usage-limit pause lifted by the operator`);
108
+ }
109
+ out.push({ id: s.id, state: "running", dir: s.dir, thread: th, watcher: w && serviceActive(w) ? "active" : "inactive", last_turn: lastTurnLine(s.agent), ...turnFacts(s.agent, th) });
100
110
  }
101
111
  catch (e) {
102
112
  const error = e.message.slice(0, 1000);
@@ -226,6 +236,27 @@ function errorText(s) {
226
236
  return last.slice(0, 500);
227
237
  }
228
238
  }
239
+ /** Whether the thread has a turn running, its process, and a usage-limit pause still in force. */
240
+ function turnFacts(agent, thread) {
241
+ const rollout = thread ? codexRollout(thread) : null;
242
+ if (!thread || !rollout)
243
+ return { turn: { running: false, since: null }, process: null, paused_until: null };
244
+ const ph = readTurnPhase(rollout);
245
+ const until = !ph.running ? usageLimitUntil(ph.lastError) : null;
246
+ const unpausedAt = readState(agent).turn_unpaused_at ?? 0;
247
+ const paused = until && until > Date.now() && Date.parse(ph.lastEventAt ?? "") > unpausedAt ? new Date(until).toISOString() : null;
248
+ return { turn: { running: ph.running, since: ph.running ? ph.startedAt : ph.lastEventAt }, process: ph.running ? turnProcess(thread) : null, paused_until: paused };
249
+ }
250
+ /** The Codex process running a turn of this thread (`codex exec resume <thread>`), from ps. */
251
+ export function turnProcess(thread, psOutput) {
252
+ const out = psOutput ?? (spawnSync("ps", ["-axo", "pid=,pcpu=,rss=,args="], { encoding: "utf8", maxBuffer: 16 * 1024 * 1024 }).stdout ?? "");
253
+ for (const line of out.split("\n")) {
254
+ const m = /^\s*(\d+)\s+([\d.]+)\s+(\d+)\s+(.*)$/.exec(line);
255
+ if (m && /\bcodex\b/.test(m[4]) && m[4].includes(`exec resume ${thread}`) && !m[4].startsWith("/bin/sh"))
256
+ return { pid: Number(m[1]), cpu: Number(m[2]), rss_bytes: Number(m[3]) * 1024 };
257
+ }
258
+ return null;
259
+ }
229
260
  /** The watcher's latest turn line (started, finished, FAILED, STOPPED, paused), for the report. */
230
261
  function lastTurnLine(ref) {
231
262
  const f = join(homedir(), ".bschat", "logs", `watch.${ref.replace("/", ".")}.err.log`);
@@ -253,6 +284,15 @@ export function codexSandbox(force = false) {
253
284
  sandboxCache = { at: Date.now(), result };
254
285
  return result;
255
286
  }
287
+ /** Memory a new process could use: Linux's MemAvailable (free alone undercounts the page cache), else free. */
288
+ function memAvailable() {
289
+ if (platform() === "linux") {
290
+ const m = /^MemAvailable:\s+(\d+) kB/m.exec(readFileSync("/proc/meminfo", "utf8"));
291
+ if (m)
292
+ return Number(m[1]) * 1024;
293
+ }
294
+ return freemem();
295
+ }
256
296
  /** What the operator needs to know about this machine, reported with every pass. */
257
297
  export function machineFacts() {
258
298
  const codex = spawnSync("codex", ["--version"], { encoding: "utf8" });
@@ -268,6 +308,7 @@ export function machineFacts() {
268
308
  const req = "/etc/codex/requirements.toml";
269
309
  return {
270
310
  os: platform(), arch: arch(), hostname: hostname(), user: userInfo().username, cpus: cpus().length, mem_total_bytes: totalmem(),
311
+ load: loadavg().map((x) => Math.round(x * 100) / 100), mem_available_bytes: memAvailable(),
271
312
  bschat: { version: CLI_VERSION, bin: requireInstall().bin },
272
313
  codex: codex.status === 0 ? { version: codex.stdout.trim(), logged_in: login?.status === 0 && /logged in/i.test(`${login.stdout}${login.stderr}`), login: `${login?.stdout ?? ""}${login?.stderr ?? ""}`.trim().slice(0, 200), sandbox: codexSandbox() } : null,
273
314
  managed_hooks: existsSync(req) && readFileSync(req, "utf8").includes(CODEX_MANAGED_MARK),
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLI_VERSION = "0.12.62";
1
+ export const CLI_VERSION = "0.12.64";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "benignswarm",
3
- "version": "0.12.62",
3
+ "version": "0.12.64",
4
4
  "description": "bschat: chat, status board and evidence store for coding agents (benignswarm.org)",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {