neurastream 0.3.20 → 0.3.21
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 +1 -1
- package/src/adapters/index.ts +3 -2
- package/src/adapters/omp/adapter.ts +2 -1
- package/src/adapters/omp/auto-close.ts +64 -0
- package/src/adapters/omp/chat-commands.ts +161 -0
- package/src/adapters/omp/inbox.ts +221 -0
- package/src/adapters/omp/index.ts +115 -1039
- package/src/adapters/omp/launch.ts +31 -3
- package/src/adapters/omp/rate-limit-controller.ts +196 -0
- package/src/adapters/omp/rollover-controller.ts +334 -0
- package/src/adapters/omp/types.ts +143 -0
- package/src/cli/commands.ts +33 -2
- package/src/cli/dispatch.ts +37 -16
- package/src/cli/recall.ts +3 -2
- package/src/cli/results-build.ts +1 -1
- package/src/cli/workspace.ts +13 -2
- package/src/core/adapter.ts +16 -0
- package/src/core/blockers.ts +326 -30
- package/src/core/file-lock.ts +71 -12
- package/src/core/guards/plan-edit.ts +338 -0
- package/src/core/guards/planner.ts +70 -0
- package/src/core/guards/review.ts +391 -0
- package/src/core/guards/spawn.ts +699 -0
- package/src/core/guards/state.ts +581 -0
- package/src/core/guards/stop.ts +235 -0
- package/src/core/guards/tool.ts +541 -0
- package/src/core/guards.ts +12 -2658
- package/src/core/incidents.ts +125 -91
- package/src/core/machine-guard.ts +23 -5
- package/src/core/main-start.ts +10 -8
- package/src/core/owner-topics.ts +2 -13
- package/src/core/owner-ui/auth.ts +164 -0
- package/src/core/owner-ui/data.ts +437 -0
- package/src/core/owner-ui/handlers.ts +250 -0
- package/src/core/owner-ui/messages.ts +209 -0
- package/src/core/owner-ui/render.ts +714 -0
- package/src/core/owner-ui/results.ts +266 -0
- package/src/core/owner-ui/server.ts +142 -0
- package/src/core/owner-ui.ts +11 -2121
- package/src/core/page-notes.ts +25 -0
- package/src/core/pages.ts +452 -77
- package/src/core/plan-approval.ts +12 -14
- package/src/core/queue/lib.ts +88 -39
- package/src/core/queue-cancel.ts +21 -17
- package/src/core/queue-close.ts +48 -23
- package/src/core/secret-redact.ts +65 -14
- package/src/core/tailscale.ts +79 -3
- package/src/core/team-client.ts +13 -2
- package/src/core/uninstall.ts +2 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
// Plan/code review gates, review spawn locking, open-MR cache, and pending-work checks.
|
|
2
|
+
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import type { PlanEditEvent, ToolEvent } from "../adapter.ts";
|
|
7
|
+
import { nowSec } from "../clock.ts";
|
|
8
|
+
import type { Config } from "../config.ts";
|
|
9
|
+
import { withFileLock, writeAtomicSync } from "../file-lock.ts";
|
|
10
|
+
import { defaultRunner, listOpenMrs, mentioned, type CommandRunner, type OpenMr } from "../mr-sweep.ts";
|
|
11
|
+
import { reviewHash } from "../plan-approval.ts";
|
|
12
|
+
import * as q from "../queue/lib.ts";
|
|
13
|
+
import { unlandedRows } from "../wip-check.ts";
|
|
14
|
+
import { findingsValue, log, newProgressEntries, progressEntries, type Spawner } from "./state.ts";
|
|
15
|
+
|
|
16
|
+
// --- plan review gate state (plan 20261028_1600): the open-MR cache beside the edit window ---
|
|
17
|
+
|
|
18
|
+
export interface MrCache {
|
|
19
|
+
at: number;
|
|
20
|
+
repos: Record<string, OpenMr[]>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function mrCacheFile(paths: Pick<q.QueuePaths, "queue_state">): string {
|
|
24
|
+
return path.join(paths.queue_state, "mr-cache.json");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function readMrCache(paths: Pick<q.QueuePaths, "queue_state">): MrCache {
|
|
28
|
+
try {
|
|
29
|
+
const c = JSON.parse(readFileSync(mrCacheFile(paths), "utf8")) as MrCache;
|
|
30
|
+
if (typeof c.at === "number" && c.repos && typeof c.repos === "object") return c;
|
|
31
|
+
} catch {}
|
|
32
|
+
return { at: 0, repos: {} };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function writeMrCache(c: MrCache, cfg: Config): void {
|
|
36
|
+
const file = mrCacheFile(cfg.paths);
|
|
37
|
+
try {
|
|
38
|
+
withFileLock(`${file}.lock`, () => {
|
|
39
|
+
writeAtomicSync(file, JSON.stringify(c, null, 1));
|
|
40
|
+
});
|
|
41
|
+
} catch (err) {
|
|
42
|
+
log(cfg.paths.guard_log, `cannot write ${file}: ${err}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// --- plan review gate state (plan 20261028_1600): the reviewer PASS record beside the edit window ---
|
|
47
|
+
|
|
48
|
+
export interface PlanReview {
|
|
49
|
+
verdict: "PASS" | "FAIL";
|
|
50
|
+
hash: string;
|
|
51
|
+
finding: string;
|
|
52
|
+
at: number;
|
|
53
|
+
by?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A CODE REVIEW record (P6): the plan phase ids (P1, P4a, …) the entry names, for the tick gate. */
|
|
57
|
+
export interface CodeReview extends PlanReview {
|
|
58
|
+
phases: string[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function planReviewsFile(paths: Pick<q.QueuePaths, "queue_state">): string {
|
|
62
|
+
return path.join(paths.queue_state, "plan-reviews.json");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function readPlanReviews(paths: Pick<q.QueuePaths, "queue_state">): Record<string, PlanReview> {
|
|
66
|
+
try {
|
|
67
|
+
const r = JSON.parse(readFileSync(planReviewsFile(paths), "utf8")) as { reviews?: Record<string, PlanReview> };
|
|
68
|
+
if (r.reviews && typeof r.reviews === "object") return r.reviews;
|
|
69
|
+
} catch {}
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function readCodeReviews(paths: Pick<q.QueuePaths, "queue_state">): Record<string, CodeReview> {
|
|
74
|
+
try {
|
|
75
|
+
const r = JSON.parse(readFileSync(planReviewsFile(paths), "utf8")) as { code?: Record<string, CodeReview> };
|
|
76
|
+
if (r.code && typeof r.code === "object") return r.code;
|
|
77
|
+
} catch {}
|
|
78
|
+
return {};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function writePlanReviewsUnlocked(reviews: Record<string, PlanReview>, code: Record<string, CodeReview>, cfg: Config): void {
|
|
82
|
+
const file = planReviewsFile(cfg.paths);
|
|
83
|
+
try {
|
|
84
|
+
writeAtomicSync(file, JSON.stringify({ reviews, code }, null, 1));
|
|
85
|
+
} catch (err) {
|
|
86
|
+
log(cfg.paths.plan_guard_log, `cannot write ${file}: ${err}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The progress entry that records a review — PLAN for the spawn gate, CODE for the phase tick gate (P6); only a subagent's edit can carry one. The verdict is the match, never a substring. The time may carry a `~` and a zone word (`~23:35 CET`); the author segment may carry a note (`Rev (re-verify): `) because the recorded name comes from the spawn, never from the line. */
|
|
91
|
+
export const REVIEW_LINE = /^- (?:\d{4}-\d{2}-\d{2} ~?\d{2}:\d{2}(?: [A-Z]{2,5})? [^:\n]+?: )?(PLAN|CODE) REVIEW: (PASS|FAIL)\b/;
|
|
92
|
+
/** Any entry that opens like a review line; one that REVIEW_LINE does not match is restored, not recorded. */
|
|
93
|
+
export const REVIEW_LOOKALIKE = /^- (?:\d{4}-\d{2}-\d{2} ~?\d{2}:\d{2}(?: [A-Z]{2,5})? [^:\n]+?: )?(PLAN|CODE) REVIEW\b/;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* P2 (plan 20261028_1600): a spawn on a plan needs a recorded reviewer PASS whose hash still
|
|
97
|
+
* matches the plan text — progress-only edits keep a PASS valid, any other change expires it,
|
|
98
|
+
* a FAIL is denied with its first finding. A review spawn itself — an agent from review.agents
|
|
99
|
+
* whose brief has the `Plan review: <plan>` line — is exempt when Main launches it. P5 removed
|
|
100
|
+
* the cutover: every named plan is gated. review.plan: false skips the gate (P6); an empty
|
|
101
|
+
* review.agents list no longer turns it off.
|
|
102
|
+
*/
|
|
103
|
+
export function reviewPassProblem(named: string[], brief: string, agent: string, cfg: Config, spawner: Spawner): string | null {
|
|
104
|
+
if (!cfg.review.plan) return null; // P6: review.plan: false skips the PASS gate; an empty review.agents list no longer does
|
|
105
|
+
const reviews = readPlanReviews(cfg.paths);
|
|
106
|
+
for (const file of named) {
|
|
107
|
+
if (isReviewSpawn(file, brief, agent, cfg, spawner)) continue;
|
|
108
|
+
const rec = reviews[file];
|
|
109
|
+
if (!rec) return `plan ${file} has no current reviewer PASS. Spawn a reviewer with templates/plan-review-brief.md.`;
|
|
110
|
+
if (rec.verdict === "FAIL")
|
|
111
|
+
return `plan ${file} has no current reviewer PASS (FAIL: ${rec.finding || "no finding recorded"}). Spawn a reviewer with templates/plan-review-brief.md.`;
|
|
112
|
+
let text = "";
|
|
113
|
+
try {
|
|
114
|
+
text = readFileSync(file, "utf8");
|
|
115
|
+
} catch {}
|
|
116
|
+
if (rec.hash !== reviewHash(text))
|
|
117
|
+
return `plan ${file} has no current reviewer PASS (changed since review). Spawn a reviewer with templates/plan-review-brief.md.`;
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A subagent's `PLAN REVIEW` or `CODE REVIEW` (P6) entry records the review of the plan it edited; Main's edit path never reaches this. */
|
|
123
|
+
export function recordReviews(e: PlanEditEvent, cfg: Config): void {
|
|
124
|
+
const entries = newProgressEntries(e.before, e.after).filter(b => REVIEW_LINE.test(b));
|
|
125
|
+
if (!entries.length) return;
|
|
126
|
+
// Lock order: review-spawns.json.lock -> plan-reviews.json.lock (strictly acyclic).
|
|
127
|
+
withFileLock(`${reviewSpawnsFile(cfg.paths)}.lock`, () => {
|
|
128
|
+
const spawns = readReviewSpawns(cfg.paths);
|
|
129
|
+
const found = matchReviewSpawn(spawns, e.agentId ?? "");
|
|
130
|
+
if (!found) return;
|
|
131
|
+
const { name, spawn } = found;
|
|
132
|
+
withFileLock(`${planReviewsFile(cfg.paths)}.lock`, () => {
|
|
133
|
+
const reviews = readPlanReviews(cfg.paths);
|
|
134
|
+
const code = readCodeReviews(cfg.paths);
|
|
135
|
+
for (const block of entries) {
|
|
136
|
+
const kind = REVIEW_LINE.exec(block)![1];
|
|
137
|
+
const repeat = spawn.verdicts > 0;
|
|
138
|
+
spawn.verdicts++;
|
|
139
|
+
const at = nowSec();
|
|
140
|
+
// Judged against the plan as the spawn first saw it (P4k): a plan that moved during the review cannot pass.
|
|
141
|
+
const record: PlanReview = repeat
|
|
142
|
+
? { verdict: "FAIL", hash: spawn.hash, finding: "review spawn wrote 2 verdicts", at, by: name }
|
|
143
|
+
: reviewHash(e.after) !== spawn.hash
|
|
144
|
+
? { verdict: "FAIL", hash: spawn.hash, finding: "plan changed during review", at, by: name }
|
|
145
|
+
: { verdict: REVIEW_LINE.exec(block)![2] === "PASS" ? "PASS" : "FAIL", hash: spawn.hash, finding: findingsValue(block) ?? "", at, by: name };
|
|
146
|
+
// A CODE REVIEW record covers only the phases the verdict names before its Findings line: a phase in the Findings text ("see P2") is a reference, not coverage; an entry without a Findings line covers nothing.
|
|
147
|
+
if (kind === "CODE") {
|
|
148
|
+
const head = block.slice(0, Math.max(block.indexOf("Findings:"), 0));
|
|
149
|
+
code[e.path] = { ...record, phases: [...new Set([...head.matchAll(/\bP\d+[a-z]?\b/g)].map(m => m[0]))] };
|
|
150
|
+
} else reviews[e.path] = record;
|
|
151
|
+
}
|
|
152
|
+
writePlanReviewsUnlocked(reviews, code, cfg);
|
|
153
|
+
});
|
|
154
|
+
writeReviewSpawnsUnlocked(spawns, cfg.paths);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** P4k backstop, at a sub agent's end: a named review spawn whose PLAN REVIEW line reached no edit (a path the write targets miss) is recorded here. Returns Main's note when the spawn wrote no verdict at all. */
|
|
159
|
+
export function reviewSpawnEnd(agentId: string, cfg: Config): string | null {
|
|
160
|
+
const found = matchReviewSpawn(readReviewSpawns(cfg.paths), agentId);
|
|
161
|
+
if (!found || found.spawn.verdicts > 0) return null;
|
|
162
|
+
const { name, spawn } = found;
|
|
163
|
+
const after = readPlanText(spawn.plan);
|
|
164
|
+
if (newProgressEntries(spawn.text, after).some(b => REVIEW_LINE.test(b))) {
|
|
165
|
+
recordReviews({ path: spawn.plan, before: spawn.text, after, agent: "sub", agentId: name }, cfg);
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
return `${name} wrote no verdict on ${spawn.plan}.`;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** P4f: a `Plan review: <plan>` brief from Main, by a configured review agent (the exemption from the plan gate). */
|
|
172
|
+
export function isReviewSpawn(file: string, brief: string, agent: string, cfg: Config, spawner: Spawner): boolean {
|
|
173
|
+
const reviewLines = brief.split("\n").filter(l => l.startsWith("Plan review:"));
|
|
174
|
+
return spawner === "main" && cfg.review.agents.includes(agent) && reviewLines.some(l => l.slice("Plan review:".length).trim() === file);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** P4f: the review role of one task: the plan it reviews (null for other spawns). A review spawn needs a name, the name keys its lock, and it reviews one plan. */
|
|
178
|
+
export function reviewRole(name: string | undefined, named: string[], brief: string, agent: string, cfg: Config, spawner: Spawner): { plan: string | null; problem: string | null } {
|
|
179
|
+
const reviewed = named.filter(f => isReviewSpawn(f, brief, agent, cfg, spawner));
|
|
180
|
+
if (reviewed.length > 1) return { plan: null, problem: "a review spawn reviews one plan; split the spawn." };
|
|
181
|
+
if (reviewed.length && !name) return { plan: null, problem: "a review spawn needs a name (the task name keys its review lock)." };
|
|
182
|
+
if (reviewed.length && !/^Scope: whole plan\s*$/m.test(brief)) return { plan: null, problem: "a review spawn reviews the whole plan: add the line `Scope: whole plan` to the brief (every unticked phase, one pass; no per-phase reviews)." };
|
|
183
|
+
const plan = reviewed[0] ?? null;
|
|
184
|
+
if (plan) {
|
|
185
|
+
const rec = readPlanReviews(cfg.paths)[plan];
|
|
186
|
+
if (rec?.verdict === "PASS" && rec.hash === reviewHash(readPlanText(plan)))
|
|
187
|
+
return { plan: null, problem: `plan ${plan} already has a current reviewer PASS (by ${rec.by}); a second review is waste. Change the plan text, or spawn the implementer.` };
|
|
188
|
+
}
|
|
189
|
+
return { plan, problem: null };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** P4f/P4k: the named review spawns, spawn name → its review role: the plan, the plan text and hash as the spawn first saw them, and the verdicts it wrote (review-spawns.json in the state dir). */
|
|
193
|
+
export interface ReviewSpawn {
|
|
194
|
+
plan: string;
|
|
195
|
+
text: string;
|
|
196
|
+
hash: string;
|
|
197
|
+
verdicts: number;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function reviewSpawnsFile(paths: Pick<q.QueuePaths, "queue_state">): string {
|
|
201
|
+
return path.join(paths.queue_state, "review-spawns.json");
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function readReviewSpawns(paths: Pick<q.QueuePaths, "queue_state">): Record<string, ReviewSpawn> {
|
|
205
|
+
try {
|
|
206
|
+
const parsed = JSON.parse(readFileSync(reviewSpawnsFile(paths), "utf8"));
|
|
207
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
208
|
+
} catch {
|
|
209
|
+
return {};
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function writeReviewSpawnsUnlocked(spawns: Record<string, ReviewSpawn>, paths: Pick<q.QueuePaths, "queue_state">): void {
|
|
214
|
+
writeAtomicSync(reviewSpawnsFile(paths), `${JSON.stringify(spawns, null, 2)}\n`);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function readPlanText(file: string): string {
|
|
218
|
+
try {
|
|
219
|
+
return readFileSync(file, "utf8");
|
|
220
|
+
} catch {
|
|
221
|
+
return "";
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** Set (plan) or drop (null) a named spawn's review role; a non-review spawn under the same name drops it. */
|
|
226
|
+
export function recordReviewSpawn(name: string, plan: string | null, paths: Pick<q.QueuePaths, "queue_state">): void {
|
|
227
|
+
withFileLock(`${reviewSpawnsFile(paths)}.lock`, () => {
|
|
228
|
+
const spawns = readReviewSpawns(paths);
|
|
229
|
+
if (plan) {
|
|
230
|
+
const text = readPlanText(plan);
|
|
231
|
+
spawns[name] = { plan, text, hash: reviewHash(text), verdicts: 0 };
|
|
232
|
+
} else delete spawns[name];
|
|
233
|
+
writeReviewSpawnsUnlocked(spawns, paths);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** The review role an agent id belongs to: its spawn name, or the name without a -<digits> suffix (a sub agent's id, plan P4f gap). */
|
|
238
|
+
export function matchReviewSpawn(spawns: Record<string, ReviewSpawn>, agentId: string): { name: string; spawn: ReviewSpawn } | null {
|
|
239
|
+
for (const name of [agentId, agentId.replace(/-\d+$/, "")]) {
|
|
240
|
+
const spawn = Object.hasOwn(spawns, name) ? spawns[name] : undefined;
|
|
241
|
+
if (spawn && typeof spawn.plan === "string") return { name, spawn };
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** P4f: the secret-read deny: credential dirs under a home, and env files. Shell text expands `~` to the process home (queue lib) while config points at the sandbox home in tests, so both roots are judged; they are the same directory in production. */
|
|
247
|
+
function secretPath(p: string, cfg: Config, cwd: string): boolean {
|
|
248
|
+
const abs = path.resolve(cwd, p.startsWith("~/") ? path.join(cfg.paths.home, p.slice(2)) : p);
|
|
249
|
+
const base = path.basename(abs);
|
|
250
|
+
if (base.startsWith(".env") || base.endsWith(".env")) return true;
|
|
251
|
+
return [cfg.paths.home, homedir()].some(home =>
|
|
252
|
+
[".config", ".ssh", ".gnupg", ".aws"].some(d => abs.startsWith(path.join(path.resolve(home), d) + path.sep)),
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// P7 (plan 20261028_1600): option tokens that write or execute on an otherwise allowed command;
|
|
257
|
+
// `-w`/`--web` are denied on gh/glab segments only (they open a browser), `-c` only right after
|
|
258
|
+
// `git` (config injection, e.g. core.pager=sh).
|
|
259
|
+
const SHELL_DENY_OPTS = ["--output", "--pre", "--ext-diff", "--textconv", "--open-files-in-pager", "--exec", "-O"];
|
|
260
|
+
|
|
261
|
+
/** P7 (plan 20261028_1600): why a review spawn's bash command is not allowed, or null. Single
|
|
262
|
+
* line, no `$(`/backtick/`<(`/`>(`, no `>` outside quotes (quoted text stripped the way
|
|
263
|
+
* withoutQuotedRedirects does), and every `|`/`&&`/`||`/`;`/`&` segment starts with a
|
|
264
|
+
* review.shell_allow entry followed by a word boundary. */
|
|
265
|
+
function reviewShellProblem(command: string, cfg: Config): string | null {
|
|
266
|
+
if (/[\r\n]/.test(command)) return "a multi-line command";
|
|
267
|
+
if (command.includes("`") || command.includes("$(") || command.includes("<(") || command.includes(">(")) return "command substitution";
|
|
268
|
+
if (q.withoutQuotedRedirects(command).includes(">")) return "a shell redirect";
|
|
269
|
+
for (const raw of command.split(/&&|\|\||[|;&]/)) {
|
|
270
|
+
const seg = raw.trim();
|
|
271
|
+
const toks = seg.split(/\s+/);
|
|
272
|
+
const pre = cfg.review.shell_allow.find(p => seg.startsWith(p) && (seg.length === p.length || /\s/.test(seg.charAt(p.length))));
|
|
273
|
+
if (!pre) return `${toks[0] || "an empty segment"} is not a review.shell_allow command`;
|
|
274
|
+
const opt = toks.find(t => SHELL_DENY_OPTS.some(o => t === o || t.startsWith(`${o}=`)));
|
|
275
|
+
if (opt) return `the writing/executing option ${opt}`;
|
|
276
|
+
if ((toks[0] === "gh" || toks[0] === "glab") && toks.some(t => t === "-w" || t === "--web")) return "gh/glab --web opens a browser";
|
|
277
|
+
if (toks.some((t, i) => i > 0 && t === "-c" && toks[i - 1] === "git")) return "git -c config injection";
|
|
278
|
+
}
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** P4f: a named review spawn may read, use todo, and edit only its own plan. Shell, spawn, cancel and MCP tools are denied; so are secret reads. */
|
|
283
|
+
export function reviewLockProblem(e: ToolEvent, cfg: Config): string | null {
|
|
284
|
+
const plan = matchReviewSpawn(readReviewSpawns(cfg.paths), e.agentId ?? "")?.spawn.plan ?? null;
|
|
285
|
+
if (!plan) return null;
|
|
286
|
+
const input = e.input ?? {};
|
|
287
|
+
if (cfg.guards.write_tools.includes(e.tool)) {
|
|
288
|
+
const writes = [...(e.writes ?? []), ...q.writeTargets(e.tool, input, e.cwd, cfg.paths, [])];
|
|
289
|
+
if (writes.length && writes.every(w => path.resolve(e.cwd, w) === path.resolve(plan))) return null;
|
|
290
|
+
return `review spawn ${e.agentId} may edit only its plan ${plan}.`;
|
|
291
|
+
}
|
|
292
|
+
if (cfg.guards.read_tools.includes(e.tool) || e.tool === "todo") {
|
|
293
|
+
const reads = [...(e.paths ?? []), ...q.touchedPaths(e.tool, input, e.cwd)];
|
|
294
|
+
return reads.some(p => secretPath(p, cfg, e.cwd)) ? `review spawn ${e.agentId} may not read credentials or env files.` : null;
|
|
295
|
+
}
|
|
296
|
+
// P7 (plan 20261028_1600): the reviewer's shell is an allow list, not a ban list — eval never
|
|
297
|
+
// (it runs code), and bash only when every |/&&/||/;/& segment starts with a review.shell_allow
|
|
298
|
+
// command: single-line, no substitution or redirect characters, no writing/executing option
|
|
299
|
+
// tokens. The secret-path deny stays. (I-20261031-348: the old MUTATING ban missed
|
|
300
|
+
// fs.renameSync-style writes; I-20261031-350: a reviewer's shell ran anything unlisted.)
|
|
301
|
+
if (cfg.guards.shell_tools.includes(e.tool)) {
|
|
302
|
+
if (e.tool === "eval")
|
|
303
|
+
return `review spawn ${e.agentId} may not run eval; use the read tools or a bash command from review.shell_allow.`;
|
|
304
|
+
const problem = reviewShellProblem(String(input.command ?? ""), cfg);
|
|
305
|
+
if (problem) return `review spawn ${e.agentId} may run only review.shell_allow commands: ${problem}.`;
|
|
306
|
+
const reads = [...(e.paths ?? []), ...q.touchedPaths(e.tool, input, e.cwd)];
|
|
307
|
+
return reads.some(p => secretPath(p, cfg, e.cwd)) ? `review spawn ${e.agentId} may not read credentials or env files.` : null;
|
|
308
|
+
}
|
|
309
|
+
return `review spawn ${e.agentId} may only read, use todo, and edit its plan; ${e.tool} is denied.`;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** The brief names pending work the P3 way: `MR !N: <what it holds> — this task: <why not overlapping>` (GitHub `#N`), `Branch <name>: …`. */
|
|
313
|
+
export function briefNamesPending(brief: string, prefix: "MR" | "Branch", token: string): boolean {
|
|
314
|
+
const esc = token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
315
|
+
return new RegExp(`^[ \\t]*(?:[-*][ \\t]+)?${prefix} ${esc}:[^\\n]*this task:`, "im").test(brief);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** The branch a linked worktree has checked out, or null. */
|
|
319
|
+
function worktreeBranch(wt: string | null, run: CommandRunner): string | null {
|
|
320
|
+
if (!wt) return null;
|
|
321
|
+
const r = run(["git", "-C", wt, "rev-parse", "--abbrev-ref", "HEAD"], {});
|
|
322
|
+
return r.code === 0 ? r.stdout.trim() || null : null;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* P3 (plan 20261028_1600): done-but-not-merged work must be named by the brief before a spawn
|
|
327
|
+
* runs on its plan. (a) open MRs of the plan's repo that the plan mentions (mr-sweep's lister and
|
|
328
|
+
* `mentioned`; the list is cached `review.mr_cache_minutes`); (b) the plan's `Branch:` line or its
|
|
329
|
+
* worktree's checked-out branch when wip-check reports it UNLANDED — still enforced when the forge
|
|
330
|
+
* is unreachable, whose degrade skips only (a), like the sweep. A repo with no origin remote has
|
|
331
|
+
* no merge target: both checks skip it with one guard-log line.
|
|
332
|
+
*/
|
|
333
|
+
export function pendingWorkProblem(named: string[], brief: string, cfg: Config, run: CommandRunner = defaultRunner): string | null {
|
|
334
|
+
const plans = q.activePlans(cfg.paths).filter(p => named.includes(p.file));
|
|
335
|
+
if (!plans.length) return null;
|
|
336
|
+
const cache = readMrCache(cfg.paths);
|
|
337
|
+
const fresh = nowSec() - cache.at < cfg.review.mr_cache_minutes * 60;
|
|
338
|
+
const openMrs = new Map<string, OpenMr[]>();
|
|
339
|
+
const hasOrigin = new Set<string>();
|
|
340
|
+
let cacheDirty = false;
|
|
341
|
+
for (const repo of new Set(plans.map(p => p.repo))) {
|
|
342
|
+
if (fresh && cache.repos[repo]) {
|
|
343
|
+
openMrs.set(repo, cache.repos[repo]);
|
|
344
|
+
hasOrigin.add(repo);
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
const listed = listOpenMrs(repo, run);
|
|
348
|
+
if (!listed.ok) {
|
|
349
|
+
log(cfg.paths.guard_log, `pending-work: ${repo}: ${listed.why}; the open-MR check is skipped`);
|
|
350
|
+
if (listed.why !== "no origin remote") hasOrigin.add(repo); // forge down still enforces (b)
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
openMrs.set(repo, listed.mrs);
|
|
354
|
+
hasOrigin.add(repo);
|
|
355
|
+
cache.repos[repo] = listed.mrs;
|
|
356
|
+
cacheDirty = true;
|
|
357
|
+
}
|
|
358
|
+
if (cacheDirty) writeMrCache({ at: nowSec(), repos: cache.repos }, cfg);
|
|
359
|
+
const unlanded = hasOrigin.size ? new Set(unlandedRows(run(["wip-check"], {}).stdout).map(r => r.branch)) : null;
|
|
360
|
+
for (const p of plans) {
|
|
361
|
+
// (a) only MRs the plan itself mentions are pending work here: the sweep already flags
|
|
362
|
+
// unmentioned open MRs as UNTRACKED for the whole queue.
|
|
363
|
+
for (const mr of openMrs.get(p.repo) ?? []) {
|
|
364
|
+
if (!mentioned(mr.token, mr.branch, [p.text]) || briefNamesPending(brief, "MR", mr.token)) continue;
|
|
365
|
+
return `plan ${p.file} has pending work MR ${mr.token} not merged; say in the brief what it already holds and why this task does not redo it ("MR ${mr.token}: <what it holds> — this task: <why not overlapping>").`;
|
|
366
|
+
}
|
|
367
|
+
if (!unlanded || !hasOrigin.has(p.repo)) continue;
|
|
368
|
+
for (const b of [p.branch, worktreeBranch(p.worktree, run)]) {
|
|
369
|
+
if (!b || !unlanded.has(b) || briefNamesPending(brief, "Branch", b)) continue;
|
|
370
|
+
return `plan ${p.file} has pending work branch ${b} not merged; say in the brief what it already holds and why this task does not redo it ("Branch ${b}: <what it holds> — this task: <why not overlapping>").`;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** P6 (plan 20261028_1600): the refusal for ticking `ids` with review.code on. Each phase needs a recorded CODE REVIEW: PASS from a named review spawn (the `code` map of plan-reviews.json), and its entry must still be the plan's last Progress entry naming the phase — last by the entry's `<YYYY-MM-DD HH:MM>` stamp, not file order (live plans are newest-first or appended): implementer work after the review voids it. */
|
|
377
|
+
export function codeReviewProblem(file: string, after: string, ids: string[], paths: Pick<q.QueuePaths, "queue_state">): string | null {
|
|
378
|
+
const rec = readCodeReviews(paths)[file];
|
|
379
|
+
// toSorted is stable: ties and unstamped entries ("") keep file order.
|
|
380
|
+
const entries = progressEntries(after)
|
|
381
|
+
.map(b => [b, /^\s*-\s*(\d{4}-\d{2}-\d{2} \d{2}:\d{2})/.exec(b)?.[1] ?? ""] as const)
|
|
382
|
+
.toSorted((a, b) => (a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0))
|
|
383
|
+
.map(([b]) => b);
|
|
384
|
+
for (const id of ids) {
|
|
385
|
+
const last = entries.findLast(b => new RegExp(`\\b${id}\\b`).test(b));
|
|
386
|
+
const valid = last !== undefined && REVIEW_LINE.test(last) && REVIEW_LINE.exec(last)![1] === "CODE" && REVIEW_LINE.exec(last)![2] === "PASS";
|
|
387
|
+
if (!(rec?.verdict === "PASS" && rec.phases.includes(id) && valid))
|
|
388
|
+
return `plan-guard: phase ${id} was ticked but has no current CODE REVIEW: PASS from a review spawn after its last implementer entry — spawn a code reviewer or set review.code: false. The file was restored.`;
|
|
389
|
+
}
|
|
390
|
+
return null;
|
|
391
|
+
}
|