neurastream 0.1.10 → 0.1.12
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 +3 -1
- package/package.json +1 -1
- package/src/adapters/omp/launch.ts +57 -11
- package/src/core/blockers.ts +434 -26
package/README.md
CHANGED
|
@@ -24,7 +24,7 @@ flowchart LR
|
|
|
24
24
|
| Part | What it enforces |
|
|
25
25
|
|---|---|
|
|
26
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
|
|
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 judges the command that runs and the files it changes, not words inside commit messages, quoted text or heredoc bodies: `git commit -m "... push ..."` is allowed, `git push` is refused. |
|
|
28
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
29
|
| Ledger (`agent-ledger`) | Tokens, cost, state and exit reason per agent; `--cap` refuses a spawn when a provider already runs 2. |
|
|
30
30
|
| Scoring (`agent-score`) | Points only for work Main accepted by commit, times the phase difficulty. Ranked per dollar and per hour. |
|
|
@@ -97,6 +97,8 @@ There are two modes:
|
|
|
97
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
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.
|
|
99
99
|
|
|
100
|
+
A relaunch keeps the flags of the first run (`--model`, `--session-dir`, …) and drops only the first message and the session-picking and one-shot flags (`-r`, `-c`, `--restore`, `--goal`, `-p`, …), so the fresh omp stays on the same model and session directory.
|
|
101
|
+
|
|
100
102
|
In both modes:
|
|
101
103
|
|
|
102
104
|
- A note counts only if it was written after the threshold was crossed and is at most one hour old.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "neurastream",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
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",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// `neurastream main [args…]`: runs the harness in a loop. Under this launcher (env
|
|
2
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
|
|
4
|
-
//
|
|
3
|
+
// relaunch.json just before it exits; the launcher then starts a fresh harness with the owner's
|
|
4
|
+
// flags (carriedArgs) and the handoff as its message. Plain `omp` never gets the flag, so it never relaunches.
|
|
5
5
|
|
|
6
6
|
import { spawn } from "node:child_process";
|
|
7
7
|
import { mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
@@ -16,6 +16,53 @@ import { adapterLog } from "./index.ts";
|
|
|
16
16
|
/** The harness the launcher runs. Tests pass another path through LaunchOptions. */
|
|
17
17
|
export const OMP_BIN = "omp";
|
|
18
18
|
|
|
19
|
+
/** Flags that take the next argument as their value (omp 18.11.1 --help shows them as `--flag=<value>`). */
|
|
20
|
+
export const VALUE_FLAGS: Record<string, true> = {
|
|
21
|
+
"--model": true, "--smol": true, "--slow": true, "--plan": true, "--goal": true, "--prewalk-into": true,
|
|
22
|
+
"--plan-yolo-into": true, "--provider": true, "--api-key": true, "--system-prompt": true,
|
|
23
|
+
"--system-prompt-template": true, "--append-system-prompt": true, "--profile": true, "--alias": true,
|
|
24
|
+
"--cwd": true, "--mode": true, "--config": true, "--add-dir": true, "--restore": true, "--session-dir": true,
|
|
25
|
+
"--models": true, "--tools": true, "--thinking": true, "--service-tier": true, "--hook": true, "-e": true,
|
|
26
|
+
"--extension": true, "--skills": true, "--export": true, "--max-time": true, "--approval-mode": true,
|
|
27
|
+
"--plugin-dir": true,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Session-picking and one-shot flags: a relaunch never carries them, with their value if they take one. */
|
|
31
|
+
const DROPPED_FLAGS: Record<string, true> = {
|
|
32
|
+
"-r": true, "--resume": true, "--restore": true, "-c": true, "--continue": true, "--goal": true,
|
|
33
|
+
"--from-claude": true, "--from-codex": true, "--export": true, "--alias": true, "-p": true, "--print": true,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** Whether `flag` takes `next` as its value: VALUE_FLAGS always do; `-r` only when `next` is not a flag. */
|
|
37
|
+
function takesValue(flag: string, next: string | undefined): boolean {
|
|
38
|
+
if (next === undefined) return false;
|
|
39
|
+
if (VALUE_FLAGS[flag] === true) return true;
|
|
40
|
+
return (flag === "-r" || flag === "--resume") && !next.startsWith("-");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The owner's flags for a relaunch, from the first run's args. Positional arguments (the first
|
|
45
|
+
* message) and session-picking or one-shot flags, with their values, are dropped; every other flag
|
|
46
|
+
* keeps its value. Everything after a bare `--` is positional.
|
|
47
|
+
*/
|
|
48
|
+
export function carriedArgs(args: string[]): string[] {
|
|
49
|
+
const out: string[] = [];
|
|
50
|
+
for (let i = 0; i < args.length; i++) {
|
|
51
|
+
const arg = args[i] ?? "";
|
|
52
|
+
if (arg === "--") break;
|
|
53
|
+
if (!arg.startsWith("-")) continue;
|
|
54
|
+
const eq = arg.indexOf("=");
|
|
55
|
+
const flag = eq === -1 ? arg : arg.slice(0, eq);
|
|
56
|
+
const valued = eq === -1 && takesValue(flag, args[i + 1]);
|
|
57
|
+
if (DROPPED_FLAGS[flag] !== true) {
|
|
58
|
+
out.push(arg);
|
|
59
|
+
if (valued) out.push(args[i + 1] ?? "");
|
|
60
|
+
}
|
|
61
|
+
if (valued) i++;
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
19
66
|
/** Handoffs up to this many bytes go in the first message; larger ones go to a file. */
|
|
20
67
|
const MAX_INLINE_HANDOFF = 100_000;
|
|
21
68
|
|
|
@@ -36,6 +83,7 @@ interface RelaunchRequest {
|
|
|
36
83
|
export async function runMainLauncher(args: string[], cfg: Config, opts: LaunchOptions = {}): Promise<number> {
|
|
37
84
|
applyQueueEnv(cfg);
|
|
38
85
|
const harness = opts.harness ?? OMP_BIN;
|
|
86
|
+
const carried = carriedArgs(args);
|
|
39
87
|
let runArgs = args;
|
|
40
88
|
for (;;) {
|
|
41
89
|
rmSync(relaunchPath(), { force: true });
|
|
@@ -43,9 +91,9 @@ export async function runMainLauncher(args: string[], cfg: Config, opts: LaunchO
|
|
|
43
91
|
const code = await runChild(harness, runArgs);
|
|
44
92
|
const req = takeRequest(started, cfg);
|
|
45
93
|
if (req === null) return code;
|
|
46
|
-
const
|
|
47
|
-
if (
|
|
48
|
-
runArgs =
|
|
94
|
+
const message = await nextMessage(req, cfg);
|
|
95
|
+
if (message === null) return code;
|
|
96
|
+
runArgs = [...carried, message];
|
|
49
97
|
}
|
|
50
98
|
}
|
|
51
99
|
|
|
@@ -87,8 +135,8 @@ function takeRequest(since: number, cfg: Config): RelaunchRequest | null {
|
|
|
87
135
|
}
|
|
88
136
|
}
|
|
89
137
|
|
|
90
|
-
/** The next run's
|
|
91
|
-
async function
|
|
138
|
+
/** The next run's message: the handoff inline, or the pointer to a file that holds it. */
|
|
139
|
+
async function nextMessage(req: RelaunchRequest, cfg: Config): Promise<string | null> {
|
|
92
140
|
let note: string;
|
|
93
141
|
try {
|
|
94
142
|
note = readFileSync(req.notePath, "utf8");
|
|
@@ -107,13 +155,11 @@ async function nextArgs(req: RelaunchRequest, cfg: Config): Promise<string[] | n
|
|
|
107
155
|
const bytes = Buffer.byteLength(handoff);
|
|
108
156
|
if (bytes <= MAX_INLINE_HANDOFF) {
|
|
109
157
|
adapterLog(cfg, `rollover: relaunch from ${req.previousSessionFile} with the handoff inline (${bytes} bytes)`);
|
|
110
|
-
return
|
|
158
|
+
return handoff;
|
|
111
159
|
}
|
|
112
160
|
const file = join(dirname(req.notePath), `${basename(req.notePath, ".md")}.handoff.md`);
|
|
113
161
|
mkdirSync(dirname(file), { recursive: true });
|
|
114
162
|
writeFileSync(file, handoff);
|
|
115
163
|
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
|
-
];
|
|
164
|
+
return `Read ${file} first: it is the handoff from your previous session (too long for one message).`;
|
|
119
165
|
}
|
package/src/core/blockers.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import { readFileSync, statSync } from "node:fs";
|
|
12
12
|
import path from "node:path";
|
|
13
13
|
import { expandHome } from "./queue/lib.ts";
|
|
14
|
+
import { MAIN_PLAN_FILE } from "./guards.ts";
|
|
14
15
|
|
|
15
16
|
export interface BlockerCall {
|
|
16
17
|
tool: string;
|
|
@@ -45,11 +46,11 @@ function isDir(p: string): boolean {
|
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
//
|
|
49
|
-
const
|
|
49
|
+
// Eval code (JS or Python) keeps the whole-text test; shell commands are judged per command.
|
|
50
|
+
const CODE_MUTATING =
|
|
50
51
|
/(\brm\b|\bmv\b|\bcp\b|\btruncate\b|\bsed\s+-i|\btee\b|>{1,2}\s*\S|\bgit\s+(rm|mv|checkout|restore)\b|writeFile|Bun\.write|unlink|\bpatch\b)/;
|
|
51
52
|
|
|
52
|
-
function blockerEnv(call: BlockerCall, args: string[]): BlockerEnv {
|
|
53
|
+
export function blockerEnv(call: BlockerCall, args: string[]): BlockerEnv {
|
|
53
54
|
const shell = call.tool === "bash" || call.tool === "eval";
|
|
54
55
|
const writes = call.tool === "write" || call.tool === "edit" || call.tool === "ast_edit";
|
|
55
56
|
const command = shell ? String(call.input?.command || call.input?.code || "") : "";
|
|
@@ -136,11 +137,399 @@ function matchesPath(p: string, glob: string, call: BlockerCall): boolean {
|
|
|
136
137
|
return fnmatchToRegExp(glob).test(r) || fnmatchToRegExp(`${bare}/*`).test(r) || r === bare;
|
|
137
138
|
}
|
|
138
139
|
|
|
139
|
-
//
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
140
|
+
// ---- Shell: a quote-aware splitter, not a shell parser ---------------------------------------
|
|
141
|
+
// Splits on `;`, `&&`, `||`, `|`, `&`, `(`, `)` and newlines outside quotes. Each word keeps its
|
|
142
|
+
// quote-removed text and whether any part was quoted; output-redirection targets are kept apart
|
|
143
|
+
// from the words; heredoc bodies are dropped; the inside of `$(…)` and backticks becomes segments.
|
|
144
|
+
|
|
145
|
+
export interface ShellWord {
|
|
146
|
+
/** Text with quotes and escapes removed; a substitution contributes nothing (its segments are listed). */
|
|
147
|
+
text: string;
|
|
148
|
+
/** Source text, quotes included. */
|
|
149
|
+
raw: string;
|
|
150
|
+
/** Any part of the word was quoted or escaped. */
|
|
151
|
+
quoted: boolean;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface ShellSegment {
|
|
155
|
+
/** Command words in order; redirections and their targets are not words. */
|
|
156
|
+
words: ShellWord[];
|
|
157
|
+
/** Targets of output redirections: `>`, `>>`, `>|`, `&>`, `&>>`, `<>`, and `>&file`. */
|
|
158
|
+
writes: string[];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
type WordBuf = { text: string; quoted: boolean; start: number };
|
|
162
|
+
type RedirKind = "write" | "dup" | "skip" | "heredoc";
|
|
163
|
+
|
|
164
|
+
/** Segments of a command in source order; a substitution's segments come before their parent's. */
|
|
165
|
+
export function splitShell(command: string): ShellSegment[] {
|
|
166
|
+
const out: ShellSegment[] = [];
|
|
167
|
+
scan(command, out);
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function scan(src: string, out: ShellSegment[]): void {
|
|
172
|
+
const n = src.length;
|
|
173
|
+
const st: {
|
|
174
|
+
seg: ShellSegment;
|
|
175
|
+
word: WordBuf | null;
|
|
176
|
+
redir: RedirKind | null;
|
|
177
|
+
tabs: boolean;
|
|
178
|
+
heredocs: { delim: string; tabs: boolean }[];
|
|
179
|
+
} = { seg: { words: [], writes: [] }, word: null, redir: null, tabs: false, heredocs: [] };
|
|
180
|
+
let i = 0;
|
|
181
|
+
|
|
182
|
+
const ensure = (): WordBuf => {
|
|
183
|
+
if (!st.word) st.word = { text: "", quoted: false, start: i };
|
|
184
|
+
return st.word;
|
|
185
|
+
};
|
|
186
|
+
const endWord = (end: number): void => {
|
|
187
|
+
const w = st.word;
|
|
188
|
+
if (!w) return;
|
|
189
|
+
st.word = null;
|
|
190
|
+
if (w.text === "" && !w.quoted) return; // an unquoted empty word only comes from a $() substitution
|
|
191
|
+
const word: ShellWord = { text: w.text, raw: src.slice(w.start, end), quoted: w.quoted };
|
|
192
|
+
const kind = st.redir;
|
|
193
|
+
if (!kind) {
|
|
194
|
+
st.seg.words.push(word);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
st.redir = null;
|
|
198
|
+
if (kind === "write" || (kind === "dup" && !/^(\d+|-)$/.test(word.text))) st.seg.writes.push(word.text);
|
|
199
|
+
else if (kind === "heredoc") st.heredocs.push({ delim: word.text, tabs: st.tabs });
|
|
200
|
+
};
|
|
201
|
+
const endSeg = (end: number): void => {
|
|
202
|
+
endWord(end);
|
|
203
|
+
st.redir = null;
|
|
204
|
+
if (st.seg.words.length || st.seg.writes.length) out.push(st.seg);
|
|
205
|
+
st.seg = { words: [], writes: [] };
|
|
206
|
+
};
|
|
207
|
+
// Skips the bodies of the heredocs opened on the line before `from`; returns the index after them.
|
|
208
|
+
const skipHeredocs = (from: number): number => {
|
|
209
|
+
let j = from;
|
|
210
|
+
for (const h of st.heredocs) {
|
|
211
|
+
while (j < n) {
|
|
212
|
+
const e = src.indexOf("\n", j);
|
|
213
|
+
const line = src.slice(j, e < 0 ? n : e);
|
|
214
|
+
j = e < 0 ? n : e + 1;
|
|
215
|
+
if ((h.tabs ? line.replace(/^\t+/, "") : line) === h.delim) break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
st.heredocs = [];
|
|
219
|
+
return j;
|
|
220
|
+
};
|
|
221
|
+
// Double-quoted text from `from` (after the opening quote); returns the index after the closing quote.
|
|
222
|
+
const readDouble = (from: number, w: WordBuf): number => {
|
|
223
|
+
let j = from;
|
|
224
|
+
while (j < n && src.charAt(j) !== '"') {
|
|
225
|
+
const d = src.charAt(j);
|
|
226
|
+
const nx = src.charAt(j + 1);
|
|
227
|
+
if (d === "\\" && nx === "\n") j += 2;
|
|
228
|
+
else if (d === "\\" && nx !== "" && '"\\$`'.includes(nx)) {
|
|
229
|
+
w.text += nx;
|
|
230
|
+
j += 2;
|
|
231
|
+
} else if (d === "$" && nx === "(") {
|
|
232
|
+
const end = matchParen(src, j + 2);
|
|
233
|
+
scan(src.slice(j + 2, end), out);
|
|
234
|
+
j = end + 1;
|
|
235
|
+
} else if (d === "`") {
|
|
236
|
+
const end = closeTick(src, j + 1);
|
|
237
|
+
scan(src.slice(j + 1, end), out);
|
|
238
|
+
j = end + 1;
|
|
239
|
+
} else {
|
|
240
|
+
w.text += d;
|
|
241
|
+
j++;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return j + 1;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
while (i < n) {
|
|
248
|
+
const c = src.charAt(i);
|
|
249
|
+
if (c === " " || c === "\t" || c === "\r") {
|
|
250
|
+
endWord(i);
|
|
251
|
+
i++;
|
|
252
|
+
} else if (c === "\\") {
|
|
253
|
+
if (src.charAt(i + 1) === "\n") i += 2;
|
|
254
|
+
else {
|
|
255
|
+
const w = ensure();
|
|
256
|
+
w.quoted = true;
|
|
257
|
+
w.text += src.charAt(i + 1);
|
|
258
|
+
i += 2;
|
|
259
|
+
}
|
|
260
|
+
} else if (c === "'") {
|
|
261
|
+
const w = ensure();
|
|
262
|
+
w.quoted = true;
|
|
263
|
+
const close = src.indexOf("'", i + 1);
|
|
264
|
+
const end = close < 0 ? n : close;
|
|
265
|
+
w.text += src.slice(i + 1, end);
|
|
266
|
+
i = end + 1;
|
|
267
|
+
} else if (c === '"') {
|
|
268
|
+
const w = ensure();
|
|
269
|
+
w.quoted = true;
|
|
270
|
+
i = readDouble(i + 1, w);
|
|
271
|
+
} else if (c === "$" && src.charAt(i + 1) === "(") {
|
|
272
|
+
ensure();
|
|
273
|
+
const end = matchParen(src, i + 2);
|
|
274
|
+
scan(src.slice(i + 2, end), out);
|
|
275
|
+
i = end + 1;
|
|
276
|
+
} else if (c === "`") {
|
|
277
|
+
ensure();
|
|
278
|
+
const end = closeTick(src, i + 1);
|
|
279
|
+
scan(src.slice(i + 1, end), out);
|
|
280
|
+
i = end + 1;
|
|
281
|
+
} else if (c === "#" && !st.word) {
|
|
282
|
+
while (i < n && src.charAt(i) !== "\n") i++;
|
|
283
|
+
} else if (c === "\n") {
|
|
284
|
+
endSeg(i);
|
|
285
|
+
i = skipHeredocs(i + 1);
|
|
286
|
+
} else if (c === ";" || c === "(" || c === ")") {
|
|
287
|
+
endSeg(i);
|
|
288
|
+
i++;
|
|
289
|
+
} else if (c === "|") {
|
|
290
|
+
endSeg(i);
|
|
291
|
+
i += src.charAt(i + 1) === "|" ? 2 : 1;
|
|
292
|
+
} else if (c === "&") {
|
|
293
|
+
if (src.charAt(i + 1) === ">") {
|
|
294
|
+
endWord(i);
|
|
295
|
+
st.redir = "write";
|
|
296
|
+
i += src.charAt(i + 2) === ">" ? 3 : 2;
|
|
297
|
+
} else {
|
|
298
|
+
endSeg(i);
|
|
299
|
+
i += src.charAt(i + 1) === "&" ? 2 : 1;
|
|
300
|
+
}
|
|
301
|
+
} else if (c === ">" || c === "<") {
|
|
302
|
+
const pending = st.word;
|
|
303
|
+
// A bare fd number before a redirection (the 2 in 2>file) is not a word.
|
|
304
|
+
if (pending && !pending.quoted && /^\d+$/.test(pending.text)) st.word = null;
|
|
305
|
+
else endWord(i);
|
|
306
|
+
let j = i + 1;
|
|
307
|
+
const nx = src.charAt(j);
|
|
308
|
+
if (c === ">") {
|
|
309
|
+
if (nx === ">" || nx === "|") {
|
|
310
|
+
st.redir = "write";
|
|
311
|
+
j++;
|
|
312
|
+
} else if (nx === "&") {
|
|
313
|
+
st.redir = "dup";
|
|
314
|
+
j++;
|
|
315
|
+
} else st.redir = "write";
|
|
316
|
+
} else if (nx === "<" && src.charAt(j + 1) === "<") {
|
|
317
|
+
st.redir = "skip";
|
|
318
|
+
j += 2;
|
|
319
|
+
} else if (nx === "<") {
|
|
320
|
+
st.redir = "heredoc";
|
|
321
|
+
j++;
|
|
322
|
+
st.tabs = src.charAt(j) === "-";
|
|
323
|
+
if (st.tabs) j++;
|
|
324
|
+
} else if (nx === ">") {
|
|
325
|
+
st.redir = "write";
|
|
326
|
+
j++;
|
|
327
|
+
} else if (nx === "&") {
|
|
328
|
+
st.redir = "dup";
|
|
329
|
+
j++;
|
|
330
|
+
} else st.redir = "skip";
|
|
331
|
+
i = j;
|
|
332
|
+
} else {
|
|
333
|
+
ensure().text += c;
|
|
334
|
+
i++;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
endSeg(n);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Index of the `)` closing a `$(` whose body starts at `from`; src.length when unclosed.
|
|
341
|
+
function matchParen(src: string, from: number): number {
|
|
342
|
+
let depth = 1;
|
|
343
|
+
let j = from;
|
|
344
|
+
while (j < src.length) {
|
|
345
|
+
const c = src.charAt(j);
|
|
346
|
+
if (c === "\\") j += 2;
|
|
347
|
+
else if (c === "'") {
|
|
348
|
+
const close = src.indexOf("'", j + 1);
|
|
349
|
+
j = close < 0 ? src.length : close + 1;
|
|
350
|
+
} else if (c === '"') {
|
|
351
|
+
j++;
|
|
352
|
+
while (j < src.length && src.charAt(j) !== '"') j += src.charAt(j) === "\\" ? 2 : 1;
|
|
353
|
+
j++;
|
|
354
|
+
} else {
|
|
355
|
+
if (c === "(") depth++;
|
|
356
|
+
else if (c === ")" && --depth === 0) return j;
|
|
357
|
+
j++;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return src.length;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// Index of the backtick closing one opened before `from`; src.length when unclosed.
|
|
364
|
+
function closeTick(src: string, from: number): number {
|
|
365
|
+
let j = from;
|
|
366
|
+
while (j < src.length && src.charAt(j) !== "`") j += src.charAt(j) === "\\" ? 2 : 1;
|
|
367
|
+
return Math.min(j, src.length);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// ---- Shell commands ---------------------------------------------------------------------------
|
|
371
|
+
const ASSIGN = /^[A-Za-z_]\w*=/;
|
|
372
|
+
// Words that only run another command: skipped to reach the command they run.
|
|
373
|
+
const WRAPPERS: Record<string, true> = {
|
|
374
|
+
env: true,
|
|
375
|
+
nohup: true,
|
|
376
|
+
command: true,
|
|
377
|
+
builtin: true,
|
|
378
|
+
exec: true,
|
|
379
|
+
time: true,
|
|
380
|
+
nice: true,
|
|
381
|
+
sudo: true,
|
|
382
|
+
xargs: true,
|
|
383
|
+
timeout: true,
|
|
384
|
+
"!": true,
|
|
385
|
+
"{": true,
|
|
386
|
+
"}": true,
|
|
387
|
+
};
|
|
388
|
+
const SHELLS: Record<string, true> = { bash: true, sh: true, zsh: true, dash: true };
|
|
389
|
+
// Interpreters whose arguments may be code that writes files (bun -e 'writeFileSync(...)').
|
|
390
|
+
const INTERPRETERS: Record<string, true> = {
|
|
391
|
+
...SHELLS,
|
|
392
|
+
bun: true,
|
|
393
|
+
node: true,
|
|
394
|
+
deno: true,
|
|
395
|
+
python: true,
|
|
396
|
+
python3: true,
|
|
397
|
+
perl: true,
|
|
398
|
+
ruby: true,
|
|
399
|
+
};
|
|
400
|
+
const GIT_VALUE_OPTS: Record<string, true> = { "-C": true, "-c": true, "--git-dir": true, "--work-tree": true, "--namespace": true };
|
|
401
|
+
const MUTATING_NAMES: Record<string, true> = { rm: true, mv: true, truncate: true, tee: true, patch: true };
|
|
402
|
+
const GIT_MUTATING: Record<string, true> = { rm: true, mv: true, checkout: true, restore: true };
|
|
403
|
+
const CODE_WRITE = /writeFile|Bun\.write|unlink/;
|
|
404
|
+
|
|
405
|
+
interface ShellCommand {
|
|
406
|
+
name: string;
|
|
407
|
+
args: ShellWord[];
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// The command a segment runs: leading assignments, wrapper words, options and durations are skipped.
|
|
411
|
+
function commandOf(seg: ShellSegment): ShellCommand | null {
|
|
412
|
+
let k = 0;
|
|
413
|
+
for (; k < seg.words.length; k++) {
|
|
414
|
+
const w = seg.words[k];
|
|
415
|
+
if (!w) break;
|
|
416
|
+
const skippable = ASSIGN.test(w.raw) || WRAPPERS[w.text] === true || w.text.startsWith("-") || /^[\d.]+[smhd]?$/.test(w.text);
|
|
417
|
+
if (!skippable) break;
|
|
418
|
+
}
|
|
419
|
+
const head = seg.words[k];
|
|
420
|
+
return head ? { name: path.basename(head.text), args: seg.words.slice(k + 1) } : null;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// git's subcommand and the words after it, skipping global options (-C <dir>, --git-dir=...).
|
|
424
|
+
function gitSub(args: ShellWord[]): { sub: string; rest: ShellWord[] } | null {
|
|
425
|
+
for (let k = 0; k < args.length; k++) {
|
|
426
|
+
const t = args[k]?.text ?? "";
|
|
427
|
+
if (GIT_VALUE_OPTS[t] === true) {
|
|
428
|
+
k++;
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
if (!t.startsWith("-")) return { sub: t, rest: args.slice(k + 1) };
|
|
432
|
+
}
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// A `-c` script as shell source: its quotes removed.
|
|
437
|
+
function scriptText(w: ShellWord): string {
|
|
438
|
+
const r = w.raw;
|
|
439
|
+
if (r.length >= 2 && r.startsWith("'") && r.endsWith("'")) return r.slice(1, -1);
|
|
440
|
+
if (r.length >= 2 && r.startsWith('"') && r.endsWith('"')) return r.slice(1, -1).replace(/\\(["\\$`])/g, "$1");
|
|
441
|
+
return w.text;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Scripts a segment runs through `bash|sh|zsh|dash -c <script>`.
|
|
445
|
+
function shellScripts(seg: ShellSegment): string[] {
|
|
446
|
+
const cmd = commandOf(seg);
|
|
447
|
+
if (!cmd || SHELLS[cmd.name] !== true) return [];
|
|
448
|
+
for (let k = 0; k < cmd.args.length; k++) {
|
|
449
|
+
const w = cmd.args[k];
|
|
450
|
+
if (!w) break;
|
|
451
|
+
if (w.text === "-o" || w.text === "+o") {
|
|
452
|
+
k++;
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
if (/^-[a-zA-Z]*c/.test(w.text)) {
|
|
456
|
+
const script = cmd.args[k + 1];
|
|
457
|
+
return script ? [scriptText(script)] : [];
|
|
458
|
+
}
|
|
459
|
+
if (!w.text.startsWith("-") && !w.text.startsWith("+")) break;
|
|
460
|
+
}
|
|
461
|
+
return [];
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// Every segment that runs, in order: the splitter's segments, each followed by the segments of
|
|
465
|
+
// any `bash -c` script it runs.
|
|
466
|
+
function executed(command: string): ShellSegment[] {
|
|
467
|
+
const out: ShellSegment[] = [];
|
|
468
|
+
for (const seg of splitShell(command)) {
|
|
469
|
+
out.push(seg);
|
|
470
|
+
for (const script of shellScripts(seg)) out.push(...executed(script));
|
|
471
|
+
}
|
|
472
|
+
return out;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Words a segment changes: its output redirections, the operands of mutating commands, the last
|
|
476
|
+
// operand of cp (its destination; the source is only read), and the file arguments of code an
|
|
477
|
+
// interpreter runs.
|
|
478
|
+
function mutationTargets(seg: ShellSegment): string[] {
|
|
479
|
+
const out = [...seg.writes];
|
|
480
|
+
const cmd = commandOf(seg);
|
|
481
|
+
if (!cmd) return out;
|
|
482
|
+
let args = cmd.args;
|
|
483
|
+
let mutating = MUTATING_NAMES[cmd.name] === true;
|
|
484
|
+
if (cmd.name === "git") {
|
|
485
|
+
const g = gitSub(cmd.args);
|
|
486
|
+
args = g?.rest ?? [];
|
|
487
|
+
mutating = g !== null && GIT_MUTATING[g.sub] === true;
|
|
488
|
+
}
|
|
489
|
+
const ops = args.filter((w) => w.text !== "" && !w.text.startsWith("-")).map((w) => w.text);
|
|
490
|
+
if (mutating) out.push(...ops);
|
|
491
|
+
else if (cmd.name === "cp" && ops.length) out.push(ops[ops.length - 1] ?? "");
|
|
492
|
+
else if (cmd.name === "sed" && cmd.args.some((a) => /^-[a-zA-Z]*i|^--in-place/.test(a.text))) out.push(...ops.slice(1));
|
|
493
|
+
const raw = seg.words.map((w) => w.raw).join(" ");
|
|
494
|
+
if (INTERPRETERS[cmd.name] === true && CODE_WRITE.test(raw)) {
|
|
495
|
+
out.push(...raw.split(/[\s;&|<>(),'"]+/).filter((t) => t !== "" && !t.startsWith("-")));
|
|
496
|
+
}
|
|
497
|
+
return out;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// Directories a command may run in: the call's cwd and every directory a `cd` reaches. Subshells
|
|
501
|
+
// and `&&` chains are not tracked, so each relative target is checked against all of them.
|
|
502
|
+
function shellDirs(segs: ShellSegment[], base: string): string[] {
|
|
503
|
+
const dirs = [base];
|
|
504
|
+
let last = base;
|
|
505
|
+
for (const seg of segs) {
|
|
506
|
+
const cmd = commandOf(seg);
|
|
507
|
+
if (cmd?.name !== "cd") continue;
|
|
508
|
+
const dir = cmd.args.find((w) => !w.text.startsWith("-"))?.text ?? "~";
|
|
509
|
+
last = path.resolve(last, expandHome(dir));
|
|
510
|
+
dirs.push(last);
|
|
511
|
+
}
|
|
512
|
+
return dirs;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// Absolute targets of a shell command, each resolved against every directory it may run in.
|
|
516
|
+
function shellTargets(env: BlockerEnv): string[] {
|
|
517
|
+
const base = expandHome(String(env.call.input?.cwd || env.call.cwd || "/"));
|
|
518
|
+
const segs = executed(env.command);
|
|
519
|
+
const dirs = shellDirs(segs, base);
|
|
520
|
+
const out: string[] = [];
|
|
521
|
+
for (const seg of segs) {
|
|
522
|
+
for (const t of mutationTargets(seg)) {
|
|
523
|
+
if (t === "") continue;
|
|
524
|
+
for (const d of dirs) out.push(path.resolve(d, expandHome(t)));
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return out;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// eval code keeps the old whole-text rule: every word of the code counts as a path.
|
|
531
|
+
function evalPaths(env: BlockerEnv): string[] {
|
|
532
|
+
const base = expandHome(String(env.call.input?.cwd || env.call.cwd || "/"));
|
|
144
533
|
const out: string[] = [];
|
|
145
534
|
for (const w of env.command.split(/[\s;&|<>()'"]+/)) {
|
|
146
535
|
const t = w.trim();
|
|
@@ -149,6 +538,18 @@ function shellPaths(env: BlockerEnv): string[] {
|
|
|
149
538
|
return out;
|
|
150
539
|
}
|
|
151
540
|
|
|
541
|
+
// A segment that pushes (git push) or merges an MR/PR (glab mr merge, gh pr merge), as a command.
|
|
542
|
+
function pushesOrMerges(seg: ShellSegment): boolean {
|
|
543
|
+
const cmd = commandOf(seg);
|
|
544
|
+
if (!cmd) return false;
|
|
545
|
+
if (cmd.name === "git") return gitSub(cmd.args)?.sub === "push";
|
|
546
|
+
const words = cmd.args.map((w) => w.text);
|
|
547
|
+
return (
|
|
548
|
+
(cmd.name === "glab" && words[0] === "mr" && words[1] === "merge") ||
|
|
549
|
+
(cmd.name === "gh" && words[0] === "pr" && words[1] === "merge")
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
|
|
152
553
|
// The text a call would write: write content, the added lines of an edit, the whole input of an
|
|
153
554
|
// ast_edit, and the command itself for shell/eval.
|
|
154
555
|
function writtenText(env: BlockerEnv): string {
|
|
@@ -164,24 +565,31 @@ function writtenText(env: BlockerEnv): string {
|
|
|
164
565
|
return env.command;
|
|
165
566
|
}
|
|
166
567
|
|
|
167
|
-
/** deny-command <regex>...: refuse shell
|
|
568
|
+
/** deny-command <regex>...: refuse shell commands matching any regex (use \s for spaces). Each command is matched by its unquoted words, so quoted arguments and heredoc bodies never match; eval code is matched whole. */
|
|
168
569
|
export function denyCommand(env: BlockerEnv): void {
|
|
169
570
|
if (!env.args.length) block("deny-command needs at least one regex in the plan");
|
|
170
|
-
if (env.shell)
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
571
|
+
if (!env.shell) return;
|
|
572
|
+
const texts =
|
|
573
|
+
env.call.tool === "eval"
|
|
574
|
+
? [env.command]
|
|
575
|
+
: executed(env.command).map((seg) => seg.words.filter((w) => !w.quoted).map((w) => w.text).join(" "));
|
|
576
|
+
for (const rx of env.args) {
|
|
577
|
+
const re = new RegExp(rx);
|
|
578
|
+
if (texts.some((t) => re.test(t))) block(`command matches forbidden pattern /${rx}/ (plan Forbidden list)`);
|
|
174
579
|
}
|
|
175
580
|
}
|
|
176
581
|
|
|
177
|
-
/** deny-path <glob>...: refuse
|
|
582
|
+
/** deny-path <glob>...: refuse changes to matching paths: write/edit/ast_edit targets (live plan files excepted: plan-edit governs them), shell redirections and mutating commands. */
|
|
178
583
|
export function denyPath(env: BlockerEnv): void {
|
|
179
584
|
if (!env.args.length) block("deny-path needs at least one glob in the plan");
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
585
|
+
let targets: string[] = [];
|
|
586
|
+
if (env.writes) targets = (env.call.paths ?? []).filter((p) => !MAIN_PLAN_FILE.test(p));
|
|
587
|
+
else if (env.call.tool === "eval") {
|
|
588
|
+
if (CODE_MUTATING.test(env.command)) targets = [...(env.call.paths ?? []), ...evalPaths(env)];
|
|
589
|
+
} else if (env.shell) targets = shellTargets(env);
|
|
590
|
+
for (const p of targets) {
|
|
591
|
+
for (const g of env.args) {
|
|
592
|
+
if (matchesPath(p, g, env.call)) block(`${p} is forbidden by the plan (deny-path ${g})`);
|
|
185
593
|
}
|
|
186
594
|
}
|
|
187
595
|
}
|
|
@@ -197,12 +605,12 @@ export function denyText(env: BlockerEnv): void {
|
|
|
197
605
|
}
|
|
198
606
|
}
|
|
199
607
|
|
|
200
|
-
/** no-push: subagents
|
|
608
|
+
/** no-push: subagents never push or merge (Main pushes and merges). Matched as commands, never as words inside arguments. */
|
|
201
609
|
export function noPush(env: BlockerEnv): void {
|
|
202
|
-
if (
|
|
203
|
-
|
|
204
|
-
env.call.
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
610
|
+
if (!env.shell || env.call.agent === "main") return;
|
|
611
|
+
const hit =
|
|
612
|
+
env.call.tool === "eval"
|
|
613
|
+
? /\bgit\b[^;&|\n]*\bpush\b|\bglab\s+mr\s+merge\b|\bgh\s+pr\s+merge\b/.test(env.command)
|
|
614
|
+
: executed(env.command).some(pushesOrMerges);
|
|
615
|
+
if (hit) block("subagents never push or merge; commit on the feature branch and report");
|
|
208
616
|
}
|