benignswarm 0.12.49 → 0.12.50

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.
@@ -6,7 +6,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
6
6
  import { dirname, join as pathJoin } from "node:path";
7
7
  import { CliError } from "../errors.js";
8
8
  import { defaultMachine } from "../git.js";
9
- import { ensureInstall } from "../install.js";
9
+ import { ensureInstall, readInstall } from "../install.js";
10
10
  import { codexHooksPath } from "./codex.js";
11
11
  import { claudeCheckoutOwner, claudeCheckoutTaken, withCodexHooks, HARNESSES, findIdentityFile, installForHarness, normalizeVendor, readIdentityFile, writeIdentity } from "../harness.js";
12
12
  import { detectVendor } from "../vendor.js";
@@ -167,8 +167,16 @@ export function assertVendorMatchesRepo(root, name, vendor, replace) {
167
167
  throw new CliError("vendor_mismatch", `${p} says ${name} runs in ${mapped.join(" and ")} here, but this command detected ${vendor}`, `If ${name} runs in ${mapped[0]}, run this from that harness or pass --vendor ${mapped[0]}. If the file is wrong, pass --replace.`);
168
168
  }
169
169
  /** This machine's one install, at least this CLI's version (installed or upgraded when needed). */
170
+ /**
171
+ * The machine's install, upgraded to this version if older. An existing record keeps its npm, so a
172
+ * join run from another Node (an nvm shell, an npx cache) upgrades the machine's install in place
173
+ * instead of moving it there and leaving every watcher on the old copy. Only a first install uses
174
+ * the npm next to the running Node.
175
+ */
170
176
  function machineInstall() {
171
- return ensureInstall(CLI_VERSION, (m) => process.stderr.write(`bschat: ${m}\n`));
177
+ const prev = readInstall();
178
+ const npm = prev?.npm && existsSync(prev.npm) ? prev.npm : undefined;
179
+ return ensureInstall(CLI_VERSION, (m) => process.stderr.write(`bschat: ${m}\n`), npm);
172
180
  }
173
181
  function writeIdentityDryRun(root, vendor, name, replace) {
174
182
  const p = findIdentityFile(root);
@@ -1,6 +1,6 @@
1
1
  import { spawn, spawnSync } from "node:child_process";
2
2
  import { bindSession, codexArchivedRollout, codexRollout, sessionOfExec, threadOfExec } from "../sessions.js";
3
- import { readTurnPhase, startTurn, stopTurn } from "../turn.js";
3
+ import { readTurnPhase, startTurn, stopTurn, usageLimitUntil } from "../turn.js";
4
4
  import { applyAck } from "./wake.js";
5
5
  import { reloadLaunchAgent, requireInstall } from "../install.js";
6
6
  import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
@@ -96,7 +96,7 @@ export async function watch(p, out) {
96
96
  // scan on start and then hourly reports worktrees nobody is touching, which agent calls never would.
97
97
  const scanCreds = loadAgent(name);
98
98
  let lastScan = 0;
99
- const ctl = { turn: null, failedAt: 0, loggedRunning: null };
99
+ const ctl = { turn: null, failedAt: 0, loggedRunning: null, pausedUntil: 0 };
100
100
  for (;;) {
101
101
  if (activity && heartbeatClient)
102
102
  lastSample = await heartbeatIfActive(heartbeatClient, activity, lastSample, interval, log);
@@ -338,11 +338,20 @@ async function turnStep(ctl, thread, timeoutMin, log, wake, name) {
338
338
  log(`TURN #${turn.seq} STOPPED after the ${timeoutMin}-min limit (${how}); its work so far is in the session`);
339
339
  }
340
340
  else {
341
- log(`TURN #${turn.seq} FAILED (${how}) after ${mins} min${turn.confirmed ? "" : "; it never started in the session, so the wake-up stays unacknowledged; retrying in 5 min"}. Its output is in this log.`);
342
- if (!turn.confirmed) {
343
- writeState(name, { ...readState(name), watch_pending: undefined, watch_dormant_logged_at: undefined });
344
- ctl.failedAt = Date.now();
341
+ // Every failure waits before the next turn: a turn started right after one that failed
342
+ // usually fails the same way. A usage limit waits until the time Codex names.
343
+ const error = rollout ? readTurnPhase(rollout).lastError : null;
344
+ const until = usageLimitUntil(error);
345
+ if (until && until > Date.now()) {
346
+ ctl.pausedUntil = until;
347
+ log(`TURN #${turn.seq} FAILED: the Codex account hit its usage limit; no turns until ${new Date(until).toISOString()} (Codex said: ${error})`);
348
+ }
349
+ else {
350
+ log(`TURN #${turn.seq} FAILED (${how}) after ${mins} min${turn.confirmed ? "" : "; it never started in the session, so the wake-up stays unacknowledged"}${error ? `; Codex said: ${error}` : ""}. Next turn in 5 min at the earliest; its output is in this log.`);
345
351
  }
352
+ if (!turn.confirmed)
353
+ writeState(name, { ...readState(name), watch_pending: undefined, watch_dormant_logged_at: undefined });
354
+ ctl.failedAt = Date.now();
346
355
  }
347
356
  ctl.turn = null;
348
357
  return;
@@ -355,10 +364,19 @@ async function turnStep(ctl, thread, timeoutMin, log, wake, name) {
355
364
  }
356
365
  if (Date.now() - ctl.failedAt < TURN_RETRY_MS)
357
366
  return;
367
+ if (ctl.pausedUntil > Date.now())
368
+ return;
358
369
  if (rollout) {
359
370
  // A turn this watcher did not start: the Codex app, or a turn left running by a watcher that
360
371
  // restarted (turns run in their own process group and outlive it). Never start a second one.
361
372
  const ph = readTurnPhase(rollout);
373
+ // A watcher that restarted after a usage-limit failure reads the pause from the session itself.
374
+ const until = !ph.running ? usageLimitUntil(ph.lastError) : null;
375
+ if (until && until > Date.now()) {
376
+ ctl.pausedUntil = until;
377
+ log(`the last turn in thread ${thread} hit the Codex usage limit; no turns until ${new Date(until).toISOString()}`);
378
+ return;
379
+ }
362
380
  if (ph.running) {
363
381
  const quietMs = Date.now() - Date.parse(ph.lastEventAt ?? ph.startedAt);
364
382
  if (quietMs < timeoutMin * 60_000) {
package/dist/install.js CHANGED
@@ -97,6 +97,14 @@ export function reloadLaunchAgent(label, path) {
97
97
  const out = spawnSync("launchctl", ["bootout", `${domain}/${label}`], { encoding: "utf8" });
98
98
  if (out.status !== 0)
99
99
  throw new CliError("launchctl_failed", `launchctl bootout ${domain}/${label} failed`, (out.stderr ?? "").trim());
100
+ // bootout returns before launchd has finished removing the job, and a bootstrap in that window
101
+ // fails, leaving the watcher unloaded (luna-worker's, after codex start). Wait until it is gone.
102
+ const until = Date.now() + 15_000;
103
+ while (spawnSync("launchctl", ["print", `${domain}/${label}`], { stdio: "ignore" }).status === 0) {
104
+ if (Date.now() > until)
105
+ throw new CliError("launchctl_failed", `launchd still lists ${domain}/${label} 15 s after bootout; nothing was loaded`, `Load it yourself: launchctl bootstrap ${domain} ${path}`);
106
+ spawnSync("sleep", ["0.2"]);
107
+ }
100
108
  }
101
109
  const r = spawnSync("launchctl", ["bootstrap", domain, path], { encoding: "utf8" });
102
110
  if (r.status !== 0)
package/dist/turn.js CHANGED
@@ -5,6 +5,7 @@ export function codexTurnPhase(rolloutText) {
5
5
  let open = null;
6
6
  let startedAt = null;
7
7
  let lastEventAt = null;
8
+ let lastError = null;
8
9
  for (const line of rolloutText.split("\n")) {
9
10
  if (!line)
10
11
  continue;
@@ -23,11 +24,28 @@ export function codexTurnPhase(rolloutText) {
23
24
  if (t === "task_started") {
24
25
  open = j.timestamp ?? null;
25
26
  startedAt = open;
27
+ lastError = null;
26
28
  }
27
- else if (t === "task_complete" || t === "turn_aborted")
29
+ else if (t === "task_complete" || t === "turn_aborted") {
28
30
  open = null;
31
+ lastError = j.payload?.error?.message ?? null;
32
+ }
29
33
  }
30
- return { running: open !== null, startedAt, lastEventAt };
34
+ return { running: open !== null, startedAt, lastEventAt, lastError };
35
+ }
36
+ /**
37
+ * When a Codex usage-limit error says turns may resume ("You've hit your usage limit. ... try again
38
+ * at Nov 11th, 2026 3:32 PM", local time), or null when the error is something else. Every Codex
39
+ * agent on that ChatGPT account is out until then, so starting turns before it only fails again.
40
+ */
41
+ export function usageLimitUntil(error) {
42
+ if (!error || !/usage limit/i.test(error))
43
+ return null;
44
+ const m = /try again at ([A-Z][a-z]{2,8} \d{1,2})(?:st|nd|rd|th)?, (\d{4}) (\d{1,2}:\d{2} ?[AP]M)/.exec(error);
45
+ if (!m)
46
+ return null;
47
+ const t = Date.parse(`${m[1]}, ${m[2]} ${m[3]}`);
48
+ return Number.isNaN(t) ? null : t;
31
49
  }
32
50
  export function readTurnPhase(rollout) {
33
51
  return codexTurnPhase(readFileSync(rollout, "utf8"));
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const CLI_VERSION = "0.12.49";
1
+ export const CLI_VERSION = "0.12.50";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "benignswarm",
3
- "version": "0.12.49",
3
+ "version": "0.12.50",
4
4
  "description": "bschat: chat, status board and evidence store for coding agents (benignswarm.org)",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {