neurastream 0.2.76 → 0.2.78

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.76",
3
+ "version": "0.2.78",
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,12 +631,24 @@ 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;
640
+ /** The probe's command line and env. Never process.execPath: inside the harness that is the omp
641
+ * binary, so the child was a full Main session (2026-11-03: stray Mains). bun comes from PATH, as
642
+ * every bin's `#!/usr/bin/env bun` shebang does; the child env drops MAIN_ENV. */
643
+ export function limitProbeSpawn(env: NodeJS.ProcessEnv = process.env): { cmd: string; args: string[]; env: NodeJS.ProcessEnv } {
644
+ const { [MAIN_ENV]: _main, ...rest } = env;
645
+ return { cmd: "nice", args: ["-n", "19", "bun", join(import.meta.dir, "../../cli/limit-probe.ts")], env: rest };
646
+ }
635
647
  /** One limit probe as a niced child (cli/limit-probe.ts). It writes the episode state itself;
636
648
  * a failed start is logged and the next tick tries again. */
637
649
  function spawnLimitProbeChild(cfg: Config): void {
638
- const child = spawn("nice", ["-n", "19", process.execPath, join(import.meta.dir, "../../cli/limit-probe.ts")], { detached: true, stdio: "ignore", env: process.env });
650
+ const p = limitProbeSpawn();
651
+ const child = spawn(p.cmd, p.args, { detached: true, stdio: "ignore", env: p.env });
639
652
  child.on("error", err => adapterLog(cfg, `RATE-LIMIT probe: could not start: ${err.message}`));
640
653
  child.unref();
641
654
  }
@@ -883,6 +896,22 @@ export function createOmpAdapter(pi: ExtensionHost, initial: Config, freshConfig
883
896
  const c = asCtx(ctx);
884
897
  // Original: proceed only when no agent is set or it is the main session.
885
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
+ }
886
915
  autoClose(cfg);
887
916
  if (timer) return;
888
917
  const every = cfg.queue.auto_close_min * 60_000;
@@ -1393,6 +1422,8 @@ export function createOmpAdapter(pi: ExtensionHost, initial: Config, freshConfig
1393
1422
  activeAdapterReset = (): void => {
1394
1423
  clearRolloverState();
1395
1424
  stopLimitTick();
1425
+ heldSessionLock?.release();
1426
+ heldSessionLock = null;
1396
1427
  };
1397
1428
  // The launcher restarts omp: the request file carries the note and the editor draft, and omp
1398
1429
  // shuts down only after the file is written. A failed write keeps the session running.