neurastream 0.1.8 → 0.1.10
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 +68 -12
- package/package.json +8 -2
- package/src/adapters/omp/index.ts +46 -11
- package/src/adapters/omp/launch.ts +119 -0
- package/src/cli/commands.ts +5 -0
- package/src/cli/dispatch.ts +3 -1
- package/src/core/guards.ts +8 -1
- package/src/core/rollover.ts +12 -4
- package/templates/config.yml +1 -1
package/README.md
CHANGED
|
@@ -1,19 +1,56 @@
|
|
|
1
1
|
# neurastream
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Guardrails for running many AI coding agents at once.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
One "main" agent plans the work and reviews it; cheaper "helper" agents do the coding. neurastream is the set of checks between them. It keeps a queue of written plans, refuses helper actions a plan forbids, limits how many helpers run per provider, scores each helper by accepted work per dollar, and only lets a plan leave the queue once its branch is really merged.
|
|
6
|
+
|
|
7
|
+
The core does not depend on one agent tool. Adapters translate a tool's events into core calls. The first adapter is [omp](https://github.com/can1357/oh-my-pi).
|
|
8
|
+
|
|
9
|
+
A step-by-step visual walkthrough (50 steps, plain language) is in [docs/visual/neurastream-deck.html](docs/visual/neurastream-deck.html). Download it and open it in a browser.
|
|
10
|
+
|
|
11
|
+
```mermaid
|
|
12
|
+
flowchart LR
|
|
13
|
+
O[Owner message] --> P[Plan file in docs/plans]
|
|
14
|
+
P --> Q[queue-check]
|
|
15
|
+
Q --> S[Spawn checks<br/>ledger cap, score gate, brief-lint]
|
|
16
|
+
S --> H[Helper agent<br/>blockers + call budget]
|
|
17
|
+
H --> R[Main review<br/>agent-score accept]
|
|
18
|
+
R --> M[Merge to main]
|
|
19
|
+
M --> C[queue-close<br/>merged proof + done check]
|
|
20
|
+
```
|
|
6
21
|
|
|
7
|
-
|
|
22
|
+
## What it does
|
|
23
|
+
|
|
24
|
+
| Part | What it enforces |
|
|
25
|
+
|---|---|
|
|
26
|
+
| Queue (`queue-check`, `queue-close`, `queue-cancel`) | Every open plan is listed in an index and follows a template. A plan leaves the queue only when its branch is merged into remote main and its done check passes; a failing check reopens it. A cancelled plan's unmerged work is saved under `refs/queue-archive/`. |
|
|
27
|
+
| Blockers | Each plan's phase 0 lists what helpers may not do: push, touch a path, run a command, or write a text. The guard refuses those calls. |
|
|
28
|
+
| Guards | Helpers may only edit their plan's `## Progress`; they get a warning at call 90 and are stopped at call 100. Protected files need an owner unlock (a file the owner touches, valid 30 minutes). |
|
|
29
|
+
| Ledger (`agent-ledger`) | Tokens, cost, state and exit reason per agent; `--cap` refuses a spawn when a provider already runs 2. |
|
|
30
|
+
| Scoring (`agent-score`) | Points only for work Main accepted by commit, times the phase difficulty. Ranked per dollar and per hour. |
|
|
31
|
+
| Brief lint (`brief-lint`) | Refuses a helper brief that misses a required rule (acceptance, stop condition, no push, call budget). |
|
|
32
|
+
| Machine health (`machine-guard`, `main-start`, `heavy-run`) | Load, free memory, disk and `/tmp` limits; one heavy test run at a time. |
|
|
33
|
+
| 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. |
|
|
34
|
+
| Session rollover | When the main session file passes 5 MB, the next owner message starts a fresh session from a handoff note. |
|
|
35
|
+
|
|
36
|
+
## Requirements
|
|
37
|
+
|
|
38
|
+
- [Bun](https://bun.sh) 1.x
|
|
39
|
+
- git
|
|
40
|
+
- omp, for the guards and rollover (the command-line tools work without it)
|
|
41
|
+
|
|
42
|
+
## Install
|
|
8
43
|
|
|
9
44
|
```sh
|
|
10
|
-
export HOME=/tmp/neurastream-home-dev
|
|
11
45
|
bun add -g neurastream
|
|
12
46
|
neurastream init
|
|
13
47
|
neurastream adapter install omp
|
|
48
|
+
neurastream doctor
|
|
14
49
|
```
|
|
15
50
|
|
|
16
|
-
`init` writes `~/.config/neurastream/config.yml` from the defaults in
|
|
51
|
+
`init` writes `~/.config/neurastream/config.yml` from the defaults in [docs/config.md](docs/config.md), plus an empty index, notes, the queue template, blocker wrappers, and the state directories. A second run changes nothing. `adapter install omp` links the extension (`~/.omp/agent/extensions` and `omp plugin link`). `doctor` exits 0 when the install matches the package.
|
|
52
|
+
|
|
53
|
+
The defaults are the author's setup (paths, the backup repo, provider names). Edit `config.yml` before relying on them. To try it without touching your real home, set `HOME=/tmp/neurastream-home-dev` first.
|
|
17
54
|
|
|
18
55
|
Check the merged result:
|
|
19
56
|
|
|
@@ -21,11 +58,21 @@ Check the merged result:
|
|
|
21
58
|
neurastream config --json
|
|
22
59
|
```
|
|
23
60
|
|
|
24
|
-
|
|
61
|
+
Every tool is one program, selected by the name it was called with: `agent-score`, `agent-ledger`, `brief-lint`, `oc-run`, `wip-check`, `wip-backup`, `wip-snapshot`, `pm-fail-facts`, `main-health`, `queue-check`, `queue-close`, `queue-cancel`, `main-start`, `machine-guard`, `glm-reset`, `priority-watch`, `resource-alerts`, `tmp-snapshot`, `heavy-run`.
|
|
62
|
+
|
|
63
|
+
## Develop
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
bun install
|
|
67
|
+
bun run typecheck
|
|
68
|
+
bun test
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Green is both exiting 0.
|
|
25
72
|
|
|
26
|
-
Scoring
|
|
73
|
+
## Scoring details
|
|
27
74
|
|
|
28
|
-
|
|
75
|
+
`agent-score accept` records accepted work by commit; `agent-score validate <Agent> --plan <plan.md> --phase <P> --evidence <report.md> --section "<heading prefix>"` records a validation phase (checks/tests ran, report section written, no commits) with proof Main cannot fake by typing: the plan line must be ticked `- [x]`, the cited section body must name a heavy-run label run by that agent (from `paths.heavy_log`), and the phase must not be scored already. The ranking board counts it in `Accepted (diag/val)` as `N (d/v)`.
|
|
29
76
|
|
|
30
77
|
## Main reads the code for its plans
|
|
31
78
|
|
|
@@ -43,15 +90,20 @@ A plan parked for the owner has one line: `WAITING on owner: <what>`. That line
|
|
|
43
90
|
|
|
44
91
|
## Session rollover
|
|
45
92
|
|
|
46
|
-
Main's session file grows with every turn. When it reaches `rollover.max_session_mb` (default
|
|
93
|
+
Main's session file grows with every turn. When it reaches `rollover.max_session_mb` (default 5, `docs/config.md`), Main writes a handoff note to `<queue_state>/rollover/<session id>.md`. The note covers the work in progress and its state, the decisions made and why, the next step, and the files involved. Main is asked for it once per session file, when a turn ends.
|
|
94
|
+
|
|
95
|
+
There are two modes:
|
|
96
|
+
|
|
97
|
+
- Under `neurastream main` (the launcher) the rollover is automatic. Once the note is written and no own jobs (subagents included) are still running, omp writes `relaunch.json` and exits. The launcher starts a fresh omp whose first message is the handoff: the note verbatim, the generated `main-start` state (including the owner holds), the owner's unsent draft if there is one, and a line that the owner is away. Nobody types anything. A handoff over 100000 bytes goes to `<queue_state>/rollover/<session id>.handoff.md`, and the first message points at that file.
|
|
98
|
+
- Under plain `omp` the switch is never automatic. The owner's next plain message closes the session and starts a fresh one; the fresh session opens with the note verbatim, the generated state, and then the owner's message.
|
|
47
99
|
|
|
48
|
-
|
|
100
|
+
In both modes:
|
|
49
101
|
|
|
50
102
|
- A note counts only if it was written after the threshold was crossed and is at most one hour old.
|
|
51
|
-
-
|
|
52
|
-
- While own jobs (subagents included) are still running, the switch waits: a rollover would cancel them, so the owner's message stays in the current session until they finish.
|
|
103
|
+
- While own jobs (subagents included) are still running, the switch waits: a rollover would cancel them.
|
|
53
104
|
- `/main-rollover <message>` does the same by hand. It refuses below the threshold, without a fresh note, or while jobs run.
|
|
54
105
|
- `/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.
|
|
106
|
+
- The previous session file stays where it is.
|
|
55
107
|
- `rollover.enabled: false` turns the feature off.
|
|
56
108
|
|
|
57
109
|
## Changing a script
|
|
@@ -64,3 +116,7 @@ neurastream adapter install omp
|
|
|
64
116
|
```
|
|
65
117
|
|
|
66
118
|
Live copies are never hand-edited. `neurastream doctor` names each hand-edited or foreign installed file, and a stale install (the installed version is not `npm view neurastream version`). If the registry cannot be reached, that check is a warning, not a failure.
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
MIT. See [LICENSE](LICENSE).
|
package/package.json
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "neurastream",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.10",
|
|
4
|
+
"description": "Guardrails for running many AI coding agents at once: plan queue, blockers, call budgets, provider caps, scoring, merge-verified close.",
|
|
5
|
+
"keywords": ["ai-agents", "coding-agents", "orchestration", "guardrails", "omp", "bun", "cli"],
|
|
6
|
+
"homepage": "https://github.com/umiddey/neurastream#readme",
|
|
7
|
+
"bugs": "https://github.com/umiddey/neurastream/issues",
|
|
5
8
|
"type": "module",
|
|
6
9
|
"license": "MIT",
|
|
7
10
|
"repository": {
|
|
8
11
|
"type": "git",
|
|
9
12
|
"url": "git+https://github.com/umiddey/neurastream.git"
|
|
10
13
|
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"bun": ">=1.0"
|
|
16
|
+
},
|
|
11
17
|
"publishConfig": {
|
|
12
18
|
"access": "public"
|
|
13
19
|
},
|
|
@@ -17,13 +17,13 @@
|
|
|
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 { join } from "node:path";
|
|
20
|
+
import { dirname, join } from "node:path";
|
|
21
21
|
import type { Decision } from "../../core/adapter.ts";
|
|
22
22
|
import { loadConfig, type Config } from "../../core/config.ts";
|
|
23
23
|
import { checkInject, checkPlanEdit, checkStop, checkTool, MAIN_PLAN_FILE, recordMainResult } from "../../core/guards.ts";
|
|
24
24
|
import { applyQueueEnv } from "../../core/queue/env.ts";
|
|
25
25
|
import { mainStartText } from "../../core/main-start.ts";
|
|
26
|
-
import { buildHandoff, holdsBlock, notePath, noteRequest, type RolloverState, rolloverState } from "../../core/rollover.ts";
|
|
26
|
+
import { buildHandoff, holdsBlock, notePath, noteRequest, relaunchPath, type RolloverState, rolloverState } from "../../core/rollover.ts";
|
|
27
27
|
import * as q from "../../core/queue/lib.ts";
|
|
28
28
|
// Registers the plugin-command matcher into core's writeTargets (side effect).
|
|
29
29
|
import "./package-ops.ts";
|
|
@@ -33,6 +33,7 @@ export interface ExtensionHost {
|
|
|
33
33
|
on(event: string, cb: (...args: unknown[]) => unknown): void;
|
|
34
34
|
registerCommand(name: string, opts: { description?: string; handler: (args: unknown, ctx: unknown) => unknown }): void;
|
|
35
35
|
sendUserMessage(content: string): void;
|
|
36
|
+
sendMessage(message: { customType: string; content: string; display: boolean }, options: { triggerTurn: boolean; deliverAs: "nextTurn" }): void;
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
/** The parts of a harness call context the handlers look at. */
|
|
@@ -79,7 +80,7 @@ export function toExtensionResult(d: Decision): Record<string, unknown> {
|
|
|
79
80
|
// A push or merge by Main may have landed a completed plan: try closing right away.
|
|
80
81
|
const PUSH_MERGE = /\bgit\b[^\n;|&]*\bpush\b|\b(glab\s+mr|gh\s+pr)\s+merge\b/;
|
|
81
82
|
|
|
82
|
-
function adapterLog(cfg: Config, line: string): void {
|
|
83
|
+
export function adapterLog(cfg: Config, line: string): void {
|
|
83
84
|
try {
|
|
84
85
|
appendFileSync(cfg.paths.guard_log, `${new Date().toISOString()} ${line}\n`);
|
|
85
86
|
} catch {}
|
|
@@ -172,6 +173,8 @@ interface RolloverCtx {
|
|
|
172
173
|
waitForIdle(): unknown;
|
|
173
174
|
newSession(opts: { parentSession: string }): unknown;
|
|
174
175
|
notify(message: string): void;
|
|
176
|
+
draft(): string;
|
|
177
|
+
shutdown(): void;
|
|
175
178
|
}
|
|
176
179
|
|
|
177
180
|
/** Call a method on a harness object by name; undefined when it is not a function. */
|
|
@@ -196,6 +199,13 @@ function asRolloverCtx(v: unknown): RolloverCtx | null {
|
|
|
196
199
|
waitForIdle: () => call(r, "waitForIdle"),
|
|
197
200
|
newSession: opts => call(r, "newSession", [opts]),
|
|
198
201
|
notify: message => call(asRecord(r.ui), "notify", [message]),
|
|
202
|
+
draft: () => {
|
|
203
|
+
const text = call(asRecord(r.ui), "getEditorText");
|
|
204
|
+
return typeof text === "string" ? text : "";
|
|
205
|
+
},
|
|
206
|
+
shutdown: () => {
|
|
207
|
+
call(r, "shutdown");
|
|
208
|
+
},
|
|
199
209
|
};
|
|
200
210
|
}
|
|
201
211
|
|
|
@@ -343,8 +353,9 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
|
|
|
343
353
|
});
|
|
344
354
|
|
|
345
355
|
// Session rollover (src/core/rollover.ts). Main's session size decides when a handoff note is
|
|
346
|
-
// asked for
|
|
347
|
-
//
|
|
356
|
+
// asked for at turn end (a sendMessage that triggers Main's next turn). The owner's next message
|
|
357
|
+
// then starts a fresh session from that note; under `neurastream main` the launcher starts it at
|
|
358
|
+
// once. The size is re-read at every turn end and tool result; the note request goes out once per session file.
|
|
348
359
|
let crossed: { file: string; atMs: number } | null = null;
|
|
349
360
|
let asked: string | null = null;
|
|
350
361
|
let waitNoticeMs = 0;
|
|
@@ -365,10 +376,38 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
|
|
|
365
376
|
});
|
|
366
377
|
};
|
|
367
378
|
|
|
379
|
+
let relaunched = false;
|
|
380
|
+
// The launcher restarts omp: the request file carries the note and the editor draft, and omp
|
|
381
|
+
// shuts down only after the file is written. A failed write keeps the session running.
|
|
382
|
+
const requestRelaunch = (c: RolloverCtx, note: string): void => {
|
|
383
|
+
try {
|
|
384
|
+
mkdirSync(dirname(relaunchPath()), { recursive: true });
|
|
385
|
+
writeFileSync(
|
|
386
|
+
relaunchPath(),
|
|
387
|
+
`${JSON.stringify({ previousSessionFile: c.sessionFile, notePath: note, draft: c.draft() })}\n`,
|
|
388
|
+
);
|
|
389
|
+
} catch (err) {
|
|
390
|
+
adapterLog(cfg, `rollover: relaunch request not written: ${String(err)}`);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
relaunched = true;
|
|
394
|
+
adapterLog(cfg, `rollover: relaunch requested from ${c.sessionFile}`);
|
|
395
|
+
c.shutdown();
|
|
396
|
+
};
|
|
397
|
+
|
|
368
398
|
const onSizeCheck = (_e: unknown, ctx: unknown): void => {
|
|
369
399
|
const c = asRolloverCtx(ctx);
|
|
370
400
|
if (!cfg.rollover.enabled || !c || !isMain(ctx)) return;
|
|
371
|
-
stateOf(c);
|
|
401
|
+
const state = stateOf(c);
|
|
402
|
+
if (state === "need-note" && asked !== c.sessionFile) {
|
|
403
|
+
asked = c.sessionFile;
|
|
404
|
+
pi.sendMessage(
|
|
405
|
+
{ customType: "main-guard.rollover", content: noteRequest(notePath(c.sessionId), process.env.NEURASTREAM_MAIN_LAUNCHER === "1"), display: false },
|
|
406
|
+
{ triggerTurn: true, deliverAs: "nextTurn" },
|
|
407
|
+
);
|
|
408
|
+
} else if (state === "ready" && process.env.NEURASTREAM_MAIN_LAUNCHER === "1" && !relaunched) {
|
|
409
|
+
requestRelaunch(c, notePath(c.sessionId));
|
|
410
|
+
}
|
|
372
411
|
};
|
|
373
412
|
pi.on("agent_end", onSizeCheck);
|
|
374
413
|
|
|
@@ -385,15 +424,11 @@ export function createOmpAdapter(pi: ExtensionHost, cfg: Config): void {
|
|
|
385
424
|
};
|
|
386
425
|
});
|
|
387
426
|
|
|
388
|
-
/**
|
|
427
|
+
/** Wait-jobs notice for Main's next turn, throttled; null otherwise. The note request goes out at turn end. */
|
|
389
428
|
const rolloverNotice = (ctx: unknown): string | null => {
|
|
390
429
|
const c = asRolloverCtx(ctx);
|
|
391
430
|
if (!cfg.rollover.enabled || !c) return null;
|
|
392
431
|
const state = stateOf(c);
|
|
393
|
-
if (state === "need-note" && asked !== c.sessionFile) {
|
|
394
|
-
asked = c.sessionFile;
|
|
395
|
-
return noteRequest(notePath(c.sessionId));
|
|
396
|
-
}
|
|
397
432
|
if (state === "wait-jobs" && Date.now() - waitNoticeMs >= WAIT_NOTICE_MS) {
|
|
398
433
|
waitNoticeMs = Date.now();
|
|
399
434
|
const jobs = c.runningJobs().join(", ");
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// `neurastream main [args…]`: runs the harness in a loop. Under this launcher (env
|
|
2
|
+
// NEURASTREAM_MAIN_LAUNCHER=1) a session that is over the rollover size and idle writes
|
|
3
|
+
// relaunch.json just before it exits; the launcher then starts a fresh harness whose only argument
|
|
4
|
+
// is the handoff. Plain `omp` never gets the flag, so it never relaunches.
|
|
5
|
+
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import { mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { basename, dirname, join } from "node:path";
|
|
9
|
+
import type { Config } from "../../core/config.ts";
|
|
10
|
+
import { mainStartText } from "../../core/main-start.ts";
|
|
11
|
+
import { applyQueueEnv } from "../../core/queue/env.ts";
|
|
12
|
+
import * as q from "../../core/queue/lib.ts";
|
|
13
|
+
import { buildHandoff, holdsBlock, relaunchPath } from "../../core/rollover.ts";
|
|
14
|
+
import { adapterLog } from "./index.ts";
|
|
15
|
+
|
|
16
|
+
/** The harness the launcher runs. Tests pass another path through LaunchOptions. */
|
|
17
|
+
export const OMP_BIN = "omp";
|
|
18
|
+
|
|
19
|
+
/** Handoffs up to this many bytes go in the first message; larger ones go to a file. */
|
|
20
|
+
const MAX_INLINE_HANDOFF = 100_000;
|
|
21
|
+
|
|
22
|
+
const OWNER_AWAY =
|
|
23
|
+
"Owner away (automatic rollover). Continue the work in the note and the queue; the owner reads results later.";
|
|
24
|
+
|
|
25
|
+
export interface LaunchOptions {
|
|
26
|
+
harness?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface RelaunchRequest {
|
|
30
|
+
previousSessionFile: string;
|
|
31
|
+
notePath: string;
|
|
32
|
+
draft: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Runs the harness until a run ends without a fresh relaunch request; returns the last run's code. */
|
|
36
|
+
export async function runMainLauncher(args: string[], cfg: Config, opts: LaunchOptions = {}): Promise<number> {
|
|
37
|
+
applyQueueEnv(cfg);
|
|
38
|
+
const harness = opts.harness ?? OMP_BIN;
|
|
39
|
+
let runArgs = args;
|
|
40
|
+
for (;;) {
|
|
41
|
+
rmSync(relaunchPath(), { force: true });
|
|
42
|
+
const started = Date.now();
|
|
43
|
+
const code = await runChild(harness, runArgs);
|
|
44
|
+
const req = takeRequest(started, cfg);
|
|
45
|
+
if (req === null) return code;
|
|
46
|
+
const next = await nextArgs(req, cfg);
|
|
47
|
+
if (next === null) return code;
|
|
48
|
+
runArgs = next;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function runChild(harness: string, args: string[]): Promise<number> {
|
|
53
|
+
const { promise, resolve } = Promise.withResolvers<number>();
|
|
54
|
+
const child = spawn(harness, args, {
|
|
55
|
+
stdio: "inherit",
|
|
56
|
+
env: { ...process.env, NEURASTREAM_MAIN_LAUNCHER: "1" },
|
|
57
|
+
});
|
|
58
|
+
child.on("error", err => {
|
|
59
|
+
console.error(`neurastream main: cannot start ${harness}: ${err.message}`);
|
|
60
|
+
resolve(127);
|
|
61
|
+
});
|
|
62
|
+
child.on("close", code => resolve(code ?? 1));
|
|
63
|
+
return promise;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The request the last run left behind, only if it was written during that run; consumed on read. */
|
|
67
|
+
function takeRequest(since: number, cfg: Config): RelaunchRequest | null {
|
|
68
|
+
const path = relaunchPath();
|
|
69
|
+
let mtimeMs: number;
|
|
70
|
+
try {
|
|
71
|
+
mtimeMs = statSync(path).mtimeMs;
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
if (mtimeMs < since) return null;
|
|
76
|
+
try {
|
|
77
|
+
const raw = JSON.parse(readFileSync(path, "utf8")) as Partial<RelaunchRequest>;
|
|
78
|
+
rmSync(path, { force: true });
|
|
79
|
+
if (typeof raw.previousSessionFile !== "string" || typeof raw.notePath !== "string") {
|
|
80
|
+
adapterLog(cfg, `rollover: relaunch request ignored, missing fields in ${path}`);
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
return { previousSessionFile: raw.previousSessionFile, notePath: raw.notePath, draft: raw.draft ?? "" };
|
|
84
|
+
} catch (err) {
|
|
85
|
+
adapterLog(cfg, `rollover: relaunch request ignored, unreadable ${path}: ${String(err)}`);
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The next run's arguments: the handoff inline, or a file the inline argument points at. */
|
|
91
|
+
async function nextArgs(req: RelaunchRequest, cfg: Config): Promise<string[] | null> {
|
|
92
|
+
let note: string;
|
|
93
|
+
try {
|
|
94
|
+
note = readFileSync(req.notePath, "utf8");
|
|
95
|
+
} catch (err) {
|
|
96
|
+
adapterLog(cfg, `rollover: relaunch stopped, note unreadable ${req.notePath}: ${String(err)}`);
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
const generated = [await mainStartText([], cfg, { inheritStdin: false }), holdsBlock(q.ownerHolds())].join("\n");
|
|
100
|
+
const draft = req.draft.trim() === "" ? "" : `\nOwner's unsent draft: ${req.draft}`;
|
|
101
|
+
const handoff = buildHandoff({
|
|
102
|
+
generated,
|
|
103
|
+
note,
|
|
104
|
+
previousSessionFile: req.previousSessionFile,
|
|
105
|
+
ownerMessage: `${OWNER_AWAY}${draft}`,
|
|
106
|
+
});
|
|
107
|
+
const bytes = Buffer.byteLength(handoff);
|
|
108
|
+
if (bytes <= MAX_INLINE_HANDOFF) {
|
|
109
|
+
adapterLog(cfg, `rollover: relaunch from ${req.previousSessionFile} with the handoff inline (${bytes} bytes)`);
|
|
110
|
+
return [handoff];
|
|
111
|
+
}
|
|
112
|
+
const file = join(dirname(req.notePath), `${basename(req.notePath, ".md")}.handoff.md`);
|
|
113
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
114
|
+
writeFileSync(file, handoff);
|
|
115
|
+
adapterLog(cfg, `rollover: relaunch from ${req.previousSessionFile} with the handoff in ${file} (${bytes} bytes)`);
|
|
116
|
+
return [
|
|
117
|
+
`Read ${file} first: it is the handoff from your previous session (too long for one message).`,
|
|
118
|
+
];
|
|
119
|
+
}
|
package/src/cli/commands.ts
CHANGED
|
@@ -18,6 +18,7 @@ import { wipBackup } from "../core/wip-backup.ts";
|
|
|
18
18
|
import { wipCheck } from "../core/wip-check.ts";
|
|
19
19
|
import { wipSnapshot } from "../core/wip-snapshot.ts";
|
|
20
20
|
import { runtime } from "./runtime.ts";
|
|
21
|
+
import { runMainLauncher } from "../adapters/omp/launch.ts";
|
|
21
22
|
|
|
22
23
|
export const PORTED = [
|
|
23
24
|
"agent-score",
|
|
@@ -41,6 +42,10 @@ export const PORTED = [
|
|
|
41
42
|
"heavy-run",
|
|
42
43
|
] as const;
|
|
43
44
|
|
|
45
|
+
export async function runMain(args: string[]): Promise<number> {
|
|
46
|
+
return runMainLauncher(args, runtime().cfg);
|
|
47
|
+
}
|
|
48
|
+
|
|
44
49
|
export async function runPorted(cmd: string, args: string[]): Promise<number> {
|
|
45
50
|
const rt = runtime();
|
|
46
51
|
switch (cmd) {
|
package/src/cli/dispatch.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { runAdapterInstall } from "../adapters/omp/install.ts";
|
|
|
4
4
|
import { runDoctor } from "../adapters/omp/doctor.ts";
|
|
5
5
|
import { GUARD_USAGE, runGuard } from "./guard.ts";
|
|
6
6
|
import { runInit } from "./init.ts";
|
|
7
|
-
import { PORTED, runPorted } from "./commands.ts";
|
|
7
|
+
import { PORTED, runMain, runPorted } from "./commands.ts";
|
|
8
8
|
|
|
9
9
|
const BINS = [
|
|
10
10
|
"agent-score",
|
|
@@ -43,6 +43,7 @@ export function helpText(): string {
|
|
|
43
43
|
" neurastream config [--json] print the merged config (defaults + file + env)",
|
|
44
44
|
" neurastream guard <event> --json",
|
|
45
45
|
` (${GUARD_USAGE.slice(GUARD_USAGE.indexOf("(") + 1, -1)})`,
|
|
46
|
+
" neurastream main [args] run omp under the launcher (automatic session rollover)",
|
|
46
47
|
"",
|
|
47
48
|
"Compatibility bins (same names as today; ports land in later phases):",
|
|
48
49
|
...BINS.map(b => ` ${b}`),
|
|
@@ -57,6 +58,7 @@ export async function run(cmd: string, args: string[]): Promise<number> {
|
|
|
57
58
|
console.log(helpText());
|
|
58
59
|
return 0;
|
|
59
60
|
}
|
|
61
|
+
if (cmd === "main") return runMain(args);
|
|
60
62
|
if (cmd === "init") return runInit(args);
|
|
61
63
|
if (cmd === "adapter") return runAdapterInstall(args);
|
|
62
64
|
if (cmd === "config") {
|
package/src/core/guards.ts
CHANGED
|
@@ -353,7 +353,14 @@ export function checkTool(e: ToolEvent, cfg: Config): Decision {
|
|
|
353
353
|
for (const p of [...(e.paths ?? []), ...q.touchedPaths(e.tool, input, e.cwd)]) if (!paths.includes(p)) paths.push(p);
|
|
354
354
|
const writes: string[] = [];
|
|
355
355
|
for (const p of [...(e.writes ?? []), ...q.writeTargets(e.tool, input, e.cwd)]) if (!writes.includes(p)) writes.push(p);
|
|
356
|
-
|
|
356
|
+
// Main writes its own rollover note unattended (the note turn has nobody to ask). Only a Write
|
|
357
|
+
// of a .md directly in <state>/rollover/ is exempt; shell writes, subagents and relaunch.json stay refused.
|
|
358
|
+
const rolloverDir = path.join(q.stateDirPath(), "rollover");
|
|
359
|
+
const openWrites =
|
|
360
|
+
e.agent === "main" && e.tool === "write"
|
|
361
|
+
? writes.filter(p => !(path.extname(p) === ".md" && path.dirname(path.resolve(p)) === rolloverDir))
|
|
362
|
+
: writes;
|
|
363
|
+
const hit = protectedHit(openWrites, cfg);
|
|
357
364
|
if (hit && !unlocked(cfg)) {
|
|
358
365
|
log(cfg.paths.plan_guard_log, `BLOCK ${e.agent}:${e.agentId} tool=${e.tool} protected: ${hit}`);
|
|
359
366
|
return deny(
|
package/src/core/rollover.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Session rollover, pure core: when Main's session file has grown past `rollover.max_session_mb`,
|
|
2
|
-
// Main writes a handoff note first; the owner's next message then opens a fresh session that
|
|
3
|
-
//
|
|
2
|
+
// Main writes a handoff note first; the owner's next message then opens a fresh session that starts
|
|
3
|
+
// from the note plus the generated state. Under `neurastream main` the launcher starts that session
|
|
4
|
+
// without waiting for the owner. Decisions only: the adapter reads the file size,
|
|
4
5
|
// the jobs and the note mtime, and performs the switch. No harness names here.
|
|
5
6
|
|
|
6
7
|
import { join } from "node:path";
|
|
@@ -71,12 +72,14 @@ export function holdsBlock(holds: readonly string[]): string {
|
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
/** The one-time note instruction for Main's next turn. */
|
|
74
|
-
export function noteRequest(path: string): string {
|
|
75
|
+
export function noteRequest(path: string, auto = false): string {
|
|
75
76
|
return [
|
|
76
77
|
"Session rollover: this session has grown past rollover.max_session_mb.",
|
|
77
78
|
`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.`,
|
|
78
79
|
"Do not list holds or owner blockers in the note; they come from the plans' WAITING on owner lines.",
|
|
79
|
-
|
|
80
|
+
auto
|
|
81
|
+
? "As soon as this note is written and no own jobs run, this session restarts into a fresh one that starts from this note verbatim plus the generated main-start state."
|
|
82
|
+
: "When the owner's next message arrives, this session closes and a fresh one starts from that note verbatim plus the generated main-start state.",
|
|
80
83
|
].join("\n");
|
|
81
84
|
}
|
|
82
85
|
|
|
@@ -84,3 +87,8 @@ export function noteRequest(path: string): string {
|
|
|
84
87
|
export function notePath(sessionId: string, stateDir = stateDirPath()): string {
|
|
85
88
|
return join(stateDir, "rollover", `${sessionId}.md`);
|
|
86
89
|
}
|
|
90
|
+
|
|
91
|
+
/** The launcher's relaunch request file: written by the harness just before it exits under `neurastream main`. */
|
|
92
|
+
export function relaunchPath(stateDir = stateDirPath()): string {
|
|
93
|
+
return join(stateDir, "rollover", "relaunch.json");
|
|
94
|
+
}
|