neurastream 0.3.20 → 0.3.22
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 +1 -0
- 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/doctor.ts +6 -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 +44 -16
- package/src/cli/recall.ts +3 -2
- package/src/cli/results-build.ts +1 -1
- package/src/cli/reuse.ts +109 -0
- package/src/cli/setup.ts +25 -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/config.ts +29 -0
- package/src/core/file-lock.ts +71 -12
- package/src/core/guards/plan-edit.ts +530 -0
- package/src/core/guards/planner.ts +70 -0
- package/src/core/guards/review.ts +391 -0
- package/src/core/guards/spawn.ts +709 -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 +146 -43
- package/src/core/queue-cancel.ts +21 -17
- package/src/core/queue-close.ts +48 -23
- package/src/core/reuse-map.ts +742 -0
- package/src/core/secret-redact.ts +65 -14
- package/src/core/setup-detect.ts +8 -3
- package/src/core/tailscale.ts +79 -3
- package/src/core/team-client.ts +13 -2
- package/src/core/uninstall.ts +16 -0
- package/templates/config.yml +7 -0
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
// Plan edit verification: subagent Progress-only / planner-body / Findings / review rules, and Main read-evidence checks.
|
|
2
|
+
|
|
3
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import type { PlanEditEvent } from "../adapter.ts";
|
|
6
|
+
import { currentPlanner } from "../autonomy.ts";
|
|
7
|
+
import { heldByOther } from "../claims.ts";
|
|
8
|
+
import type { Config } from "../config.ts";
|
|
9
|
+
import { MAIN_ONLY_LINE } from "../plan-approval.ts";
|
|
10
|
+
import * as q from "../queue/lib.ts";
|
|
11
|
+
import { claimStoreFor } from "../team-client.ts";
|
|
12
|
+
import {
|
|
13
|
+
MAX_TERMS,
|
|
14
|
+
NONE_TERMS_MIN,
|
|
15
|
+
SLOW_RETRY,
|
|
16
|
+
citeRoots,
|
|
17
|
+
citedQuotes,
|
|
18
|
+
freshGraph,
|
|
19
|
+
hasBareChecked,
|
|
20
|
+
isMapOrNoneLine,
|
|
21
|
+
mapLine,
|
|
22
|
+
normRel,
|
|
23
|
+
optionTermProblems,
|
|
24
|
+
parseMapLine,
|
|
25
|
+
parseNoneLine,
|
|
26
|
+
planTerms,
|
|
27
|
+
queryBudget,
|
|
28
|
+
readConfined,
|
|
29
|
+
realGraphify,
|
|
30
|
+
searchTerms,
|
|
31
|
+
versionProblem,
|
|
32
|
+
type CitedQuote,
|
|
33
|
+
type QueryBudget,
|
|
34
|
+
type ReuseGraph,
|
|
35
|
+
} from "../reuse-map.ts";
|
|
36
|
+
import { plannerBody, plannerSpawnPlan } from "./planner.ts";
|
|
37
|
+
import { codeReviewProblem, matchReviewSpawn, readReviewSpawns, recordReviews, REVIEW_LINE, REVIEW_LOOKALIKE } from "./review.ts";
|
|
38
|
+
import {
|
|
39
|
+
ALLOW,
|
|
40
|
+
findingsValue,
|
|
41
|
+
heldProblem,
|
|
42
|
+
log,
|
|
43
|
+
MAIN_PLAN_FILE,
|
|
44
|
+
mainId,
|
|
45
|
+
newlyTicked,
|
|
46
|
+
newProgressEntries,
|
|
47
|
+
readMainReads,
|
|
48
|
+
type PlanEditDecision,
|
|
49
|
+
} from "./state.ts";
|
|
50
|
+
|
|
51
|
+
const GREP_LINE = /^([^\s:]+):\d+:/gm;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The pre-draft warning (I-20261025-208): when Main reads a plan file (the read tool, or a result
|
|
55
|
+
* whose first `path:line:` hit is the plan), the tool result gains one note naming the plan's
|
|
56
|
+
* listed files that would fail the read check right now — before Main drafts an edit the guard
|
|
57
|
+
* would refuse and throw away. Silence when every listed file is fresh in some checkout.
|
|
58
|
+
*/
|
|
59
|
+
export function planReadNote(tool: string, input: Record<string, unknown>, cwd: string, text: string, cfg: Config): string | undefined {
|
|
60
|
+
let planPath = "";
|
|
61
|
+
if (tool === "read" && typeof input.path === "string") planPath = path.resolve(cwd, q.expandHome(input.path));
|
|
62
|
+
else {
|
|
63
|
+
const first = [...text.matchAll(GREP_LINE)][0];
|
|
64
|
+
if (first) {
|
|
65
|
+
const base = typeof input.cwd === "string" ? path.resolve(cwd, q.expandHome(input.cwd)) : cwd;
|
|
66
|
+
planPath = path.resolve(q.shellCwd(String(input.command ?? input.code ?? ""), base), first[1] ?? "");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (!planPath || !MAIN_PLAN_FILE.test(planPath)) return undefined;
|
|
70
|
+
let plan: string;
|
|
71
|
+
try {
|
|
72
|
+
plan = readFileSync(planPath, "utf8");
|
|
73
|
+
} catch {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
const stale = planReadProblems(plan, planPath, cfg)
|
|
77
|
+
.filter(p => p.includes(" was not read by Main within "))
|
|
78
|
+
.map(p => p.slice(0, p.indexOf(" was not read by Main within ")));
|
|
79
|
+
if (!stale.length) return undefined;
|
|
80
|
+
return `plan-guard: before you edit this plan, re-read: ${stale.join(", ")}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function mainOnlyLines(text: string): string {
|
|
84
|
+
return text.split("\n").filter(l => MAIN_ONLY_LINE.test(l)).join("\n");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* plan-edit: after a subagent changed a plan, everything outside "## Progress" and every
|
|
89
|
+
* Main-only line must be unchanged, and every Progress entry the edit adds must carry a
|
|
90
|
+
* "Findings:" line (2026-10-24: a finding buried in a Progress note never became a plan);
|
|
91
|
+
* `restore` hands back the untouched text for the caller to write (the original restored the
|
|
92
|
+
* file itself before reporting).
|
|
93
|
+
*/
|
|
94
|
+
export function checkPlanEdit(e: PlanEditEvent, cfg: Config, graph: ReuseGraph = realGraphify): PlanEditDecision {
|
|
95
|
+
if (e.agent === "main") return checkMainPlan(e, cfg, graph);
|
|
96
|
+
// planner mode P2 (b): the plan bound to this sub by its approved spawn (the `Planner role for`
|
|
97
|
+
// line) is the planner agent's to write — body freedom, but never the Main-only lines, and the
|
|
98
|
+
// header names its agent. Only while delegated: in main mode the spawn gate refused the
|
|
99
|
+
// Planner-role brief, and the plain Progress-only rule holds.
|
|
100
|
+
if (currentPlanner(cfg) === "delegated" && e.agentId !== undefined && plannerSpawnPlan(e.agentId, cfg.paths) === e.path) {
|
|
101
|
+
if (mainOnlyLines(e.after) !== mainOnlyLines(e.before)) {
|
|
102
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (planner wrote a Main-only line)`);
|
|
103
|
+
return {
|
|
104
|
+
allow: false,
|
|
105
|
+
reason: `plan-guard: the Main-only lines ("Status:", "WAITING on owner:", "Waits on:", "Cancelled by owner:", "False positive:") of ${e.path} are never a planner agent's. The file was restored.`,
|
|
106
|
+
restore: { path: e.path, text: e.before || null },
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const name = e.agentId ?? "";
|
|
110
|
+
// The header records the agent the owner picked, not the spawn instance: a `glm-3` spawn
|
|
111
|
+
// satisfies `Planner: glm`, and the spawn form still works for plans written before this.
|
|
112
|
+
const names = new Set([name, name.replace(/-\d+$/, "")]);
|
|
113
|
+
const head = e.after.split(/^## /m)[0] ?? "";
|
|
114
|
+
if (!head.split("\n").some((l) => l.trim().startsWith("Planner: ") && names.has(l.trim().slice("Planner: ".length).trim()))) {
|
|
115
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (no Planner header)`);
|
|
116
|
+
return {
|
|
117
|
+
allow: false,
|
|
118
|
+
reason: `plan-guard: ${e.path} carries no "Planner: ${name}" header — a planner agent's plan names its agent above the first section. The file was restored.`,
|
|
119
|
+
restore: { path: e.path, text: e.before || null },
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
// Reuse map (plan 20261122_1828_reuse_map P2): the planner's plan body carries the map
|
|
123
|
+
// line or none line too — the same write-time check Main's plan text faces.
|
|
124
|
+
const reuse = reuseMapWriteProblems(e.after, e.path, cfg, graph);
|
|
125
|
+
if (reuse.length) {
|
|
126
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (reuse map)`);
|
|
127
|
+
return {
|
|
128
|
+
allow: false,
|
|
129
|
+
reason: `plan-guard: ${e.path} fails the reuse map gate:\n- ${reuse.join("\n- ")}\nThe file was restored.`,
|
|
130
|
+
restore: { path: e.path, text: e.before || null },
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
} else if (q.outsideProgress(e.after) !== q.outsideProgress(e.before) || mainOnlyLines(e.after) !== mainOnlyLines(e.before)) {
|
|
134
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path}`);
|
|
135
|
+
return {
|
|
136
|
+
allow: false,
|
|
137
|
+
reason: `plan-guard: you changed ${e.path} outside your progress entries (only "## Progress" entries are yours; the "Status:", "WAITING on owner:", "Waits on:", "Cancelled by owner:" and "False positive:" lines are Main's, it marks a plan complete after reviewing your work). The file was restored.`,
|
|
138
|
+
restore: { path: e.path, text: e.before },
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
const missing = newProgressEntries(e.before, e.after).filter(b => !findingsValue(b));
|
|
142
|
+
if (missing.length) {
|
|
143
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (no Findings line)`);
|
|
144
|
+
return {
|
|
145
|
+
allow: false,
|
|
146
|
+
reason: `plan-guard: ${missing.length === 1 ? "your new entry in" : `${missing.length} of your new entries in`} ${e.path} ${missing.length === 1 ? "has" : "have"} no "Findings:" line. End every new "## Progress" entry with "Findings: none", or "Findings: <one item per finding (bug, workaround, improvement — any repo), each with file:line evidence>", items separated by ";". The file was restored.`,
|
|
147
|
+
restore: { path: e.path, text: e.before },
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
const loose = newProgressEntries(e.before, e.after).filter(b => REVIEW_LOOKALIKE.test(b) && !REVIEW_LINE.test(b));
|
|
151
|
+
if (loose.length) {
|
|
152
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (review line)`);
|
|
153
|
+
return {
|
|
154
|
+
allow: false,
|
|
155
|
+
reason: `plan-guard: a PLAN REVIEW or CODE REVIEW line in ${e.path} does not match "- [<date> <time> <name>: ]PLAN REVIEW|CODE REVIEW: PASS|FAIL — …". The file was restored.`,
|
|
156
|
+
restore: { path: e.path, text: e.before },
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
if (e.agent === "sub" && newProgressEntries(e.before, e.after).some(b => REVIEW_LINE.test(b)) && matchReviewSpawn(readReviewSpawns(cfg.paths), e.agentId ?? "")?.spawn.plan !== e.path) {
|
|
160
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} review line from a spawn that is not its named review spawn`);
|
|
161
|
+
return { allow: false, reason: `plan-guard: a PLAN REVIEW or CODE REVIEW line counts only from the named review spawn of ${e.path}; the file was restored.`, restore: { path: e.path, text: e.before } };
|
|
162
|
+
}
|
|
163
|
+
recordReviews(e, cfg);
|
|
164
|
+
return ALLOW;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Main's plan text outside "## Progress" must rest on files Main read itself (planReadProblems).
|
|
168
|
+
// An empty `before` means the plan file did not exist yet; a refusal then deletes it.
|
|
169
|
+
function checkMainPlan(e: PlanEditEvent, cfg: Config, graph: ReuseGraph): PlanEditDecision {
|
|
170
|
+
// planner mode P2 (a): while delegated, Main plans nothing — a new plan file or a rewrite of
|
|
171
|
+
// the text outside "## Progress" is the planner agent's work. Main keeps its Progress entries,
|
|
172
|
+
// the Main-only lines (Status / WAITING on owner / Waits on / Cancelled by owner / False
|
|
173
|
+
// positive) and the index; running the queue stays Main's.
|
|
174
|
+
if (currentPlanner(cfg) === "delegated" && e.path !== cfg.paths.queue_index && plannerBody(e.after) !== plannerBody(e.before)) {
|
|
175
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (planner delegated)`);
|
|
176
|
+
return {
|
|
177
|
+
allow: false,
|
|
178
|
+
reason: `plan-guard: planner is delegated — route the ask to the planner agent. ${e.before ? "The file was restored." : "The new file was removed."}`,
|
|
179
|
+
restore: { path: e.path, text: e.before || null },
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
// P3 (plan 20261027_1300): another Main's live claim on this plan refuses the edit, restored
|
|
183
|
+
// like any refused plan edit. No claim — or this Main's own — edits freely. The chooser gets
|
|
184
|
+
// `e.before` (the file on disk already holds the edit, so a team plan stays guarded while an
|
|
185
|
+
// edit removes its header; the record rule covers it too).
|
|
186
|
+
const holder = claimStoreFor(e.path, cfg, e.before).holder(e.path);
|
|
187
|
+
if (holder !== null && "unreachable" in holder) {
|
|
188
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (team server ${holder.unreachable} unreachable)`);
|
|
189
|
+
return {
|
|
190
|
+
allow: false,
|
|
191
|
+
reason: `plan-guard: ${heldProblem(e.path, holder)}; the file was restored.`,
|
|
192
|
+
restore: { path: e.path, text: e.before || null },
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
if (holder && heldByOther(holder, mainId())) {
|
|
196
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (held by Main ${holder.main})`);
|
|
197
|
+
return {
|
|
198
|
+
allow: false,
|
|
199
|
+
reason: `plan-guard: ${heldProblem(e.path, holder)}; the file was restored.`,
|
|
200
|
+
restore: { path: e.path, text: e.before || null },
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
// A PLAN REVIEW or CODE REVIEW line (P6) is a review spawn's verdict (P4k): Main writes none, so its edit is restored.
|
|
204
|
+
if (newProgressEntries(e.before, e.after).some(b => REVIEW_LINE.test(b))) {
|
|
205
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (Main wrote a review line)`);
|
|
206
|
+
return {
|
|
207
|
+
allow: false,
|
|
208
|
+
reason: `plan-guard: a PLAN REVIEW or CODE REVIEW line is written only by the named review spawn of ${e.path}, never by Main. The file was restored.`,
|
|
209
|
+
restore: { path: e.path, text: e.before || null },
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
// P6 (plan 20261028_1600): with review.code on, ticking a phase needs a named review spawn's CODE REVIEW: PASS behind it — the recorded verdict counts, never Main's own line.
|
|
213
|
+
if (cfg.review.code) {
|
|
214
|
+
const ticks = newlyTicked(e.before, e.after);
|
|
215
|
+
const problem = ticks.length ? codeReviewProblem(e.path, e.after, ticks, cfg.paths) : null;
|
|
216
|
+
if (problem) {
|
|
217
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (code review: ${ticks.join(", ")})`);
|
|
218
|
+
return { allow: false, reason: problem, restore: { path: e.path, text: e.before || null } };
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (q.outsideProgress(e.after) === q.outsideProgress(e.before)) {
|
|
222
|
+
return ALLOW;
|
|
223
|
+
}
|
|
224
|
+
// A cited file Main did read lands in the list instead of the refusal (I-20261025-208): fix the
|
|
225
|
+
// text first, then judge the fixed text like any other edit.
|
|
226
|
+
const fix = autoListReads(e.after, e.path, cfg);
|
|
227
|
+
const fixed = fix ? fix.text : e.after;
|
|
228
|
+
// Refuse only what this edit adds: a problem the plan already had is not Main's doing now.
|
|
229
|
+
// The missing-section problem is always refused: a plan with no read list has no evidence behind its text.
|
|
230
|
+
// Reuse map (plan 20261122_1828_reuse_map P2b): its problems join the same refusal, likewise
|
|
231
|
+
// only the ones this edit adds. Both passes share one query budget (and memoised search results), so a slow
|
|
232
|
+
// check is never filtered as known: SLOW_RETRY always refuses.
|
|
233
|
+
const budget = queryBudget();
|
|
234
|
+
const known = new Set(e.before ? planReadProblems(e.before, e.path, cfg) : []);
|
|
235
|
+
const knownReuse = new Set(e.before ? reuseMapWriteProblems(e.before, e.path, cfg, graph, budget) : []);
|
|
236
|
+
const problems = [
|
|
237
|
+
...planReadProblems(fixed, e.path, cfg).filter((p) => p === NO_SECTION || !known.has(p)),
|
|
238
|
+
...reuseMapWriteProblems(fixed, e.path, cfg, graph, budget).filter((p) => p === SLOW_RETRY || !knownReuse.has(p)),
|
|
239
|
+
];
|
|
240
|
+
if (!problems.length) {
|
|
241
|
+
if (!fix) return ALLOW;
|
|
242
|
+
const list = fix.paths.join(", ");
|
|
243
|
+
log(cfg.paths.plan_guard_log, `AUTO-LIST ${e.path}: ${list}`);
|
|
244
|
+
return {
|
|
245
|
+
allow: true,
|
|
246
|
+
reason: "",
|
|
247
|
+
rewrite: { path: e.path, text: fix.text },
|
|
248
|
+
note: `plan-guard: added ${fix.paths.length} file(s) to "## Code read by Main": ${list}`,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
log(cfg.paths.plan_guard_log, `REFUSE-READ ${e.path}: ${problems.join("; ")}`);
|
|
252
|
+
return {
|
|
253
|
+
allow: false,
|
|
254
|
+
reason: `plan-guard: ${e.path} cites code Main has not read for this plan:\n- ${problems.join("\n- ")}\nRead each file (read or grep it), list it under "## Code read by Main", then edit again. ${e.before ? "The file was restored." : "The new file was removed."}`,
|
|
255
|
+
restore: { path: e.path, text: e.before || null },
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* The write-time half of the reuse map gate (plan 20261122_1828_reuse_map P2b): the plan's
|
|
261
|
+
* map/none line is compared against a fresh `graphify` run, and every quoted citation against
|
|
262
|
+
* the cited file's actual line. Runs only at plan write (checkMainPlan and the delegated planner
|
|
263
|
+
* branch) — never on the parsePlan/queue-check/stop paths, and never inside planReadProblems
|
|
264
|
+
* (planReadNote runs that on every plan read, so a graphify call there would sit on the hot path).
|
|
265
|
+
* Every failure refuses with a one-line fix; a disabled feature never reaches the seam.
|
|
266
|
+
*/
|
|
267
|
+
export function reuseMapWriteProblems(
|
|
268
|
+
text: string,
|
|
269
|
+
planPath: string,
|
|
270
|
+
cfg: Config,
|
|
271
|
+
graph: ReuseGraph = realGraphify,
|
|
272
|
+
budget: QueryBudget = queryBudget(),
|
|
273
|
+
): string[] {
|
|
274
|
+
if (!cfg.reuse_map.enabled) return [];
|
|
275
|
+
const lines = (q.sections(text).get("reuse") ?? "").split("\n").map((l) => l.trim()).filter(Boolean);
|
|
276
|
+
const base = reuseMapBase(text, planPath, cfg, graph, lines, true);
|
|
277
|
+
if (base.problems.length) return base.problems;
|
|
278
|
+
return reuseMapLineProblems(lines, base.repo, cfg, graph, budget, "under ## Reuse", { planPath, text });
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The spawn-time half (plan 20261122_1828_reuse_map P4b): a delegated planner's brief must carry
|
|
283
|
+
* its plan's map or none line, judged by the same re-run/compare as the write gate. It builds
|
|
284
|
+
* nothing (`build: false`): a stale or busy map is refused with the command that fixes it, so a
|
|
285
|
+
* spawn never waits on a 600 s graphify run. The brief carries no `new:` citations yet, so only
|
|
286
|
+
* the line itself is judged. Off: [].
|
|
287
|
+
*/
|
|
288
|
+
export function reuseMapBriefProblems(
|
|
289
|
+
brief: string,
|
|
290
|
+
planPath: string,
|
|
291
|
+
cfg: Config,
|
|
292
|
+
graph: ReuseGraph = realGraphify,
|
|
293
|
+
budget: QueryBudget = queryBudget(),
|
|
294
|
+
): string[] {
|
|
295
|
+
if (!cfg.reuse_map.enabled) return [];
|
|
296
|
+
const lines = brief.split("\n").map((l) => l.trim()).filter(Boolean);
|
|
297
|
+
const base = reuseMapBase(brief, planPath, cfg, graph, lines, false);
|
|
298
|
+
if (base.problems.length) return base.problems;
|
|
299
|
+
return reuseMapLineProblems(lines, base.repo, cfg, graph, budget, "in the brief");
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** The prelude both halves share: the plan's repo must be mapped, its terms well formed, graphify at the pin, and the graph fresh (`build` false: stale is refused, never rebuilt). */
|
|
303
|
+
function reuseMapBase(
|
|
304
|
+
text: string,
|
|
305
|
+
planPath: string,
|
|
306
|
+
cfg: Config,
|
|
307
|
+
graph: ReuseGraph,
|
|
308
|
+
lines: string[],
|
|
309
|
+
build: boolean,
|
|
310
|
+
): { repo: string; problems: string[] } {
|
|
311
|
+
const roots = planRoots(planPath, text);
|
|
312
|
+
const repo = roots[roots.length - 1]!;
|
|
313
|
+
if (!cfg.reuse_map.repos.map((r) => path.resolve(q.expandHome(r))).includes(path.resolve(repo)))
|
|
314
|
+
return { repo, problems: [`reuse map: ${repo} is not mapped — run \`neurastream reuse on ${repo}\` first`] };
|
|
315
|
+
// A dashed term is refused before the seam runs: graphify would read it as an option.
|
|
316
|
+
const options = optionTermProblems(planTerms(lines));
|
|
317
|
+
if (options.length) return { repo, problems: options };
|
|
318
|
+
const vp = versionProblem(graph);
|
|
319
|
+
if (vp) return { repo, problems: [`reuse map: ${vp}`] };
|
|
320
|
+
return { repo, problems: freshGraph(repo, cfg.reuse_map.exclude, graph, { build }).map((p) => `reuse map: ${p}`) };
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** The map/none line half both checks share: the line is re-run against the fresh graph (a none line must not hit; a map line's files and cite: N must match). With `body`, the plan's own `new:` citations are judged too (each quoted, N matched files cited); the brief check passes none and stops at the line. */
|
|
324
|
+
function reuseMapLineProblems(
|
|
325
|
+
lines: string[],
|
|
326
|
+
repo: string,
|
|
327
|
+
cfg: Config,
|
|
328
|
+
graph: ReuseGraph,
|
|
329
|
+
budget: QueryBudget,
|
|
330
|
+
where: string,
|
|
331
|
+
body?: { planPath: string; text: string },
|
|
332
|
+
): string[] {
|
|
333
|
+
const map = lines.map(parseMapLine).find((m) => m !== null) ?? null;
|
|
334
|
+
const none = lines.map(parseNoneLine).find((n) => n !== null) ?? null;
|
|
335
|
+
if (!map && !none) return [`reuse map: no map line ${where} — run \`neurastream reuse map <plan> "<term>"...\` and paste its - map: line`];
|
|
336
|
+
if (map && none) return [`reuse map: either a - map: line or a none-to-reuse line ${where}, not both`];
|
|
337
|
+
if (none) {
|
|
338
|
+
if (none.terms.length < NONE_TERMS_MIN)
|
|
339
|
+
return [`reuse map: the none line searches ${none.terms.length} terms (search at least ${NONE_TERMS_MIN} distinct terms)`];
|
|
340
|
+
if (none.terms.length > MAX_TERMS) return [`reuse map: ${none.terms.length} terms on the none line (at most ${MAX_TERMS})`];
|
|
341
|
+
const res = searchTerms(graph, repo, none.terms, undefined, budget);
|
|
342
|
+
if (res.some((r) => r.status === "slow")) return [SLOW_RETRY];
|
|
343
|
+
const bad = res.filter((r) => r.problem);
|
|
344
|
+
if (bad.length) return bad.map((r) => `reuse map: ${r.problem}`);
|
|
345
|
+
const hits = none.terms.map((t, i) => ({ term: t, r: res[i]! })).filter((x) => x.r.status === "hit");
|
|
346
|
+
const quote = body ? quoteProblems(lines, body.planPath, body.text, cfg) : null;
|
|
347
|
+
if (hits.length) {
|
|
348
|
+
const found = hits.map((x) => `"${x.term}" finds ${x.r.files.join(", ")}`).join("; ");
|
|
349
|
+
return [...(quote ? [quote] : []), `reuse map: not none to reuse — ${found}; cite it under ## Reuse instead`];
|
|
350
|
+
}
|
|
351
|
+
return quote ? [quote] : [];
|
|
352
|
+
}
|
|
353
|
+
// A map line: re-run its terms; the fresh files and cite: N must match what the line wrote.
|
|
354
|
+
// (Unreachable when map is null — both-null and none-only already returned — but it narrows.)
|
|
355
|
+
if (!map) return [];
|
|
356
|
+
if (map.terms.length > MAX_TERMS) return [`reuse map: ${map.terms.length} terms on the map line (at most ${MAX_TERMS})`];
|
|
357
|
+
const res = searchTerms(graph, repo, map.terms, undefined, budget);
|
|
358
|
+
if (res.some((r) => r.status === "slow")) return [SLOW_RETRY];
|
|
359
|
+
const bad = res.filter((r) => r.problem);
|
|
360
|
+
if (bad.length) return bad.map((r) => `reuse map: ${r.problem}`);
|
|
361
|
+
const files = [...new Set(res.flatMap((r) => r.files))];
|
|
362
|
+
const wantCite = Math.min(cfg.reuse_map.min_cited, files.length);
|
|
363
|
+
if (map.cite !== wantCite || [...map.files].map(normRel).toSorted().join("\n") !== [...files].map(normRel).toSorted().join("\n"))
|
|
364
|
+
return [`reuse map: the map moved — replace the map line ${where} with: ${mapLine(map.terms, files, cfg.reuse_map.min_cited)}`];
|
|
365
|
+
if (!body) return [];
|
|
366
|
+
const quote = quoteProblems(lines, body.planPath, body.text, cfg);
|
|
367
|
+
if (quote) return [quote];
|
|
368
|
+
const cited = new Set<string>();
|
|
369
|
+
const matched = new Set(files.map(normRel));
|
|
370
|
+
for (const line of lines) {
|
|
371
|
+
if (isMapOrNoneLine(line) || !line.includes("new:")) continue;
|
|
372
|
+
for (const c of citedQuotes(line)) if (matched.has(normRel(c.file))) cited.add(normRel(c.file));
|
|
373
|
+
}
|
|
374
|
+
if (cited.size < wantCite)
|
|
375
|
+
return [`reuse map: ${cited.size} of ${wantCite} matched files cited — cite ${wantCite} of ${files.join(", ")} with quoted lines`];
|
|
376
|
+
return [];
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** Every `new:` citation carries a quote equal to the cited file's line; the file is read only through readConfined. */
|
|
380
|
+
function quoteProblems(lines: string[], planPath: string, text: string, cfg: Config): string | null {
|
|
381
|
+
const scope = {
|
|
382
|
+
roots: citeRoots(path.dirname(planPath), cfg.reuse_map.repos.map((r) => q.expandHome(r))),
|
|
383
|
+
repo: planRoots(planPath, text).at(-1) ?? "",
|
|
384
|
+
};
|
|
385
|
+
for (const line of lines) {
|
|
386
|
+
if (isMapOrNoneLine(line) || !line.includes("new:")) continue;
|
|
387
|
+
if (hasBareChecked(line))
|
|
388
|
+
return `reuse map: citation without a quoted line ("checked: <path>:<n> \\"the file's text at that line\\""): ${line.slice(0, 80)}`;
|
|
389
|
+
for (const c of citedQuotes(line)) {
|
|
390
|
+
const problem = quoteMismatch(c, planPath, text, scope);
|
|
391
|
+
if (problem) return problem;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function quoteMismatch(c: CitedQuote, planPath: string, text: string, scope: { roots: string[]; repo: string }): string | null {
|
|
398
|
+
const abs = planFile(c.file, planPath, text);
|
|
399
|
+
if (!existsSync(abs)) return `reuse map: cited file ${c.file} does not exist — rerun \`neurastream reuse map\` and re-cite`;
|
|
400
|
+
const body = readConfined(abs, scope.roots);
|
|
401
|
+
if (body === null) return `reuse map: cited file ${c.file} is outside ${scope.repo} or not committed — cite a committed path inside the plan's repo`;
|
|
402
|
+
const actual = (body.split("\n")[c.n - 1] ?? "").trim();
|
|
403
|
+
if (!actual) return `reuse map: ${c.file} has no line ${c.n} — rerun \`neurastream reuse map\` and re-cite`;
|
|
404
|
+
if (actual !== c.quote) return `reuse map: ${c.file}:${c.n} is not "${c.quote.slice(0, 60)}" — quote that line exactly as the file has it`;
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const READ_SECTION = /^## Code read by Main[^\n]*\n([\s\S]*?)(?=^## |(?![\s\S]))/m;
|
|
409
|
+
const LISTED_PATH = /`([^`\s]+)`/g;
|
|
410
|
+
const CITED_PATH = /((?:~\/|\/|\.{1,2}\/)?[\w@.-]+(?:\/[\w@.-]+)*\.[A-Za-z0-9]+):\d+/g;
|
|
411
|
+
|
|
412
|
+
const NO_SECTION = `no "## Code read by Main" section lists a file (list each file you read for this plan as a backticked path)`;
|
|
413
|
+
|
|
414
|
+
/** Read evidence behind a plan edit: section present, each listed path read within the window, no unlisted cited path. */
|
|
415
|
+
function planReadProblems(text: string, planPath: string, cfg: Config): string[] {
|
|
416
|
+
const listed = [...(READ_SECTION.exec(text)?.[1] ?? "").matchAll(LISTED_PATH)].map(m => (m[1] ?? "").replace(/:[\d,+\- ]+$/, ""));
|
|
417
|
+
if (!listed.length) return [NO_SECTION];
|
|
418
|
+
const reads = readMainReads(cfg.paths);
|
|
419
|
+
const maxMs = cfg.guards.plan_read_max_hours * 3_600_000;
|
|
420
|
+
const problems: string[] = [];
|
|
421
|
+
const named = new Set<string>();
|
|
422
|
+
for (const p of listed) {
|
|
423
|
+
const abs = planFile(p, planPath, text);
|
|
424
|
+
named.add(abs);
|
|
425
|
+
if (!isFile(abs)) problems.push(`${p} does not exist`);
|
|
426
|
+
else if (!readWithin(abs, planPath, text, reads, maxMs)) problems.push(`${p} was not read by Main within ${cfg.guards.plan_read_max_hours} h`);
|
|
427
|
+
}
|
|
428
|
+
for (const [, cited = ""] of q.outsideProgress(text).matchAll(CITED_PATH)) {
|
|
429
|
+
const abs = planFile(cited, planPath, text);
|
|
430
|
+
if (abs.includes("/docs/plans/") || namedOrTwin(named, abs, planPath, text) || !isFile(abs)) continue;
|
|
431
|
+
problems.push(`${cited} is cited but not listed under "## Code read by Main"`);
|
|
432
|
+
}
|
|
433
|
+
return problems;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** A plan-cited path: absolute, else under the plan's Worktree (when set) or its repo root. */
|
|
437
|
+
export function planFile(p: string, planPath: string, text: string): string {
|
|
438
|
+
const home = q.expandHome(p);
|
|
439
|
+
if (path.isAbsolute(home)) return home;
|
|
440
|
+
const roots = planRoots(planPath, text);
|
|
441
|
+
const root = roots.find(r => isFile(path.join(r, home))) ?? roots[roots.length - 1]!;
|
|
442
|
+
return path.join(root, home);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** The roots a plan-relative path resolves against: the plan's Worktree (when set), then its repo (the text before /docs/plans/, so a docs/plans/<subdir>/ plan still resolves repo-relative paths against <repo>). Exported for `neurastream reuse map`, which resolves the plan's repo exactly like the write gate. */
|
|
446
|
+
export function planRoots(planPath: string, text: string): string[] {
|
|
447
|
+
const wt = /^Worktree:\s*(\S+)/m.exec(text)?.[1];
|
|
448
|
+
const cut = planPath.indexOf("/docs/plans/");
|
|
449
|
+
const repo = cut >= 0 ? planPath.slice(0, cut) : path.dirname(path.dirname(path.dirname(planPath)));
|
|
450
|
+
return wt && wt !== "-" ? [wt, repo] : [repo];
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** The same repo-relative file under the plan's other root (Worktree ↔ main checkout), or null when `abs` sits under neither. */
|
|
454
|
+
function twinFile(abs: string, planPath: string, text: string): string | null {
|
|
455
|
+
const roots = planRoots(planPath, text);
|
|
456
|
+
for (const root of roots) {
|
|
457
|
+
const prefixed = root.endsWith("/") ? root : `${root}/`;
|
|
458
|
+
if (!abs.startsWith(prefixed)) continue;
|
|
459
|
+
for (const other of roots) {
|
|
460
|
+
if (other === root) continue;
|
|
461
|
+
return `${other.endsWith("/") ? other : `${other}/`}${abs.slice(prefixed.length)}`;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
return null;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/** Main read this file within the window — at the resolved path or at its twin under the plan's other root (I-20261025-208 evidence: the same file read in the main checkout must cover its Worktree copy). */
|
|
468
|
+
function readWithin(abs: string, planPath: string, text: string, reads: Record<string, string>, maxMs: number): boolean {
|
|
469
|
+
const cutoff = Date.now() - maxMs;
|
|
470
|
+
const fresh = (f: string) => Date.parse(reads[f] ?? "") >= cutoff;
|
|
471
|
+
if (fresh(abs)) return true;
|
|
472
|
+
const twin = twinFile(abs, planPath, text);
|
|
473
|
+
return twin !== null && fresh(twin);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** The resolved path, or its twin under the plan's other root, is already in the set (cited-vs-listed matches files, not spellings). */
|
|
477
|
+
function namedOrTwin(named: Set<string>, abs: string, planPath: string, text: string): boolean {
|
|
478
|
+
if (named.has(abs)) return true;
|
|
479
|
+
const twin = twinFile(abs, planPath, text);
|
|
480
|
+
return twin !== null && named.has(twin);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Cited paths (outside Progress) Main read within the window but the list omits: returns the plan
|
|
485
|
+
* text with one `- \`<path as cited>\`` line appended to "## Code read by Main" (the section is
|
|
486
|
+
* created before "## Progress" when missing), or null when nothing qualifies. Never-read citations
|
|
487
|
+
* stay out: they are refused like today.
|
|
488
|
+
*/
|
|
489
|
+
function autoListReads(text: string, planPath: string, cfg: Config): { text: string; paths: string[] } | null {
|
|
490
|
+
const reads = readMainReads(cfg.paths);
|
|
491
|
+
const maxMs = cfg.guards.plan_read_max_hours * 3_600_000;
|
|
492
|
+
const covered = new Set<string>();
|
|
493
|
+
for (const p of [...(READ_SECTION.exec(text)?.[1] ?? "").matchAll(LISTED_PATH)].map(m => (m[1] ?? "").replace(/:[\d,+\- ]+$/, "")))
|
|
494
|
+
covered.add(planFile(p, planPath, text));
|
|
495
|
+
const added: string[] = [];
|
|
496
|
+
const seen = new Set<string>();
|
|
497
|
+
for (const [, cited = ""] of q.outsideProgress(text).matchAll(CITED_PATH)) {
|
|
498
|
+
const abs = planFile(cited, planPath, text);
|
|
499
|
+
if (abs.includes("/docs/plans/") || seen.has(cited) || !isFile(abs)) continue;
|
|
500
|
+
if (namedOrTwin(covered, abs, planPath, text)) continue;
|
|
501
|
+
if (!readWithin(abs, planPath, text, reads, maxMs)) continue;
|
|
502
|
+
seen.add(cited);
|
|
503
|
+
covered.add(abs);
|
|
504
|
+
added.push(cited);
|
|
505
|
+
}
|
|
506
|
+
if (!added.length) return null;
|
|
507
|
+
const lines = added.map(p => `- \`${p}\``);
|
|
508
|
+
const m = READ_SECTION.exec(text);
|
|
509
|
+
let out: string;
|
|
510
|
+
if (m && m[1] !== undefined) {
|
|
511
|
+
// Append after the section's last entry, ahead of the blank lines before the next heading.
|
|
512
|
+
const bodyStart = m.index + m[0].length - m[1].length;
|
|
513
|
+
const at = bodyStart + m[1].replace(/\s+$/, "").length;
|
|
514
|
+
out = `${text.slice(0, at)}\n${lines.join("\n")}${text.slice(at)}`;
|
|
515
|
+
} else {
|
|
516
|
+
// No section yet: create it right before "## Progress" (or at the end when that is missing too).
|
|
517
|
+
const at = text.search(/^## Progress\b/m);
|
|
518
|
+
const block = `## Code read by Main\n${lines.join("\n")}\n\n`;
|
|
519
|
+
out = at === -1 ? `${text}${block}` : `${text.slice(0, at)}${block}${text.slice(at)}`;
|
|
520
|
+
}
|
|
521
|
+
return { text: out, paths: added };
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function isFile(file: string): boolean {
|
|
525
|
+
try {
|
|
526
|
+
return statSync(file).isFile();
|
|
527
|
+
} catch {
|
|
528
|
+
return false;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Planner mode (plan 20261102_1515 P2): named planner spawn tracking and delegated plan-body checks.
|
|
2
|
+
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { withFileLock, writeAtomicSync } from "../file-lock.ts";
|
|
6
|
+
import { MAIN_ONLY_LINE } from "../plan-approval.ts";
|
|
7
|
+
import * as q from "../queue/lib.ts";
|
|
8
|
+
|
|
9
|
+
/** The plan path a brief's `Planner role for <plan path>` line names (its first word, trailing `.,;:` dropped, so text after the path never joins it), or null. */
|
|
10
|
+
export function plannerRolePlan(text: string): string | null {
|
|
11
|
+
const p = (/^[ \t]*Planner role for[ \t]+(\S+)/im.exec(text)?.[1] ?? "").replace(/[.,;:]+$/, "");
|
|
12
|
+
return p || null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Spawn-time check of a bound planner plan path: absolute, `.md`, under a `docs/plans` dir that exists (the planner may create a subfolder). Returns the refusal text, or null when fine. */
|
|
16
|
+
export function plannerPlanProblem(plan: string): string | null {
|
|
17
|
+
const cut = plan.indexOf("/docs/plans/");
|
|
18
|
+
const ok = path.isAbsolute(plan) && plan.endsWith(".md") && cut >= 0 && existsSync(plan.slice(0, cut + "/docs/plans".length));
|
|
19
|
+
return ok
|
|
20
|
+
? null
|
|
21
|
+
: `the \`Planner role for\` path "${plan}" must be an absolute .md path in an existing docs/plans dir, alone as the first word after "for" (e.g. \`Planner role for /path/to/repo/docs/plans/20261103_1015_x.md\`).`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** P2(b): the named planner spawns, spawn name → the one plan path its brief may create and edit (planner-spawns.json in the state dir). Recorded only on an approved spawn: in main mode the spawn gate refused the brief, so a binding exists only for a delegated planner. */
|
|
25
|
+
function plannerSpawnsFile(paths: Pick<q.QueuePaths, "queue_state">): string {
|
|
26
|
+
return path.join(paths.queue_state, "planner-spawns.json");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function readPlannerSpawns(paths: Pick<q.QueuePaths, "queue_state">): Record<string, string> {
|
|
30
|
+
try {
|
|
31
|
+
const parsed: unknown = JSON.parse(readFileSync(plannerSpawnsFile(paths), "utf8"));
|
|
32
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? (parsed as Record<string, string>) : {};
|
|
33
|
+
} catch {
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function recordPlannerSpawn(name: string, plan: string, paths: Pick<q.QueuePaths, "queue_state">): void {
|
|
39
|
+
const file = plannerSpawnsFile(paths);
|
|
40
|
+
withFileLock(`${file}.lock`, () => {
|
|
41
|
+
const spawns = readPlannerSpawns(paths);
|
|
42
|
+
spawns[name] = plan;
|
|
43
|
+
writeAtomicSync(file, `${JSON.stringify(spawns, null, 2)}\n`);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The plan path the agent's brief named in its `Planner role for` line: its spawn name, or the name without a -<digits> suffix (the P4f id rule); null when no planner role is bound. */
|
|
48
|
+
export function plannerSpawnPlan(agentId: string, paths: Pick<q.QueuePaths, "queue_state">): string | null {
|
|
49
|
+
const spawns = readPlannerSpawns(paths);
|
|
50
|
+
for (const name of [agentId, agentId.replace(/-\d+$/, "")]) {
|
|
51
|
+
const plan = Object.hasOwn(spawns, name) ? spawns[name] : undefined;
|
|
52
|
+
if (typeof plan === "string" && plan) return plan;
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The plan text a planner agent owns: everything outside "## Progress" and the Main-only lines (planner mode P2a, plan 20261102_1515) — minus the two zones Main may still correct (punch list, plan 20261102_2315): a "## Done check" section's body, and the backticked `verify:` span on a "## Phases" checkbox line. */
|
|
58
|
+
export function plannerBody(text: string): string {
|
|
59
|
+
const body = q.outsideProgress(text).split("\n");
|
|
60
|
+
let section = "";
|
|
61
|
+
// Dropped lines are removed, not blanked: adding or deleting a Main-only line must not change the body.
|
|
62
|
+
return body
|
|
63
|
+
.flatMap((l) => {
|
|
64
|
+
if (l.startsWith("## ")) section = l;
|
|
65
|
+
if (/^## +Done check\b/i.test(section) || MAIN_ONLY_LINE.test(l)) return [];
|
|
66
|
+
if (/^## +Phases\b/i.test(section) && /^- \[[ x]\]/.test(l)) return [l.replace(/`verify:[^`]*`/g, "`verify:`")];
|
|
67
|
+
return [l];
|
|
68
|
+
})
|
|
69
|
+
.join("\n");
|
|
70
|
+
}
|