neurastream 0.1.13 → 0.1.15
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/README.md +2 -0
- package/package.json +1 -1
- package/src/adapters/omp/index.ts +75 -21
- package/src/adapters/omp/sessions.ts +1 -1
- package/src/core/metrics.ts +5 -2
- package/src/core/rollover.ts +22 -7
package/README.md
CHANGED
|
@@ -102,6 +102,8 @@ A relaunch keeps the flags of the first run (`--model`, `--session-dir`, …) an
|
|
|
102
102
|
In both modes:
|
|
103
103
|
|
|
104
104
|
- A note counts only if it was written after the threshold was crossed and is at most one hour old.
|
|
105
|
+
- The running plugin going stale counts as over the threshold too: at every check the adapter re-reads the `package.json` it was loaded from, and a different (or unreadable) version asks for the handoff note even below `rollover.max_session_mb`, naming both versions (`NEURASTREAM_PACKAGE_JSON` overrides the path it watches).
|
|
106
|
+
- While a rollover is pending (no fresh note, or own jobs still running), Main cannot start new subagents: a `task` call is refused until the running jobs finish and the session rolls over.
|
|
105
107
|
- While own jobs (subagents included) are still running, the switch waits: a rollover would cancel them.
|
|
106
108
|
- `/main-rollover <message>` does the same by hand. It refuses below the threshold, without a fresh note, or while jobs run.
|
|
107
109
|
- `/main-rollover` shows a notice at once and gathers `main-start` in the background, so omp keeps repainting and reading keys until the new session opens.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "neurastream",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.15",
|
|
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": ["ai-agents", "coding-agents", "orchestration", "guardrails", "omp", "bun", "cli"],
|
|
6
6
|
"homepage": "https://github.com/umiddey/neurastream#readme",
|
|
@@ -17,13 +17,14 @@
|
|
|
17
17
|
|
|
18
18
|
import { execFileSync, spawn } from "node:child_process";
|
|
19
19
|
import { appendFileSync, mkdirSync, openSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
20
|
-
import { dirname, join } from "node:path";
|
|
20
|
+
import { dirname, join, resolve } from "node:path";
|
|
21
|
+
import { fileURLToPath } from "node:url";
|
|
21
22
|
import type { Decision } from "../../core/adapter.ts";
|
|
22
23
|
import { loadConfig, type Config } from "../../core/config.ts";
|
|
23
24
|
import { checkInject, checkPlanEdit, checkStop, checkTool, MAIN_PLAN_FILE, recordMainResult } from "../../core/guards.ts";
|
|
24
25
|
import { applyQueueEnv } from "../../core/queue/env.ts";
|
|
25
26
|
import { mainStartText } from "../../core/main-start.ts";
|
|
26
|
-
import { buildHandoff, holdsBlock, notePath, noteRequest, relaunchPath, type RolloverState
|
|
27
|
+
import { buildHandoff, holdsBlock, notePath, noteRequest, relaunchPath, rolloverState, spawnRefusal, type RolloverState } from "../../core/rollover.ts";
|
|
27
28
|
import * as q from "../../core/queue/lib.ts";
|
|
28
29
|
// Registers the plugin-command matcher into core's writeTargets (side effect).
|
|
29
30
|
import "./package-ops.ts";
|
|
@@ -80,6 +81,22 @@ export function toExtensionResult(d: Decision): Record<string, unknown> {
|
|
|
80
81
|
// A push or merge by Main may have landed a completed plan: try closing right away.
|
|
81
82
|
const PUSH_MERGE = /\bgit\b[^\n;|&]*\bpush\b|\b(glab\s+mr|gh\s+pr)\s+merge\b/;
|
|
82
83
|
|
|
84
|
+
// The installed copy this plugin comes from (computed as in doctor.ts) and the package.json whose
|
|
85
|
+
// version decides staleness. omp never reloads a plugin, so the version is read once at adapter
|
|
86
|
+
// start (the code actually running) and re-read at every rollover check; a different or unreadable
|
|
87
|
+
// version means the running plugin is stale. NEURASTREAM_PACKAGE_JSON overrides the path (tests
|
|
88
|
+
// and the sandbox smoke point it at a copy whose version changes mid-run), like QUEUE_CLOSE_BIN.
|
|
89
|
+
const PACKAGE_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../../..");
|
|
90
|
+
|
|
91
|
+
function packageVersion(packageJson: string): string | null {
|
|
92
|
+
try {
|
|
93
|
+
const version = asRecord(JSON.parse(readFileSync(packageJson, "utf8"))).version;
|
|
94
|
+
return typeof version === "string" ? version : null;
|
|
95
|
+
} catch {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
83
100
|
export function adapterLog(cfg: Config, line: string): void {
|
|
84
101
|
try {
|
|
85
102
|
appendFileSync(cfg.paths.guard_log, `${new Date().toISOString()} ${line}\n`);
|
|
@@ -298,6 +315,19 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
|
|
|
298
315
|
const cwd = c.cwd ?? process.cwd();
|
|
299
316
|
const snap = new Map<string, string>();
|
|
300
317
|
snapshotPlans(tool, input, cwd, snap, !isSub);
|
|
318
|
+
// Drain (src/core/rollover.ts): while a rollover is pending no new subagent starts; the
|
|
319
|
+
// running ones finish first. Subagents' own tool calls are unaffected.
|
|
320
|
+
if (!isSub && tool === "task" && cfg.rollover.enabled) {
|
|
321
|
+
const rc = asRolloverCtx(ctx);
|
|
322
|
+
if (rc) {
|
|
323
|
+
const why = spawnRefusal(rolloverSnapshot(rc).state, rc.runningJobs());
|
|
324
|
+
if (why) {
|
|
325
|
+
const tasks = Array.isArray(input.tasks) ? input.tasks : [];
|
|
326
|
+
adapterLog(cfg, `BLOCK spawn ${String(asRecord(tasks[0]).agent ?? "task")}: rollover pending`);
|
|
327
|
+
return { block: true, reason: why };
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
301
331
|
const d = checkTool(
|
|
302
332
|
{ tool, input, cwd, agent, agentId: id, calls: n - 1, paths: [], writes: [], command: asString(input.command) },
|
|
303
333
|
cfg,
|
|
@@ -355,25 +385,44 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
|
|
|
355
385
|
// Session rollover (src/core/rollover.ts). Main's session size decides when a handoff note is
|
|
356
386
|
// asked for at turn end (a sendMessage that triggers Main's next turn). The owner's next message
|
|
357
387
|
// then starts a fresh session from that note; under `neurastream main` the launcher starts it at
|
|
358
|
-
// once. The size
|
|
388
|
+
// once. The size and the installed version are re-read at every turn end and tool result; the
|
|
389
|
+
// note request goes out once per session file and note state.
|
|
390
|
+
// The version this process loaded at start: the plugin code that is actually running.
|
|
391
|
+
const loadedVersion = packageVersion(process.env.NEURASTREAM_PACKAGE_JSON ?? join(PACKAGE_ROOT, "package.json"));
|
|
359
392
|
let crossed: { file: string; atMs: number } | null = null;
|
|
393
|
+
// The note request already sent, keyed by session file + note mtime at send time ("none" when
|
|
394
|
+
// no note exists): a note that expires while jobs run falls back to need-note with a key no
|
|
395
|
+
// request was sent for, so it gets exactly one new request and never a loop.
|
|
360
396
|
let asked: string | null = null;
|
|
361
397
|
let waitNoticeMs = 0;
|
|
362
398
|
|
|
363
|
-
const
|
|
399
|
+
const rolloverSnapshot = (c: RolloverCtx): { state: RolloverState; noteMtimeMs: number | null; stale: boolean; liveVersion: string | null } => {
|
|
364
400
|
const nowMs = Date.now();
|
|
365
401
|
const bytes = sizeOf(c.sessionFile);
|
|
366
402
|
const maxMb = cfg.rollover.max_session_mb;
|
|
367
|
-
|
|
403
|
+
const noteMtimeMs = mtimeOf(notePath(c.sessionId));
|
|
404
|
+
const liveVersion = packageVersion(process.env.NEURASTREAM_PACKAGE_JSON ?? join(PACKAGE_ROOT, "package.json"));
|
|
405
|
+
const stale = liveVersion === null || loadedVersion === null || liveVersion !== loadedVersion;
|
|
406
|
+
if (stale) {
|
|
407
|
+
// Note freshness is measured against the crossing: a stale session counts as crossed
|
|
408
|
+
// from the first check that saw it, whatever its size.
|
|
409
|
+
if (crossed?.file !== c.sessionFile) crossed = { file: c.sessionFile, atMs: nowMs };
|
|
410
|
+
} else if (bytes < maxMb * 1024 * 1024) crossed = null;
|
|
368
411
|
else if (crossed?.file !== c.sessionFile) crossed = { file: c.sessionFile, atMs: nowMs };
|
|
369
|
-
return
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
412
|
+
return {
|
|
413
|
+
state: rolloverState({
|
|
414
|
+
sessionBytes: bytes,
|
|
415
|
+
maxMb,
|
|
416
|
+
runningJobs: c.runningJobs(),
|
|
417
|
+
crossedAtMs: crossed?.atMs ?? null,
|
|
418
|
+
noteMtimeMs,
|
|
419
|
+
nowMs,
|
|
420
|
+
stale,
|
|
421
|
+
}),
|
|
422
|
+
noteMtimeMs,
|
|
423
|
+
stale,
|
|
424
|
+
liveVersion,
|
|
425
|
+
};
|
|
377
426
|
};
|
|
378
427
|
|
|
379
428
|
let relaunched = false;
|
|
@@ -398,14 +447,19 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
|
|
|
398
447
|
const onSizeCheck = (_e: unknown, ctx: unknown): void => {
|
|
399
448
|
const c = asRolloverCtx(ctx);
|
|
400
449
|
if (!cfg.rollover.enabled || !c || !isMain(ctx)) return;
|
|
401
|
-
const
|
|
402
|
-
|
|
403
|
-
|
|
450
|
+
const snap = rolloverSnapshot(c);
|
|
451
|
+
const key = `${c.sessionFile}:${snap.noteMtimeMs ?? "none"}`;
|
|
452
|
+
if (snap.state === "need-note" && asked !== key) {
|
|
453
|
+
asked = key;
|
|
454
|
+
// A stale install says so: the request names the version on disk and the one running.
|
|
455
|
+
const reason = snap.stale
|
|
456
|
+
? `neurastream on disk is ${snap.liveVersion ?? "unknown"}, this session runs ${loadedVersion ?? "unknown"}`
|
|
457
|
+
: undefined;
|
|
404
458
|
pi.sendMessage(
|
|
405
|
-
{ customType: "main-guard.rollover", content: noteRequest(notePath(c.sessionId), process.env.NEURASTREAM_MAIN_LAUNCHER === "1"), display: false },
|
|
459
|
+
{ customType: "main-guard.rollover", content: noteRequest(notePath(c.sessionId), process.env.NEURASTREAM_MAIN_LAUNCHER === "1", reason), display: false },
|
|
406
460
|
{ triggerTurn: true, deliverAs: "nextTurn" },
|
|
407
461
|
);
|
|
408
|
-
} else if (state === "ready" && process.env.NEURASTREAM_MAIN_LAUNCHER === "1" && !relaunched) {
|
|
462
|
+
} else if (snap.state === "ready" && process.env.NEURASTREAM_MAIN_LAUNCHER === "1" && !relaunched) {
|
|
409
463
|
requestRelaunch(c, notePath(c.sessionId));
|
|
410
464
|
}
|
|
411
465
|
};
|
|
@@ -428,7 +482,7 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
|
|
|
428
482
|
const rolloverNotice = (ctx: unknown): string | null => {
|
|
429
483
|
const c = asRolloverCtx(ctx);
|
|
430
484
|
if (!cfg.rollover.enabled || !c) return null;
|
|
431
|
-
const state =
|
|
485
|
+
const state = rolloverSnapshot(c).state;
|
|
432
486
|
if (state === "wait-jobs" && Date.now() - waitNoticeMs >= WAIT_NOTICE_MS) {
|
|
433
487
|
waitNoticeMs = Date.now();
|
|
434
488
|
const jobs = c.runningJobs().join(", ");
|
|
@@ -444,7 +498,7 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
|
|
|
444
498
|
const text = asString(e.text);
|
|
445
499
|
if (!cfg.rollover.enabled || !c || !isMain(ctx)) return undefined;
|
|
446
500
|
if (asString(e.source) !== "interactive" || text === "" || text.startsWith("/")) return undefined;
|
|
447
|
-
if (
|
|
501
|
+
if (rolloverSnapshot(c).state !== "ready") return undefined;
|
|
448
502
|
return { text: `/main-rollover ${text}` };
|
|
449
503
|
});
|
|
450
504
|
|
|
@@ -454,7 +508,7 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
|
|
|
454
508
|
const c = asRolloverCtx(ctx);
|
|
455
509
|
if (!c) return;
|
|
456
510
|
const path = notePath(c.sessionId);
|
|
457
|
-
const state =
|
|
511
|
+
const state = rolloverSnapshot(c).state;
|
|
458
512
|
if (state === "below") {
|
|
459
513
|
c.notify("main-rollover: this session is below rollover.max_session_mb; nothing to roll over.");
|
|
460
514
|
return;
|
|
@@ -159,7 +159,7 @@ function build(file: string, cfg: Config, mtime: number): Run | null {
|
|
|
159
159
|
|
|
160
160
|
const done = yielded || final;
|
|
161
161
|
let state: string;
|
|
162
|
-
if (
|
|
162
|
+
if (!id.startsWith("MAIN") && (yielded || final)) state = "done";
|
|
163
163
|
else if (!exited && !err) {
|
|
164
164
|
const idle = Math.floor((nowSec() - mtime) / 60);
|
|
165
165
|
state = (waiting || "RUNNING") + (idle >= cfg.ledger.idle_show_min ? ` (idle ${idle}m)` : "");
|
package/src/core/metrics.ts
CHANGED
|
@@ -218,9 +218,12 @@ export interface GuardBlock {
|
|
|
218
218
|
reason: string;
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
-
/** Groups reasons by shape: paths cut to 40 chars,
|
|
221
|
+
/** Groups reasons by shape: paths cut to 40 chars, digit runs collapsed to N, and every
|
|
222
|
+
* `open: <plan list>` tail collapsed to one key: the lists differ per plan set, the reason is one. */
|
|
222
223
|
function reasonKey(text: string): string {
|
|
223
|
-
|
|
224
|
+
const shaped = text.replace(/\S*\/\S*/g, (p) => (p.length > 40 ? `${p.slice(0, 40)}…` : p)).replace(/\d+/g, "N");
|
|
225
|
+
const at = shaped.indexOf("open: ");
|
|
226
|
+
return at === -1 ? shaped : `${shaped.slice(0, at)}open: <list>`;
|
|
224
227
|
}
|
|
225
228
|
|
|
226
229
|
/** BLOCK lines of one guard log, each `<ISO UTC> BLOCK <reason>`. Other lines are ignored. */
|
package/src/core/rollover.ts
CHANGED
|
@@ -17,20 +17,24 @@ export interface RolloverInput {
|
|
|
17
17
|
maxMb: number;
|
|
18
18
|
/** Labels of the own async jobs (subagents included) that are still running. */
|
|
19
19
|
runningJobs: readonly string[];
|
|
20
|
-
/** When the session file was first seen at or over the threshold; null if never. */
|
|
20
|
+
/** When the session file was first seen at or over the threshold (or stale); null if never. */
|
|
21
21
|
crossedAtMs: number | null;
|
|
22
22
|
/** Mtime of the handoff note; null when the note does not exist. */
|
|
23
23
|
noteMtimeMs: number | null;
|
|
24
|
+
/** The running plugin differs from the installed package (or its version is unreadable). */
|
|
25
|
+
stale: boolean;
|
|
24
26
|
nowMs: number;
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
/**
|
|
28
|
-
* below: under the threshold. need-note: over
|
|
29
|
-
* at most MAX_NOTE_AGE_MS old). wait-jobs: note is fresh but own
|
|
30
|
-
* would cancel them). ready: the next owner message may roll over.
|
|
30
|
+
* below: under the threshold and not stale. need-note: over the threshold (or stale) with no fresh
|
|
31
|
+
* note (written after the crossing, at most MAX_NOTE_AGE_MS old). wait-jobs: note is fresh but own
|
|
32
|
+
* jobs still run (rolling over would cancel them). ready: the next owner message may roll over.
|
|
33
|
+
* A stale session counts like a crossed size threshold: even a small session rolls over, because
|
|
34
|
+
* the running plugin is not the code the owner installed.
|
|
31
35
|
*/
|
|
32
36
|
export function rolloverState(p: RolloverInput): RolloverState {
|
|
33
|
-
if (p.sessionBytes < p.maxMb * 1024 * 1024) return "below";
|
|
37
|
+
if (p.sessionBytes < p.maxMb * 1024 * 1024 && !p.stale) return "below";
|
|
34
38
|
const noteFresh =
|
|
35
39
|
p.noteMtimeMs !== null &&
|
|
36
40
|
p.crossedAtMs !== null &&
|
|
@@ -40,6 +44,16 @@ export function rolloverState(p: RolloverInput): RolloverState {
|
|
|
40
44
|
return p.runningJobs.length > 0 ? "wait-jobs" : "ready";
|
|
41
45
|
}
|
|
42
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Drain: while a rollover is pending (need-note or wait-jobs) Main must not start new subagents —
|
|
49
|
+
* a rollover would cancel them, or they would outlive the handoff. Returns the refusal text, or
|
|
50
|
+
* null when the spawn may proceed (below, ready).
|
|
51
|
+
*/
|
|
52
|
+
export function spawnRefusal(state: RolloverState, runningJobs: readonly string[]): string | null {
|
|
53
|
+
if (state !== "need-note" && state !== "wait-jobs") return null;
|
|
54
|
+
return `main-guard: rollover pending (${state}); no new subagents until the session rolls over. Running: ${runningJobs.join(", ")}.`;
|
|
55
|
+
}
|
|
56
|
+
|
|
43
57
|
export interface HandoffInput {
|
|
44
58
|
/** Output of main-start (the generated state). */
|
|
45
59
|
generated: string;
|
|
@@ -71,10 +85,11 @@ export function holdsBlock(holds: readonly string[]): string {
|
|
|
71
85
|
return ["=== owner holds (plans' WAITING on owner lines)", ...(holds.length > 0 ? holds : ["none"]), ""].join("\n");
|
|
72
86
|
}
|
|
73
87
|
|
|
74
|
-
/** The one-time note instruction for Main's next turn. */
|
|
75
|
-
export function noteRequest(path: string, auto = false): string {
|
|
88
|
+
/** The one-time note instruction for Main's next turn. `reason` (a stale install) names both versions. */
|
|
89
|
+
export function noteRequest(path: string, auto = false, reason = ""): string {
|
|
76
90
|
return [
|
|
77
91
|
"Session rollover: this session has grown past rollover.max_session_mb.",
|
|
92
|
+
...(reason ? [reason] : []),
|
|
78
93
|
`Write a handoff note to ${path} (overwrite it). Cover the work in progress and its state, the decisions made and why, the next step, and the files involved.`,
|
|
79
94
|
"Do not list holds or owner blockers in the note; they come from the plans' WAITING on owner lines.",
|
|
80
95
|
auto
|