neurastream 0.2.48 → 0.2.49

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 CHANGED
@@ -31,6 +31,7 @@ flowchart LR
31
31
  | Brief lint (`brief-lint`) | Refuses a helper brief that misses a required rule (acceptance, stop condition, no push, call budget). |
32
32
  | Plan review gate (`review.*`) | Every plan carries a `## Reuse` section (one checked line per new thing — a queue problem without it); no spawn on a plan without a current reviewer PASS, recorded by plan-guard only from a read-only reviewer subagent's `PLAN REVIEW: PASS` Progress edit (`templates/plan-review-brief.md`, `review.agents`) and expired when the plan text changes; and no spawn while the plan holds done-but-not-merged work — open MRs of its repo, branches with commits off main — that the brief does not name with an `MR !N:` / `Branch <name>:` line. brief-lint enforces the brief side: an MR named in prose without the line, a plan named only under a worktree path instead of its main checkout, and `src/tests` files the plan's blocker `ALLOWED` regex rejects are all refused. Plans stamped before `review.since` are exempt until audited. |
33
33
  | Write-time checks (`write_check`) | With `write_check.enabled` on, a subagent's write or edit to a file a configured linter covers is linted before and after the call. If the call adds a lint finding or a suppression comment (`noqa` and similar), the file is put back and the call returns an error. Main is left alone unless `applies_to: all`. Off by default. |
34
+ | Main call cap (`guards.main_max_calls`) | At most 15 tool calls per Main turn — counted since the owner's last message or Main's last hand-back, so answering or delegating wins over grinding. The wrap-up warning comes at `guards.main_warn_calls` (12); the tools in `guards.main_call_exempt_tools` (`task`, `todo`, `ask`, `wait`) are never counted and never refused; `0` switches it off and tier `full` ignores it. |
34
35
  | Machine health (`machine-guard`, `main-start`, `heavy-run`) | Load, free memory, disk and `/tmp` limits; one heavy test run at a time. |
35
36
  | Unlanded work (`wip-check`, `wip-snapshot`, `wip-backup`) | Lists branches on no remote and dirty worktrees; snapshots them as hidden commits; can push them to a backup repo. |
36
37
  | Session rollover | When the main session file passes 5 MB, or the installed neurastream version differs from the running plugin, Main writes a handoff note; a fresh session then starts on its own under `neurastream main`. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neurastream",
3
- "version": "0.2.48",
3
+ "version": "0.2.49",
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",
@@ -26,7 +26,7 @@ import { loadConfig, type Config } from "../../core/config.ts";
26
26
  import { checkWrite, deferSteer, type WriteCheckConfig } from "../../core/write-check.ts";
27
27
  import { nowSec, strftime } from "../../core/clock.ts";
28
28
  import { runDetectors, runDetectorsGated, type DetectorInput } from "../../core/incident-detectors.ts";
29
- import { checkInject, checkPlanEdit, checkStop, checkTool, findingsValue, MAIN_PLAN_FILE, newProgressEntries, planReadNote, recordMainResult, revertMainEdit, rekeySpawnFile } from "../../core/guards.ts";
29
+ import { checkInject, checkPlanEdit, checkStop, checkTool, findingsValue, MAIN_PLAN_FILE, newProgressEntries, planReadNote, recordMainResult, resetMainCalls, revertMainEdit, rekeySpawnFile } from "../../core/guards.ts";
30
30
  import { rateLimitWait } from "../../core/rate-limit.ts";
31
31
  import { notifyProviders, postReply } from "../../core/notify.ts";
32
32
  import { loadKnownSecrets, redactSecrets, redactSummary } from "../../core/secret-redact.ts";
@@ -1340,6 +1340,15 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
1340
1340
  else adapterLog(cfg, `WRITE-CHECK deferred still present: ${steers.join(" | ")}`);
1341
1341
  });
1342
1342
 
1343
+ // Terminal hand-back (plan 20261028_1545 P2): a willContinue agent_end keeps the turn going
1344
+ // (blocked stop, todo/plan-mode continuation, deferred hand-back), so only its absence ends
1345
+ // the turn — the per-turn call budget resets for the next one.
1346
+ pi.on("agent_end", (e, ctx) => {
1347
+ if (!isMain(ctx)) return;
1348
+ if (asRecord(e).willContinue === true) return;
1349
+ resetMainCalls();
1350
+ });
1351
+
1343
1352
  // omp is really shutting down: the exit-grace fallback must not fire.
1344
1353
  pi.on("session_shutdown", () => clearGrace?.());
1345
1354
 
@@ -113,6 +113,12 @@ export interface Config {
113
113
  sub_max_calls: number;
114
114
  /** Highest per-spawn `Call budget: <N>` a task brief may set. */
115
115
  sub_max_calls_cap: number;
116
+ /** Main tool calls per turn that draw the wrap-up warning (plan 20261028_1545 P1). */
117
+ main_warn_calls: number;
118
+ /** Main tool calls per turn before refusal; 0 = off. */
119
+ main_max_calls: number;
120
+ /** Tools the per-turn call cap never counts and never refuses: the ways out of a turn. */
121
+ main_call_exempt_tools: string[];
116
122
  main_edit_limit: number;
117
123
  main_window_min: number;
118
124
  unlock_minutes: number;
@@ -446,6 +452,10 @@ function check(cfg: Config): void {
446
452
  if (cfg.guards.sub_warn_calls > cfg.guards.sub_max_calls) {
447
453
  throw new ConfigError("guards.sub_warn_calls must be <= guards.sub_max_calls");
448
454
  }
455
+ // 0 = off: the warn band has no cap to stay under, so the default warn loads untouched.
456
+ if (cfg.guards.main_max_calls > 0 && cfg.guards.main_warn_calls > cfg.guards.main_max_calls) {
457
+ throw new ConfigError("guards.main_warn_calls must be <= guards.main_max_calls");
458
+ }
449
459
  if (cfg.guards.sub_max_calls_cap < cfg.guards.sub_max_calls) {
450
460
  throw new ConfigError("guards.sub_max_calls_cap must be >= guards.sub_max_calls");
451
461
  }
@@ -74,6 +74,8 @@ export const CANCEL_TODO =
74
74
  interface EditWindow {
75
75
  used: string[];
76
76
  windowStart: number;
77
+ /** Main tool calls since the last owner prompt or terminal hand-back (main_call_exempt_tools never count). */
78
+ mainCalls: number;
77
79
  }
78
80
 
79
81
  function windowFile(): string {
@@ -82,10 +84,11 @@ function windowFile(): string {
82
84
 
83
85
  export function readWindow(): EditWindow {
84
86
  try {
85
- const w = JSON.parse(readFileSync(windowFile(), "utf8")) as EditWindow;
86
- if (Array.isArray(w.used) && typeof w.windowStart === "number") return w;
87
+ const w = JSON.parse(readFileSync(windowFile(), "utf8")) as Partial<EditWindow>;
88
+ if (Array.isArray(w.used) && typeof w.windowStart === "number")
89
+ return { used: w.used, windowStart: w.windowStart, mainCalls: typeof w.mainCalls === "number" ? w.mainCalls : 0 };
87
90
  } catch {}
88
- return { used: [], windowStart: 0 };
91
+ return { used: [], windowStart: 0, mainCalls: 0 };
89
92
  }
90
93
 
91
94
  export function writeWindow(w: EditWindow): void {
@@ -997,6 +1000,47 @@ function mainTool(e: ToolEvent, input: Record<string, unknown>, cfg: Config): De
997
1000
  return editWindow(e, input, cfg);
998
1001
  }
999
1002
 
1003
+ // Main's per-turn call cap (plan 20261028_1545 P2): the sub call-cap shape — warn at
1004
+ // main_warn_calls, hard stop at main_max_calls — over calls counted in the window state file
1005
+ // since the last real owner prompt or Main's terminal hand-back. The file, not the adapter's
1006
+ // session memory, is the counter: one-shot CLI calls and a reloaded plugin must see the same
1007
+ // count, and the reset points (checkInject here, the adapter's terminal agent_end) already write
1008
+ // this file. Off in tier full (same gate as editWindow) and at main_max_calls 0.
1009
+
1010
+ /** The per-turn call cap applies to this call: on, not tier full, and the tool is not exempt. */
1011
+ function mainCallApplies(e: ToolEvent, cfg: Config): boolean {
1012
+ return cfg.guards.main_max_calls > 0 && currentTier(cfg) !== "full" && !cfg.guards.main_call_exempt_tools.includes(e.tool);
1013
+ }
1014
+
1015
+ /** Refusal once this turn has spent main_max_calls, or null. */
1016
+ function mainCallStop(e: ToolEvent, cfg: Config): string | null {
1017
+ if (!mainCallApplies(e, cfg)) return null;
1018
+ const w = readWindow();
1019
+ if (w.mainCalls >= cfg.guards.main_max_calls)
1020
+ return `main-guard: call ${w.mainCalls}/${cfg.guards.main_max_calls} since the owner's last message or your last hand-back (guards.main_max_calls). End your turn with the answer, or spawn an implementer.`;
1021
+ return null;
1022
+ }
1023
+
1024
+ /** Count one allowed, non-exempt call; returns the warn-band inject at main_warn_calls, "" below it. */
1025
+ function mainCallWarn(e: ToolEvent, cfg: Config): string {
1026
+ if (!mainCallApplies(e, cfg)) return "";
1027
+ const w = readWindow();
1028
+ w.mainCalls += 1;
1029
+ writeWindow(w);
1030
+ if (w.mainCalls >= cfg.guards.main_warn_calls)
1031
+ return `main-guard: call ${w.mainCalls}/${cfg.guards.main_max_calls} this turn (guards.main_warn_calls). End your turn with the answer, or spawn an implementer.`;
1032
+ return "";
1033
+ }
1034
+
1035
+ /** Main's terminal hand-back (the adapter's agent_end without willContinue): the turn is over, so
1036
+ * the next one starts at 0 calls. The edit window keeps its own reset rules. */
1037
+ export function resetMainCalls(): void {
1038
+ const w = readWindow();
1039
+ if (w.mainCalls === 0) return;
1040
+ w.mainCalls = 0;
1041
+ writeWindow(w);
1042
+ }
1043
+
1000
1044
  /** A live plan file (archived plans excluded); Main's plan-text checks run on these. */
1001
1045
  export const MAIN_PLAN_FILE = /\/docs\/plans\/(?!archive\/)[^/]+\.md$/;
1002
1046
 
@@ -1052,10 +1096,17 @@ export function checkTool(e: ToolEvent, cfg: Config): Decision {
1052
1096
  log(cfg.paths.plan_guard_log, `BLOCK main tool=${e.tool} shell write to plan ${shellPlan}`);
1053
1097
  return deny(`plan-guard: ${shellPlan} is a plan file: change it with the edit or write tool (close or cancel it with queue-close / queue-cancel).`);
1054
1098
  }
1099
+ const stop = mainCallStop(e, cfg);
1100
+ if (stop) {
1101
+ log(cfg.paths.guard_log, `BLOCK tool=${e.tool} after ${readWindow().mainCalls} calls this turn`);
1102
+ return deny(stop);
1103
+ }
1055
1104
  const d = mainTool(e, input, cfg);
1105
+ if (!d.allow) return d;
1106
+ const warn = mainCallWarn(e, cfg);
1056
1107
  // A task brief names paths it does not read; only the other tools' paths count as reads.
1057
- if (d.allow && e.tool !== "task") recordMainReads(paths.filter(p => !writes.includes(p)));
1058
- return d;
1108
+ if (e.tool !== "task") recordMainReads(paths.filter(p => !writes.includes(p)));
1109
+ return warn ? { ...d, inject: d.inject ? `${d.inject}\n${warn}` : warn } : d;
1059
1110
  }
1060
1111
  const cap = subCallCap(e.agentId, cfg);
1061
1112
  const why = subRules(e, input, paths, writes, cfg, cap);
@@ -1242,7 +1293,7 @@ export function flagCancel(text: string, cfg: Config): string {
1242
1293
  export function checkInject(e: InjectEvent, cfg: Config): Decision {
1243
1294
  if (e.agent !== "main") return ALLOW;
1244
1295
  const continuation = e.inject.trimStart().startsWith("main-guard:");
1245
- if (!continuation) writeWindow({ used: [], windowStart: 0 });
1296
+ if (!continuation) writeWindow({ used: [], windowStart: 0, mainCalls: 0 });
1246
1297
  recordOwnerMessage(e.inject, cfg);
1247
1298
  let summary: string;
1248
1299
  try {
@@ -34,4 +34,4 @@ You are Main, the orchestrator. Main plans, delegates, reviews and lands; implem
34
34
 
35
35
  Guard files are locked for every agent; the owner unlocks with `touch {{AGENT_DIR}}/.guard-unlock` (30 min), or — when `guards.chat_unlock` is on in the config — by typing `unlock 30` in chat (`lock` closes it again).
36
36
 
37
- Autonomy tiers: the owner sets how much Main does unattended with one word — `tier strict` (edit cap `autonomy.strict_edit_limit`; every `git push`, `glab mr merge` or `gh pr merge` also waits until the owner types the approve phrase from `autonomy.go_phrase` (default `go push`; a bare `go` is an ordinary message), which approves exactly one landing for `autonomy.go_minutes` minutes), `tier opinionated` (the limits above), or `tier full` (no edit cap, and a `Priority skip: <plan> — Main: "<reason>"` line with a reason of at least four words may skip an idle higher plan). Pushes stay owner-ruled in strict and opinionated as above; whatever the tier, guard files, the phone page, notify secrets and the unlock stay owner-only.
37
+ Autonomy tiers: the owner sets how much Main does unattended with one word — `tier strict` (edit cap `autonomy.strict_edit_limit`, at most `guards.main_max_calls` tool calls per turn; every `git push`, `glab mr merge` or `gh pr merge` also waits until the owner types the approve phrase from `autonomy.go_phrase` (default `go push`; a bare `go` is an ordinary message), which approves exactly one landing for `autonomy.go_minutes` minutes), `tier opinionated` (the limits above), or `tier full` (no edit cap or call cap, and a `Priority skip: <plan> — Main: "<reason>"` line with a reason of at least four words may skip an idle higher plan). Pushes stay owner-ruled in strict and opinionated as above; whatever the tier, guard files, the phone page, notify secrets and the unlock stay owner-only.
@@ -116,6 +116,9 @@ guards:
116
116
  sub_warn_calls: 90
117
117
  sub_max_calls: 100
118
118
  sub_max_calls_cap: 200
119
+ main_warn_calls: 12 # Main tool calls per turn that draw the wrap-up warning
120
+ main_max_calls: 15 # Main tool calls per turn before refusal; 0 = off
121
+ main_call_exempt_tools: [task, todo, ask, wait] # never counted, never refused at the cap: delegating, tracking and asking stay open
119
122
  main_edit_limit: 5
120
123
  main_window_min: 15
121
124
  unlock_minutes: 30