neurastream 0.3.22 → 0.3.23
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
|
@@ -47,6 +47,7 @@ import { registerChatCommands } from "./chat-commands.ts";
|
|
|
47
47
|
import { CLAIM_BEAT_MS, deliverOwnerInbox, OWNER_INBOX_POLL_MS, ownerInboxCursorPath, ownerInboxMessage, pendingInboxRecords, pressureTick, seedInboxFloor } from "./inbox.ts";
|
|
48
48
|
import { adapterLog, MAIN_ENV } from "./launch.ts";
|
|
49
49
|
// Registers the plugin-command matcher into core's writeTargets (side effect).
|
|
50
|
+
import { createLimitWake } from "./limit-wake.ts";
|
|
50
51
|
import "./package-ops.ts";
|
|
51
52
|
import { createRateLimitController, limitProbeSpawn, recordProviderOutFromRateLimit, recordProviderOutFromTask } from "./rate-limit-controller.ts";
|
|
52
53
|
import { createRolloverController } from "./rollover-controller.ts";
|
|
@@ -272,6 +273,7 @@ export function createOmpAdapter(pi: ExtensionHost, initial: Config, freshConfig
|
|
|
272
273
|
const mainRules = renderBlock(cfg);
|
|
273
274
|
|
|
274
275
|
const rateLimit = createRateLimitController(pi, getCfg);
|
|
276
|
+
const limitWake = createLimitWake(pi, getCfg);
|
|
275
277
|
const rollover = createRolloverController(pi, getCfg, { doctor: opts.doctor });
|
|
276
278
|
|
|
277
279
|
// Clear the previous adapter's armed grace timer before this one overwrites the pointer (P3c).
|
|
@@ -281,6 +283,7 @@ export function createOmpAdapter(pi: ExtensionHost, initial: Config, freshConfig
|
|
|
281
283
|
activeAdapterReset = (): void => {
|
|
282
284
|
rollover.clearState();
|
|
283
285
|
rateLimit.stopTick();
|
|
286
|
+
limitWake.cancel();
|
|
284
287
|
};
|
|
285
288
|
|
|
286
289
|
let callsSinceStop = 0;
|
|
@@ -528,6 +531,7 @@ export function createOmpAdapter(pi: ExtensionHost, initial: Config, freshConfig
|
|
|
528
531
|
return content ? { message: { customType: "main-guard.rules", content, display: false } } : undefined;
|
|
529
532
|
}
|
|
530
533
|
rateLimit.onOwnerPrompt();
|
|
534
|
+
limitWake.cancel();
|
|
531
535
|
const d = checkInject({ inject: prompt, agent: "main" }, cfg);
|
|
532
536
|
callsSinceStop = d.state?.callsSinceStop ?? callsSinceStop;
|
|
533
537
|
idleStops = d.state?.idleStops ?? idleStops;
|
|
@@ -829,6 +833,7 @@ export function createOmpAdapter(pi: ExtensionHost, initial: Config, freshConfig
|
|
|
829
833
|
idleStops = d.state?.idleStops ?? idleStops;
|
|
830
834
|
if (d.allow) {
|
|
831
835
|
recordTurnAnswer(reply);
|
|
836
|
+
limitWake.arm(asCtx(ctx));
|
|
832
837
|
notifyHandback(reply, "allowed session_stop");
|
|
833
838
|
return undefined;
|
|
834
839
|
}
|
|
@@ -844,7 +849,7 @@ export function createOmpAdapter(pi: ExtensionHost, initial: Config, freshConfig
|
|
|
844
849
|
// posts from the event's own messages. omp also sets willContinue after a BLOCKED stop
|
|
845
850
|
// (sessionStopWillContinue) and for todo/plan-mode/rewind continuations, so a post here needs
|
|
846
851
|
// both: no blocked stop just decided (the flag), and an async job really pending. A terminal
|
|
847
|
-
// end (willContinue unset) is not a hand-back:
|
|
852
|
+
// end (willContinue unset) is not a hand-back: session_stop runs first and has already decided. A
|
|
848
853
|
// mid-run continuation — tool calls, or a non-"stop" reason such as an auto-retry — posts
|
|
849
854
|
// nothing.
|
|
850
855
|
const onDeferredHandback = (e: unknown, ctx: unknown): void => {
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Main's limit wake (plan 20261127_1005 P2). When a counted Main call cap or edit window resets
|
|
2
|
+
// while plans are still open, Main gets ONE continuation at the reset instead of sitting parked
|
|
3
|
+
// until the owner speaks. The wake lives in memory only: a restart or reload drops it, and the
|
|
4
|
+
// next owner prompt or stop re-evaluates the limits anyway.
|
|
5
|
+
|
|
6
|
+
import { currentTier } from "../../core/autonomy.ts";
|
|
7
|
+
import type { Config } from "../../core/config.ts";
|
|
8
|
+
import { editWindowEndMs, mainCallsCapOn, mainCallsResetMs, mainEditLimit, openQueue, readWindow } from "../../core/guards.ts";
|
|
9
|
+
import { adapterLog } from "./launch.ts";
|
|
10
|
+
import { LIMIT_TICK_MS } from "./rate-limit-controller.ts";
|
|
11
|
+
import type { ExtensionHost, HostCtx } from "./types.ts";
|
|
12
|
+
|
|
13
|
+
const WAKE_TEXT = "main-guard: the call/edit limit has reset — continue the queue where you stopped.";
|
|
14
|
+
|
|
15
|
+
export interface LimitWake {
|
|
16
|
+
/** At session_stop's allow: one tick at the latest reset still holding Main, when plans are open. */
|
|
17
|
+
arm(c: HostCtx): void;
|
|
18
|
+
/** An owner prompt, or a reset of the adapter, drops the wake. */
|
|
19
|
+
cancel(): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The latest reset (ms since epoch) of a limit still holding Main at `now`; null when none does. */
|
|
23
|
+
function pendingResetMs(cfg: Config, now: number): number | null {
|
|
24
|
+
const w = readWindow(cfg.paths);
|
|
25
|
+
const tier = currentTier(cfg);
|
|
26
|
+
const resets: number[] = [];
|
|
27
|
+
const calls = mainCallsResetMs(w);
|
|
28
|
+
if (mainCallsCapOn(cfg, tier) && w.mainCalls >= cfg.guards.main_max_calls && calls !== null && calls > now) resets.push(calls);
|
|
29
|
+
const edits = editWindowEndMs(w, cfg);
|
|
30
|
+
if (w.used.length >= mainEditLimit(cfg, tier) && edits > now) resets.push(edits);
|
|
31
|
+
return resets.length > 0 ? Math.max(...resets) : null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Plans still open; none when the queue cannot be read (logged: the stop path reports that itself). */
|
|
35
|
+
function openPlans(cfg: Config): string[] {
|
|
36
|
+
try {
|
|
37
|
+
return openQueue(cfg.paths, cfg.review.agents);
|
|
38
|
+
} catch (err) {
|
|
39
|
+
adapterLog(cfg, `LIMIT-WAKE skipped: queue unreadable: ${err}`);
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function createLimitWake(pi: ExtensionHost, getCfg: () => Config): LimitWake {
|
|
45
|
+
// The arm snapshots the reset time; the tick only compares the clock with it and never re-reads
|
|
46
|
+
// the window, because the terminal hand-back zeroes mainCalls right after session_stop.
|
|
47
|
+
let wakeAt: number | null = null;
|
|
48
|
+
let tick: NodeJS.Timeout | null = null;
|
|
49
|
+
|
|
50
|
+
const cancel = (): void => {
|
|
51
|
+
wakeAt = null;
|
|
52
|
+
if (tick !== null) {
|
|
53
|
+
clearInterval(tick);
|
|
54
|
+
tick = null;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const fire = (): void => {
|
|
59
|
+
if (wakeAt === null || Date.now() < wakeAt) return;
|
|
60
|
+
const cfg = getCfg();
|
|
61
|
+
cancel();
|
|
62
|
+
if (openPlans(cfg).length === 0) {
|
|
63
|
+
adapterLog(cfg, "LIMIT-WAKE none: the limit reset with no open plan");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
adapterLog(cfg, "LIMIT-WAKE sent: the limit reset with plans open");
|
|
67
|
+
pi.sendUserMessage(WAKE_TEXT);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
arm(c) {
|
|
72
|
+
cancel();
|
|
73
|
+
const cfg = getCfg();
|
|
74
|
+
const reset = pendingResetMs(cfg, Date.now());
|
|
75
|
+
if (reset === null || openPlans(cfg).length === 0) return;
|
|
76
|
+
wakeAt = reset;
|
|
77
|
+
const t: unknown = c.setInterval ? c.setInterval(fire, LIMIT_TICK_MS) : setInterval(fire, LIMIT_TICK_MS);
|
|
78
|
+
if (t && typeof t === "object" && "unref" in t && typeof t.unref === "function") t.unref();
|
|
79
|
+
tick = t as NodeJS.Timeout;
|
|
80
|
+
},
|
|
81
|
+
cancel,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -53,7 +53,7 @@ export function recordProviderOutFromRateLimit(reply: EventReply | null, untilMs
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
/** The limit tick: one check of the open episode every 30 s (plan 20261027_1500 P3). */
|
|
56
|
-
const LIMIT_TICK_MS = 30_000;
|
|
56
|
+
export const LIMIT_TICK_MS = 30_000;
|
|
57
57
|
|
|
58
58
|
/** The probe's command line and env. Never process.execPath: inside the harness that is the omp
|
|
59
59
|
* binary, so the child was a full Main session (2026-11-03: stray Mains). bun comes from PATH, as
|
package/src/core/guards/state.ts
CHANGED
|
@@ -66,6 +66,11 @@ export interface EditWindow {
|
|
|
66
66
|
mainCallsStart?: number;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
/** When the edit window's `used` list clears (ms since epoch): main_window_min after windowStart. */
|
|
70
|
+
export function editWindowEndMs(w: EditWindow, cfg: Config): number {
|
|
71
|
+
return w.windowStart + cfg.guards.main_window_min * 60_000;
|
|
72
|
+
}
|
|
73
|
+
|
|
69
74
|
// --- this Main's identity (plan 20261027_1300_multi_main P2) ---
|
|
70
75
|
|
|
71
76
|
/** What `neurastream main --name` accepts and what `NEURASTREAM_MAIN_ID` may hold. */
|
|
@@ -157,6 +162,22 @@ export function mainCallsExpired(w: EditWindow): boolean {
|
|
|
157
162
|
return w.mainCalls > 0 && nowSec() - (w.mainCallsStart ?? 0) >= MAIN_CALLS_RESET_SEC;
|
|
158
163
|
}
|
|
159
164
|
|
|
165
|
+
/** The edit cap per window for a tier: Infinity in tier full (no cap). */
|
|
166
|
+
export function mainEditLimit(cfg: Config, tier: string): number {
|
|
167
|
+
if (tier === "full") return Infinity;
|
|
168
|
+
return tier === "strict" ? cfg.autonomy.strict_edit_limit : cfg.guards.main_edit_limit;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The per-turn call cap is on: main_max_calls set and not tier full (exempt tools are the caller's check). */
|
|
172
|
+
export function mainCallsCapOn(cfg: Config, tier: string): boolean {
|
|
173
|
+
return cfg.guards.main_max_calls > 0 && tier !== "full";
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** When the counted calls expire, in ms since epoch (the wake arms on this clock); null when none are counted. */
|
|
177
|
+
export function mainCallsResetMs(w: EditWindow): number | null {
|
|
178
|
+
return w.mainCalls > 0 ? ((w.mainCallsStart ?? 0) + MAIN_CALLS_RESET_SEC) * 1000 : null;
|
|
179
|
+
}
|
|
180
|
+
|
|
160
181
|
/** Main's terminal hand-back (the adapter's agent_end without willContinue): the turn is over, so
|
|
161
182
|
* the next one starts at 0 calls. The edit window keeps its own reset rules. */
|
|
162
183
|
export function resetMainCalls(paths: Pick<q.QueuePaths, "queue_state">): void {
|
package/src/core/guards/stop.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Prompt injection, cancel-word flagging, and keep-working stop rules.
|
|
2
2
|
|
|
3
|
+
import path from "node:path";
|
|
3
4
|
import type { Decision, InjectEvent, StopEvent } from "../adapter.ts";
|
|
4
5
|
import { currentTier } from "../autonomy.ts";
|
|
5
6
|
import type { Config } from "../config.ts";
|
|
@@ -12,6 +13,7 @@ import {
|
|
|
12
13
|
incidentLine,
|
|
13
14
|
isOwnerProse,
|
|
14
15
|
log,
|
|
16
|
+
mainCallsCapOn,
|
|
15
17
|
mainCallsExpired,
|
|
16
18
|
openIncidentList,
|
|
17
19
|
readStaffing,
|
|
@@ -42,6 +44,15 @@ export function flagCancel(text: string, cfg: Config): string {
|
|
|
42
44
|
return `\nmain-guard: ${CANCEL_TODO} Flagged: "${words}"`;
|
|
43
45
|
}
|
|
44
46
|
|
|
47
|
+
/** The owner prompt's Tools-and-limits card: the call cap and the open plans' deny-command blockers, three lines at most. */
|
|
48
|
+
function toolsCard(cfg: Config, tier: string, plans: q.Plan[]): string[] {
|
|
49
|
+
const calls = mainCallsCapOn(cfg, tier) ? `Calls: ${cfg.guards.main_max_calls} per turn (warn at ${cfg.guards.main_warn_calls})` : "Calls: off";
|
|
50
|
+
const denies = plans.flatMap(p => p.blockers.filter(b => path.basename(b.script) === "deny-command").map(b => `${path.basename(p.file)}: ${b.args.join(" ")}`));
|
|
51
|
+
if (!denies.length) return ["Tools and limits", calls];
|
|
52
|
+
const more = denies.length > 3 ? ` | +${denies.length - 3} more` : "";
|
|
53
|
+
return ["Tools and limits", calls, `Deny-commands (open plans): ${denies.slice(0, 3).join(" | ")}${more}`];
|
|
54
|
+
}
|
|
55
|
+
|
|
45
56
|
/**
|
|
46
57
|
* prompt/owner message: inject the queue summary, flag cancel wording, list open incidents, reset
|
|
47
58
|
* Main's edit window and the stop counters (the original resets them at every user message). A
|
|
@@ -57,6 +68,7 @@ export function checkInject(e: InjectEvent, cfg: Config): Decision {
|
|
|
57
68
|
if (!continuation) writeWindow({ used: [], windowStart: 0, mainCalls: 0 }, cfg.paths);
|
|
58
69
|
recordOwnerMessage(e.inject, cfg);
|
|
59
70
|
let summary: string;
|
|
71
|
+
let open: q.Plan[] = [];
|
|
60
72
|
try {
|
|
61
73
|
// P4 (20261027_1305): one GET /view per team, read once and handed to the holder and the
|
|
62
74
|
// team lines, so the summary costs one request per team and never a second GET /claims.
|
|
@@ -64,6 +76,7 @@ export function checkInject(e: InjectEvent, cfg: Config): Decision {
|
|
|
64
76
|
summary = q.queueSummary(cfg.paths, cfg.review.agents, summaryHolder(cfg, {}, views));
|
|
65
77
|
const teamLines = teamSummaryLines(cfg, views, q.SUMMARY_CAP);
|
|
66
78
|
if (teamLines.length) summary += `\n${teamLines.join("\n")}`;
|
|
79
|
+
open = q.activePlans(cfg.paths).filter(p => !p.waiting && !q.awaitingMerge(p, cfg.paths));
|
|
67
80
|
} catch (err) {
|
|
68
81
|
summary = `Queue unreadable (${cfg.paths.queue_index}): ${err}`;
|
|
69
82
|
}
|
|
@@ -85,10 +98,11 @@ export function checkInject(e: InjectEvent, cfg: Config): Decision {
|
|
|
85
98
|
const unlockNote = continuation ? "" : unlockLine(cfg);
|
|
86
99
|
const mayNote = ` Main may edit Status/WAITING/Waits on/False positive lines. Test before saying "cannot".`;
|
|
87
100
|
const editLimit = continuation ? "" : `\nautonomy: ${tier} — ${tierNote}${mayNote}${unlockNote}`;
|
|
101
|
+
const card = continuation ? "" : `\n${toolsCard(cfg, tier, open).join("\n")}`;
|
|
88
102
|
return {
|
|
89
103
|
allow: true,
|
|
90
104
|
reason: "",
|
|
91
|
-
inject: `${summary}\n(main-guard: read ${cfg.paths.queue_index}; the listed plans are summarised above; work the open plans, close finished ones with queue-close.)${editLimit}${cancel}${incidentText}`,
|
|
105
|
+
inject: `${summary}\n(main-guard: read ${cfg.paths.queue_index}; the listed plans are summarised above; work the open plans, close finished ones with queue-close.)${editLimit}${card}${cancel}${incidentText}`,
|
|
92
106
|
...(continuation ? {} : { state: { callsSinceStop: 0, idleStops: 0 } }),
|
|
93
107
|
};
|
|
94
108
|
}
|
package/src/core/guards/tool.ts
CHANGED
|
@@ -18,10 +18,13 @@ import {
|
|
|
18
18
|
bypassRegExp,
|
|
19
19
|
changedProjectFiles,
|
|
20
20
|
deny,
|
|
21
|
+
editWindowEndMs,
|
|
21
22
|
log,
|
|
22
23
|
MAIN_CALLS_RESET_SEC,
|
|
23
24
|
MAIN_PLAN_FILE,
|
|
25
|
+
mainCallsCapOn,
|
|
24
26
|
mainCallsExpired,
|
|
27
|
+
mainEditLimit,
|
|
25
28
|
mainStateDir,
|
|
26
29
|
readStaffing,
|
|
27
30
|
readWindow,
|
|
@@ -320,13 +323,13 @@ function editWindow(e: ToolEvent, input: Record<string, unknown>, cfg: Config):
|
|
|
320
323
|
if (!files.length) return ALLOW;
|
|
321
324
|
const tier = currentTier(cfg);
|
|
322
325
|
if (tier === "full") return ALLOW; // no cap: Main works unattended
|
|
323
|
-
const limit =
|
|
326
|
+
const limit = mainEditLimit(cfg, tier);
|
|
324
327
|
const limitName = tier === "strict" ? "autonomy.strict_edit_limit" : "owner limit";
|
|
325
328
|
const now = Date.now();
|
|
326
329
|
return updateWindow(cfg.paths, (w) => {
|
|
327
|
-
if (w.used.length && now
|
|
330
|
+
if (w.used.length && now >= editWindowEndMs(w, cfg)) w.used = [];
|
|
328
331
|
if (w.used.length >= limit) {
|
|
329
|
-
const mins = Math.ceil((w
|
|
332
|
+
const mins = Math.ceil((editWindowEndMs(w, cfg) - now) / 60_000);
|
|
330
333
|
log(cfg.paths.guard_log, `BLOCK tool=${e.tool} after ${w.used.length} edits (tier ${tier}): ${files.join(",")} rule=main.edit-window`);
|
|
331
334
|
return {
|
|
332
335
|
write: false,
|
|
@@ -385,7 +388,7 @@ function mainTool(e: ToolEvent, input: Record<string, unknown>, cfg: Config, run
|
|
|
385
388
|
|
|
386
389
|
/** The per-turn call cap applies to this call: on, not tier full, and the tool is not exempt. */
|
|
387
390
|
function mainCallApplies(e: ToolEvent, cfg: Config): boolean {
|
|
388
|
-
return cfg
|
|
391
|
+
return mainCallsCapOn(cfg, currentTier(cfg)) && !cfg.guards.main_call_exempt_tools.includes(e.tool);
|
|
389
392
|
}
|
|
390
393
|
|
|
391
394
|
/** Refusal once this turn has spent main_max_calls, or null. */
|