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,338 @@
|
|
|
1
|
+
// Plan edit verification: subagent Progress-only / planner-body / Findings / review rules, and Main read-evidence checks.
|
|
2
|
+
|
|
3
|
+
import { 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 { plannerBody, plannerSpawnPlan } from "./planner.ts";
|
|
13
|
+
import { codeReviewProblem, matchReviewSpawn, readReviewSpawns, recordReviews, REVIEW_LINE, REVIEW_LOOKALIKE } from "./review.ts";
|
|
14
|
+
import {
|
|
15
|
+
ALLOW,
|
|
16
|
+
findingsValue,
|
|
17
|
+
heldProblem,
|
|
18
|
+
log,
|
|
19
|
+
MAIN_PLAN_FILE,
|
|
20
|
+
mainId,
|
|
21
|
+
newlyTicked,
|
|
22
|
+
newProgressEntries,
|
|
23
|
+
readMainReads,
|
|
24
|
+
type PlanEditDecision,
|
|
25
|
+
} from "./state.ts";
|
|
26
|
+
|
|
27
|
+
const GREP_LINE = /^([^\s:]+):\d+:/gm;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The pre-draft warning (I-20261025-208): when Main reads a plan file (the read tool, or a result
|
|
31
|
+
* whose first `path:line:` hit is the plan), the tool result gains one note naming the plan's
|
|
32
|
+
* listed files that would fail the read check right now — before Main drafts an edit the guard
|
|
33
|
+
* would refuse and throw away. Silence when every listed file is fresh in some checkout.
|
|
34
|
+
*/
|
|
35
|
+
export function planReadNote(tool: string, input: Record<string, unknown>, cwd: string, text: string, cfg: Config): string | undefined {
|
|
36
|
+
let planPath = "";
|
|
37
|
+
if (tool === "read" && typeof input.path === "string") planPath = path.resolve(cwd, q.expandHome(input.path));
|
|
38
|
+
else {
|
|
39
|
+
const first = [...text.matchAll(GREP_LINE)][0];
|
|
40
|
+
if (first) {
|
|
41
|
+
const base = typeof input.cwd === "string" ? path.resolve(cwd, q.expandHome(input.cwd)) : cwd;
|
|
42
|
+
planPath = path.resolve(q.shellCwd(String(input.command ?? input.code ?? ""), base), first[1] ?? "");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (!planPath || !MAIN_PLAN_FILE.test(planPath)) return undefined;
|
|
46
|
+
let plan: string;
|
|
47
|
+
try {
|
|
48
|
+
plan = readFileSync(planPath, "utf8");
|
|
49
|
+
} catch {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
const stale = planReadProblems(plan, planPath, cfg)
|
|
53
|
+
.filter(p => p.includes(" was not read by Main within "))
|
|
54
|
+
.map(p => p.slice(0, p.indexOf(" was not read by Main within ")));
|
|
55
|
+
if (!stale.length) return undefined;
|
|
56
|
+
return `plan-guard: before you edit this plan, re-read: ${stale.join(", ")}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function mainOnlyLines(text: string): string {
|
|
60
|
+
return text.split("\n").filter(l => MAIN_ONLY_LINE.test(l)).join("\n");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* plan-edit: after a subagent changed a plan, everything outside "## Progress" and every
|
|
65
|
+
* Main-only line must be unchanged, and every Progress entry the edit adds must carry a
|
|
66
|
+
* "Findings:" line (2026-10-24: a finding buried in a Progress note never became a plan);
|
|
67
|
+
* `restore` hands back the untouched text for the caller to write (the original restored the
|
|
68
|
+
* file itself before reporting).
|
|
69
|
+
*/
|
|
70
|
+
export function checkPlanEdit(e: PlanEditEvent, cfg: Config): PlanEditDecision {
|
|
71
|
+
if (e.agent === "main") return checkMainPlan(e, cfg);
|
|
72
|
+
// planner mode P2 (b): the plan bound to this sub by its approved spawn (the `Planner role for`
|
|
73
|
+
// line) is the planner agent's to write — body freedom, but never the Main-only lines, and the
|
|
74
|
+
// header names its agent. Only while delegated: in main mode the spawn gate refused the
|
|
75
|
+
// Planner-role brief, and the plain Progress-only rule holds.
|
|
76
|
+
if (currentPlanner(cfg) === "delegated" && e.agentId !== undefined && plannerSpawnPlan(e.agentId, cfg.paths) === e.path) {
|
|
77
|
+
if (mainOnlyLines(e.after) !== mainOnlyLines(e.before)) {
|
|
78
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (planner wrote a Main-only line)`);
|
|
79
|
+
return {
|
|
80
|
+
allow: false,
|
|
81
|
+
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.`,
|
|
82
|
+
restore: { path: e.path, text: e.before || null },
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const name = e.agentId ?? "";
|
|
86
|
+
// The header records the agent the owner picked, not the spawn instance: a `glm-3` spawn
|
|
87
|
+
// satisfies `Planner: glm`, and the spawn form still works for plans written before this.
|
|
88
|
+
const names = new Set([name, name.replace(/-\d+$/, "")]);
|
|
89
|
+
const head = e.after.split(/^## /m)[0] ?? "";
|
|
90
|
+
if (!head.split("\n").some((l) => l.trim().startsWith("Planner: ") && names.has(l.trim().slice("Planner: ".length).trim()))) {
|
|
91
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (no Planner header)`);
|
|
92
|
+
return {
|
|
93
|
+
allow: false,
|
|
94
|
+
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.`,
|
|
95
|
+
restore: { path: e.path, text: e.before || null },
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
} else if (q.outsideProgress(e.after) !== q.outsideProgress(e.before) || mainOnlyLines(e.after) !== mainOnlyLines(e.before)) {
|
|
99
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path}`);
|
|
100
|
+
return {
|
|
101
|
+
allow: false,
|
|
102
|
+
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.`,
|
|
103
|
+
restore: { path: e.path, text: e.before },
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const missing = newProgressEntries(e.before, e.after).filter(b => !findingsValue(b));
|
|
107
|
+
if (missing.length) {
|
|
108
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (no Findings line)`);
|
|
109
|
+
return {
|
|
110
|
+
allow: false,
|
|
111
|
+
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.`,
|
|
112
|
+
restore: { path: e.path, text: e.before },
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const loose = newProgressEntries(e.before, e.after).filter(b => REVIEW_LOOKALIKE.test(b) && !REVIEW_LINE.test(b));
|
|
116
|
+
if (loose.length) {
|
|
117
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (review line)`);
|
|
118
|
+
return {
|
|
119
|
+
allow: false,
|
|
120
|
+
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.`,
|
|
121
|
+
restore: { path: e.path, text: e.before },
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
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) {
|
|
125
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} review line from a spawn that is not its named review spawn`);
|
|
126
|
+
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 } };
|
|
127
|
+
}
|
|
128
|
+
recordReviews(e, cfg);
|
|
129
|
+
return ALLOW;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Main's plan text outside "## Progress" must rest on files Main read itself (planReadProblems).
|
|
133
|
+
// An empty `before` means the plan file did not exist yet; a refusal then deletes it.
|
|
134
|
+
function checkMainPlan(e: PlanEditEvent, cfg: Config): PlanEditDecision {
|
|
135
|
+
// planner mode P2 (a): while delegated, Main plans nothing — a new plan file or a rewrite of
|
|
136
|
+
// the text outside "## Progress" is the planner agent's work. Main keeps its Progress entries,
|
|
137
|
+
// the Main-only lines (Status / WAITING on owner / Waits on / Cancelled by owner / False
|
|
138
|
+
// positive) and the index; running the queue stays Main's.
|
|
139
|
+
if (currentPlanner(cfg) === "delegated" && e.path !== cfg.paths.queue_index && plannerBody(e.after) !== plannerBody(e.before)) {
|
|
140
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (planner delegated)`);
|
|
141
|
+
return {
|
|
142
|
+
allow: false,
|
|
143
|
+
reason: `plan-guard: planner is delegated — route the ask to the planner agent. ${e.before ? "The file was restored." : "The new file was removed."}`,
|
|
144
|
+
restore: { path: e.path, text: e.before || null },
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
// P3 (plan 20261027_1300): another Main's live claim on this plan refuses the edit, restored
|
|
148
|
+
// like any refused plan edit. No claim — or this Main's own — edits freely. The chooser gets
|
|
149
|
+
// `e.before` (the file on disk already holds the edit, so a team plan stays guarded while an
|
|
150
|
+
// edit removes its header; the record rule covers it too).
|
|
151
|
+
const holder = claimStoreFor(e.path, cfg, e.before).holder(e.path);
|
|
152
|
+
if (holder !== null && "unreachable" in holder) {
|
|
153
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (team server ${holder.unreachable} unreachable)`);
|
|
154
|
+
return {
|
|
155
|
+
allow: false,
|
|
156
|
+
reason: `plan-guard: ${heldProblem(e.path, holder)}; the file was restored.`,
|
|
157
|
+
restore: { path: e.path, text: e.before || null },
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
if (holder && heldByOther(holder, mainId())) {
|
|
161
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (held by Main ${holder.main})`);
|
|
162
|
+
return {
|
|
163
|
+
allow: false,
|
|
164
|
+
reason: `plan-guard: ${heldProblem(e.path, holder)}; the file was restored.`,
|
|
165
|
+
restore: { path: e.path, text: e.before || null },
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
// A PLAN REVIEW or CODE REVIEW line (P6) is a review spawn's verdict (P4k): Main writes none, so its edit is restored.
|
|
169
|
+
if (newProgressEntries(e.before, e.after).some(b => REVIEW_LINE.test(b))) {
|
|
170
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (Main wrote a review line)`);
|
|
171
|
+
return {
|
|
172
|
+
allow: false,
|
|
173
|
+
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.`,
|
|
174
|
+
restore: { path: e.path, text: e.before || null },
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
// 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.
|
|
178
|
+
if (cfg.review.code) {
|
|
179
|
+
const ticks = newlyTicked(e.before, e.after);
|
|
180
|
+
const problem = ticks.length ? codeReviewProblem(e.path, e.after, ticks, cfg.paths) : null;
|
|
181
|
+
if (problem) {
|
|
182
|
+
log(cfg.paths.plan_guard_log, `RESTORE ${e.path} (code review: ${ticks.join(", ")})`);
|
|
183
|
+
return { allow: false, reason: problem, restore: { path: e.path, text: e.before || null } };
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (q.outsideProgress(e.after) === q.outsideProgress(e.before)) {
|
|
187
|
+
return ALLOW;
|
|
188
|
+
}
|
|
189
|
+
// A cited file Main did read lands in the list instead of the refusal (I-20261025-208): fix the
|
|
190
|
+
// text first, then judge the fixed text like any other edit.
|
|
191
|
+
const fix = autoListReads(e.after, e.path, cfg);
|
|
192
|
+
const fixed = fix ? fix.text : e.after;
|
|
193
|
+
// Refuse only what this edit adds: a problem the plan already had is not Main's doing now.
|
|
194
|
+
// The missing-section problem is always refused: a plan with no read list has no evidence behind its text.
|
|
195
|
+
const known = new Set(e.before ? planReadProblems(e.before, e.path, cfg) : []);
|
|
196
|
+
const problems = planReadProblems(fixed, e.path, cfg).filter(p => p === NO_SECTION || !known.has(p));
|
|
197
|
+
if (!problems.length) {
|
|
198
|
+
if (!fix) return ALLOW;
|
|
199
|
+
const list = fix.paths.join(", ");
|
|
200
|
+
log(cfg.paths.plan_guard_log, `AUTO-LIST ${e.path}: ${list}`);
|
|
201
|
+
return {
|
|
202
|
+
allow: true,
|
|
203
|
+
reason: "",
|
|
204
|
+
rewrite: { path: e.path, text: fix.text },
|
|
205
|
+
note: `plan-guard: added ${fix.paths.length} file(s) to "## Code read by Main": ${list}`,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
log(cfg.paths.plan_guard_log, `REFUSE-READ ${e.path}: ${problems.join("; ")}`);
|
|
209
|
+
return {
|
|
210
|
+
allow: false,
|
|
211
|
+
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."}`,
|
|
212
|
+
restore: { path: e.path, text: e.before || null },
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const READ_SECTION = /^## Code read by Main[^\n]*\n([\s\S]*?)(?=^## |(?![\s\S]))/m;
|
|
217
|
+
const LISTED_PATH = /`([^`\s]+)`/g;
|
|
218
|
+
const CITED_PATH = /((?:~\/|\/|\.{1,2}\/)?[\w@.-]+(?:\/[\w@.-]+)*\.[A-Za-z0-9]+):\d+/g;
|
|
219
|
+
|
|
220
|
+
const NO_SECTION = `no "## Code read by Main" section lists a file (list each file you read for this plan as a backticked path)`;
|
|
221
|
+
|
|
222
|
+
/** Read evidence behind a plan edit: section present, each listed path read within the window, no unlisted cited path. */
|
|
223
|
+
function planReadProblems(text: string, planPath: string, cfg: Config): string[] {
|
|
224
|
+
const listed = [...(READ_SECTION.exec(text)?.[1] ?? "").matchAll(LISTED_PATH)].map(m => (m[1] ?? "").replace(/:[\d,+\- ]+$/, ""));
|
|
225
|
+
if (!listed.length) return [NO_SECTION];
|
|
226
|
+
const reads = readMainReads(cfg.paths);
|
|
227
|
+
const maxMs = cfg.guards.plan_read_max_hours * 3_600_000;
|
|
228
|
+
const problems: string[] = [];
|
|
229
|
+
const named = new Set<string>();
|
|
230
|
+
for (const p of listed) {
|
|
231
|
+
const abs = planFile(p, planPath, text);
|
|
232
|
+
named.add(abs);
|
|
233
|
+
if (!isFile(abs)) problems.push(`${p} does not exist`);
|
|
234
|
+
else if (!readWithin(abs, planPath, text, reads, maxMs)) problems.push(`${p} was not read by Main within ${cfg.guards.plan_read_max_hours} h`);
|
|
235
|
+
}
|
|
236
|
+
for (const [, cited = ""] of q.outsideProgress(text).matchAll(CITED_PATH)) {
|
|
237
|
+
const abs = planFile(cited, planPath, text);
|
|
238
|
+
if (abs.includes("/docs/plans/") || namedOrTwin(named, abs, planPath, text) || !isFile(abs)) continue;
|
|
239
|
+
problems.push(`${cited} is cited but not listed under "## Code read by Main"`);
|
|
240
|
+
}
|
|
241
|
+
return problems;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** A plan-cited path: absolute, else under the plan's Worktree (when set) or its repo root. */
|
|
245
|
+
function planFile(p: string, planPath: string, text: string): string {
|
|
246
|
+
const home = q.expandHome(p);
|
|
247
|
+
if (path.isAbsolute(home)) return home;
|
|
248
|
+
const roots = planRoots(planPath, text);
|
|
249
|
+
const root = roots.find(r => isFile(path.join(r, home))) ?? roots[roots.length - 1]!;
|
|
250
|
+
return path.join(root, home);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** 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>). */
|
|
254
|
+
function planRoots(planPath: string, text: string): string[] {
|
|
255
|
+
const wt = /^Worktree:\s*(\S+)/m.exec(text)?.[1];
|
|
256
|
+
const cut = planPath.indexOf("/docs/plans/");
|
|
257
|
+
const repo = cut >= 0 ? planPath.slice(0, cut) : path.dirname(path.dirname(path.dirname(planPath)));
|
|
258
|
+
return wt && wt !== "-" ? [wt, repo] : [repo];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** The same repo-relative file under the plan's other root (Worktree ↔ main checkout), or null when `abs` sits under neither. */
|
|
262
|
+
function twinFile(abs: string, planPath: string, text: string): string | null {
|
|
263
|
+
const roots = planRoots(planPath, text);
|
|
264
|
+
for (const root of roots) {
|
|
265
|
+
const prefixed = root.endsWith("/") ? root : `${root}/`;
|
|
266
|
+
if (!abs.startsWith(prefixed)) continue;
|
|
267
|
+
for (const other of roots) {
|
|
268
|
+
if (other === root) continue;
|
|
269
|
+
return `${other.endsWith("/") ? other : `${other}/`}${abs.slice(prefixed.length)}`;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** 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). */
|
|
276
|
+
function readWithin(abs: string, planPath: string, text: string, reads: Record<string, string>, maxMs: number): boolean {
|
|
277
|
+
const cutoff = Date.now() - maxMs;
|
|
278
|
+
const fresh = (f: string) => Date.parse(reads[f] ?? "") >= cutoff;
|
|
279
|
+
if (fresh(abs)) return true;
|
|
280
|
+
const twin = twinFile(abs, planPath, text);
|
|
281
|
+
return twin !== null && fresh(twin);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** The resolved path, or its twin under the plan's other root, is already in the set (cited-vs-listed matches files, not spellings). */
|
|
285
|
+
function namedOrTwin(named: Set<string>, abs: string, planPath: string, text: string): boolean {
|
|
286
|
+
if (named.has(abs)) return true;
|
|
287
|
+
const twin = twinFile(abs, planPath, text);
|
|
288
|
+
return twin !== null && named.has(twin);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Cited paths (outside Progress) Main read within the window but the list omits: returns the plan
|
|
293
|
+
* text with one `- \`<path as cited>\`` line appended to "## Code read by Main" (the section is
|
|
294
|
+
* created before "## Progress" when missing), or null when nothing qualifies. Never-read citations
|
|
295
|
+
* stay out: they are refused like today.
|
|
296
|
+
*/
|
|
297
|
+
function autoListReads(text: string, planPath: string, cfg: Config): { text: string; paths: string[] } | null {
|
|
298
|
+
const reads = readMainReads(cfg.paths);
|
|
299
|
+
const maxMs = cfg.guards.plan_read_max_hours * 3_600_000;
|
|
300
|
+
const covered = new Set<string>();
|
|
301
|
+
for (const p of [...(READ_SECTION.exec(text)?.[1] ?? "").matchAll(LISTED_PATH)].map(m => (m[1] ?? "").replace(/:[\d,+\- ]+$/, "")))
|
|
302
|
+
covered.add(planFile(p, planPath, text));
|
|
303
|
+
const added: string[] = [];
|
|
304
|
+
const seen = new Set<string>();
|
|
305
|
+
for (const [, cited = ""] of q.outsideProgress(text).matchAll(CITED_PATH)) {
|
|
306
|
+
const abs = planFile(cited, planPath, text);
|
|
307
|
+
if (abs.includes("/docs/plans/") || seen.has(cited) || !isFile(abs)) continue;
|
|
308
|
+
if (namedOrTwin(covered, abs, planPath, text)) continue;
|
|
309
|
+
if (!readWithin(abs, planPath, text, reads, maxMs)) continue;
|
|
310
|
+
seen.add(cited);
|
|
311
|
+
covered.add(abs);
|
|
312
|
+
added.push(cited);
|
|
313
|
+
}
|
|
314
|
+
if (!added.length) return null;
|
|
315
|
+
const lines = added.map(p => `- \`${p}\``);
|
|
316
|
+
const m = READ_SECTION.exec(text);
|
|
317
|
+
let out: string;
|
|
318
|
+
if (m && m[1] !== undefined) {
|
|
319
|
+
// Append after the section's last entry, ahead of the blank lines before the next heading.
|
|
320
|
+
const bodyStart = m.index + m[0].length - m[1].length;
|
|
321
|
+
const at = bodyStart + m[1].replace(/\s+$/, "").length;
|
|
322
|
+
out = `${text.slice(0, at)}\n${lines.join("\n")}${text.slice(at)}`;
|
|
323
|
+
} else {
|
|
324
|
+
// No section yet: create it right before "## Progress" (or at the end when that is missing too).
|
|
325
|
+
const at = text.search(/^## Progress\b/m);
|
|
326
|
+
const block = `## Code read by Main\n${lines.join("\n")}\n\n`;
|
|
327
|
+
out = at === -1 ? `${text}${block}` : `${text.slice(0, at)}${block}${text.slice(at)}`;
|
|
328
|
+
}
|
|
329
|
+
return { text: out, paths: added };
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function isFile(file: string): boolean {
|
|
333
|
+
try {
|
|
334
|
+
return statSync(file).isFile();
|
|
335
|
+
} catch {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
@@ -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
|
+
}
|