neurastream 0.2.77 → 0.2.79

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neurastream",
3
- "version": "0.2.77",
3
+ "version": "0.2.79",
4
4
  "description": "Guardrails for running many AI coding agents at once: plan queue, blockers, call budgets, provider caps, scoring, merge-verified close.",
5
5
  "keywords": [
6
6
  "ai-agents",
@@ -50,6 +50,7 @@ import { packageJsonPath, packageVersion } from "./package-root.ts";
50
50
  import * as q from "../../core/queue/lib.ts";
51
51
  import { pinModel, providerOfModel, recordOut, resetEpochFromText } from "../../core/provider-state.ts";
52
52
  import { annotateResetTimes } from "../../core/reset-time.ts";
53
+ import { tryLock, type HeldLock } from "../../core/file-lock.ts";
53
54
  // Registers the plugin-command matcher into core's writeTargets (side effect).
54
55
  import "./package-ops.ts";
55
56
 
@@ -630,6 +631,10 @@ export function deliverOwnerInbox(pi: ExtensionHost, cfg: Config): void {
630
631
 
631
632
  /** The most recently registered createOmpAdapter cleanup, for the exported reset(). */
632
633
  let activeAdapterReset: (() => void) | null = null;
634
+ /** This process's held Main session lock (plan 20261103_1146 P2): the first main session_start
635
+ * locks `<queue_state>/mains/<id>/session.lock` for the process lifetime — the kernel frees it
636
+ * when the process dies, so no release bookkeeping on any shutdown path. */
637
+ let heldSessionLock: HeldLock | null = null;
633
638
  /** The limit tick: one check of the open episode every 30 s (plan 20261027_1500 P3). */
634
639
  const LIMIT_TICK_MS = 30_000;
635
640
  /** The probe's command line and env. Never process.execPath: inside the harness that is the omp
@@ -891,6 +896,22 @@ export function createOmpAdapter(pi: ExtensionHost, initial: Config, freshConfig
891
896
  const c = asCtx(ctx);
892
897
  // Original: proceed only when no agent is set or it is the main session.
893
898
  if (c.hasAgent && c.kind !== "main") return;
899
+ // One live Main session per id (plan 20261103_1146 P2): the first session_start in this
900
+ // process locks mains/<id>/session.lock for the process lifetime. A second session_start
901
+ // in the SAME process is the rollover fallback re-entering (newSession) — its own lock
902
+ // reads as fine. Busy means a DIFFERENT process holds it: another live Main with this id.
903
+ if (heldSessionLock === null) {
904
+ const held = tryLock(join(mainStateDir(cfg), "session.lock"));
905
+ if (!held) {
906
+ const id = mainId();
907
+ adapterLog(cfg, `second Main ${id} refused: a Main session with this id is live`);
908
+ console.error(`Main ${id} is already running`);
909
+ // omp defers shutdown (:1328-1332), so a clean exit would end 0 and the refusal
910
+ // would look like success: end directly, before anything arms.
911
+ process.exit(1);
912
+ }
913
+ heldSessionLock = held;
914
+ }
894
915
  autoClose(cfg);
895
916
  if (timer) return;
896
917
  const every = cfg.queue.auto_close_min * 60_000;
@@ -1401,6 +1422,8 @@ export function createOmpAdapter(pi: ExtensionHost, initial: Config, freshConfig
1401
1422
  activeAdapterReset = (): void => {
1402
1423
  clearRolloverState();
1403
1424
  stopLimitTick();
1425
+ heldSessionLock?.release();
1426
+ heldSessionLock = null;
1404
1427
  };
1405
1428
  // The launcher restarts omp: the request file carries the note and the editor draft, and omp
1406
1429
  // shuts down only after the file is written. A failed write keeps the session running.
@@ -2,7 +2,7 @@ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
2
2
  import { basename, dirname, join } from "node:path";
3
3
  import type { Config } from "../../core/config.ts";
4
4
  import type { MainSession, Run, RunMessage, RunSource, RunStatus, TaskResult } from "../../core/adapter.ts";
5
- import { nowSec } from "../../core/clock.ts";
5
+ import { bootSec, nowSec } from "../../core/clock.ts";
6
6
  import { readBudgets } from "../../core/guards.ts";
7
7
  import { pyDumps } from "../../core/pyjson.ts";
8
8
 
@@ -249,6 +249,15 @@ function build(file: string, cfg: Config, mtime: number, agentsDir: string): Run
249
249
  state = "DIED: never replied";
250
250
  neverReplied = true;
251
251
  }
252
+ // A session last written before the machine's boot time cannot be alive: the reboot killed it
253
+ // (INCIDENT I-20261103-81). Main's own rows keep the idle bound; a crash without a reboot
254
+ // (no boot-time evidence) is out of scope.
255
+ let bootDead = false;
256
+ const boot = bootSec();
257
+ if (!id.startsWith("MAIN") && !exited && !err && !done && boot !== null && mtime < boot) {
258
+ state = "DIED: killed before the last boot";
259
+ bootDead = true;
260
+ }
252
261
  if ((state.startsWith("RUNNING") || state.startsWith("WAITING")) && !id.startsWith("MAIN")) {
253
262
  const cap = readBudgets(cfg.paths.queue_state)[id] ?? cfg.guards.sub_max_calls;
254
263
  if (calls > cap) state += ` OVER-BUDGET(${calls}>${cap} calls)`;
@@ -278,7 +287,7 @@ function build(file: string, cfg: Config, mtime: number, agentsDir: string): Run
278
287
  scoreModel,
279
288
  scoreCost,
280
289
  scoreHours: ((last ?? 0) - (first ?? 0)) / 3600,
281
- scoreRunning: neverReplied ? false : !exited && !scoreErr && !done,
290
+ scoreRunning: neverReplied || bootDead ? false : !exited && !scoreErr && !done,
282
291
  scoreSurvived: neverReplied ? false : done && !scoreErr,
283
292
  scoreEnd: scoreErr || (done ? "done" : "no final message"),
284
293
  };
package/src/core/clock.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  // Wall clock. Tests pin NEURASTREAM_NOW (unix seconds) so output matches a frozen original.
2
2
 
3
+ import { readFileSync } from "node:fs";
4
+
3
5
  export function nowSec(): number {
4
6
  const raw = process.env.NEURASTREAM_NOW;
5
7
  if (raw !== undefined && raw !== "") {
@@ -9,6 +11,23 @@ export function nowSec(): number {
9
11
  return Date.now() / 1000;
10
12
  }
11
13
 
14
+ // Boot time of the machine (unix seconds): NEURASTREAM_BOOT pins it for tests, as NEURASTREAM_NOW
15
+ // pins nowSec; else the kernel's btime line. null when neither is readable (non-Linux): no boot
16
+ // rule then.
17
+ export function bootSec(): number | null {
18
+ const raw = process.env.NEURASTREAM_BOOT;
19
+ if (raw !== undefined && raw !== "") {
20
+ const n = Number(raw);
21
+ if (Number.isFinite(n)) return n;
22
+ }
23
+ try {
24
+ const m = /^btime (\d+)$/m.exec(readFileSync("/proc/stat", "utf8"));
25
+ return m ? Number(m[1]) : null;
26
+ } catch {
27
+ return null;
28
+ }
29
+ }
30
+
12
31
  export function strftime(epoch: number, fmt: string): string {
13
32
  const d = new Date(epoch * 1000);
14
33
  const p = (n: number) => String(n).padStart(2, "0");
@@ -2425,8 +2425,9 @@ export function checkStop(e: StopEvent, cfg: Config): Decision {
2425
2425
  }
2426
2426
  }
2427
2427
  if (open.length === 0) return { ...ALLOW, state };
2428
- if (liveSubagents(e.ledgerText).length > 0) {
2429
- log(cfg.paths.guard_log, "ALLOW stop: live subagents");
2428
+ const live = liveSubagents(e.ledgerText);
2429
+ if (live.length > 0) {
2430
+ log(cfg.paths.guard_log, `ALLOW stop: live subagents: ${live.join(", ")}`);
2430
2431
  return { ...ALLOW, state };
2431
2432
  }
2432
2433
  if (state.callsSinceStop === 0) state.idleStops++;