neurastream 0.3.18 → 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/README.md +88 -490
- 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 +138 -1050
- package/src/adapters/omp/launch.ts +35 -9
- 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/claim.ts +1 -3
- package/src/cli/commands.ts +33 -2
- package/src/cli/dispatch.ts +55 -30
- package/src/cli/guard.ts +0 -3
- package/src/cli/incident.ts +2 -2
- package/src/cli/metrics.ts +1 -1
- package/src/cli/recall.ts +6 -8
- package/src/cli/results-build.ts +2 -4
- package/src/cli/workspace.ts +13 -5
- package/src/core/adapter.ts +16 -0
- package/src/core/autonomy.ts +24 -21
- 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 -2661
- package/src/core/incidents.ts +134 -99
- package/src/core/machine-guard.ts +23 -5
- package/src/core/main-start.ts +11 -9
- package/src/core/mr-sweep.ts +4 -2
- package/src/core/notify.ts +15 -15
- package/src/core/owner-log.ts +19 -17
- package/src/core/owner-topics.ts +38 -48
- 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 -2118
- package/src/core/page-notes.ts +25 -0
- package/src/core/pages.ts +452 -78
- package/src/core/plan-approval.ts +24 -26
- package/src/core/queue/lib.ts +170 -137
- package/src/core/queue-cancel.ts +29 -15
- package/src/core/queue-check.ts +5 -7
- package/src/core/queue-close.ts +54 -29
- package/src/core/queue-finished.ts +9 -10
- package/src/core/queue-migrate.ts +1 -3
- package/src/core/queue-trim.ts +2 -4
- package/src/core/results.ts +0 -2
- package/src/core/rollover.ts +2 -3
- package/src/core/secret-redact.ts +65 -14
- package/src/core/tailscale.ts +79 -3
- package/src/core/team-client.ts +18 -6
- package/src/core/uninstall.ts +2 -0
- package/src/core/watch.ts +3 -2
- package/src/core/queue/env.ts +0 -14
package/src/core/incidents.ts
CHANGED
|
@@ -9,10 +9,11 @@
|
|
|
9
9
|
// findings (plan-guard: the Findings: items of a subagent's new Progress entry), watch (the
|
|
10
10
|
// src/core/watch.ts watcher: failed/stale heartbeats, failed units).
|
|
11
11
|
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
12
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
13
|
+
import { join, resolve } from "node:path";
|
|
14
14
|
import { nowSec, strftime } from "./clock.ts";
|
|
15
15
|
import type { Config } from "./config.ts";
|
|
16
|
+
import { withFileLock, writeAtomicSync } from "./file-lock.ts";
|
|
16
17
|
import * as q from "./queue/lib.ts";
|
|
17
18
|
|
|
18
19
|
export type IncidentSource = "realtime" | "daily" | "weekly" | "main" | "findings" | "traces" | "watch";
|
|
@@ -132,9 +133,8 @@ function readStore(file: string): { incidents: IncidentRec[]; resolutions: Resol
|
|
|
132
133
|
return { incidents, resolutions, proposals, lines };
|
|
133
134
|
}
|
|
134
135
|
|
|
135
|
-
function
|
|
136
|
-
|
|
137
|
-
writeFileSync(file, `${lines.join("\n")}\n`);
|
|
136
|
+
function writeIncidentLines(file: string, lines: string[]): void {
|
|
137
|
+
writeAtomicSync(file, `${lines.join("\n")}\n`);
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
/** Every incident in file order, each folded with its last resolution record (if any). */
|
|
@@ -162,39 +162,40 @@ export function openIncidents(cfg: Config): IncidentView[] {
|
|
|
162
162
|
/** Records a sighting. Returns the (open) incident; `deduped` says an open one was updated. */
|
|
163
163
|
export function addIncident(cfg: Config, input: IncidentInput, now = nowSec()): { incident: IncidentView; deduped: boolean } {
|
|
164
164
|
const file = incidentsFile(cfg);
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
open
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
165
|
+
return withFileLock(`${file}.lock`, () => {
|
|
166
|
+
const store = readStore(file);
|
|
167
|
+
const fingerprint = input.fingerprint ?? defaultFingerprint(input);
|
|
168
|
+
const open = store.incidents.find(
|
|
169
|
+
i => i.fingerprint === fingerprint && !store.resolutions.some(r => r.id === i.id),
|
|
170
|
+
);
|
|
171
|
+
if (open) {
|
|
172
|
+
open.count += 1;
|
|
173
|
+
open.lastSeen = now;
|
|
174
|
+
const lines = store.lines.map(line => {
|
|
175
|
+
const rec = parseLine(line);
|
|
176
|
+
return rec && !("type" in rec) && rec.id === open.id ? JSON.stringify(open) : line;
|
|
177
|
+
});
|
|
178
|
+
writeIncidentLines(file, lines);
|
|
179
|
+
return { incident: { ...open, resolution: null, proposal: null }, deduped: true };
|
|
180
|
+
}
|
|
181
|
+
const day = strftime(now, "%Y%m%d");
|
|
182
|
+
const prefix = `I-${day}-`;
|
|
183
|
+
const used = store.incidents.filter(i => i.id.startsWith(prefix)).map(i => Number(i.id.slice(prefix.length)) || 0);
|
|
184
|
+
const rec: IncidentRec = {
|
|
185
|
+
id: `${prefix}${(used.length ? Math.max(...used) : 0) + 1}`,
|
|
186
|
+
fingerprint,
|
|
187
|
+
source: input.source,
|
|
188
|
+
kind: input.kind,
|
|
189
|
+
summary: input.summary,
|
|
190
|
+
evidence: input.evidence ?? "",
|
|
191
|
+
ts: now,
|
|
192
|
+
count: 1,
|
|
193
|
+
lastSeen: now,
|
|
194
|
+
rule: input.rule,
|
|
195
|
+
};
|
|
196
|
+
writeIncidentLines(file, [...store.lines, JSON.stringify(rec)]);
|
|
197
|
+
return { incident: { ...rec, resolution: null, proposal: null }, deduped: false };
|
|
198
|
+
});
|
|
198
199
|
}
|
|
199
200
|
|
|
200
201
|
/**
|
|
@@ -204,23 +205,24 @@ export function addIncident(cfg: Config, input: IncidentInput, now = nowSec()):
|
|
|
204
205
|
*/
|
|
205
206
|
export function planIncident(cfg: Config, id: string, planPath: string): string | null {
|
|
206
207
|
const file = incidentsFile(cfg);
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
208
|
+
return withFileLock(`${file}.lock`, () => {
|
|
209
|
+
const store = readStore(file);
|
|
210
|
+
if (!store.incidents.some(i => i.id === id)) return `unknown incident ${id}`;
|
|
211
|
+
const res = store.resolutions.filter(r => r.id === id).at(-1);
|
|
212
|
+
if (res) return `incident ${id} already has a ${res.type} record`;
|
|
213
|
+
const abs = resolve(q.expandHome(planPath));
|
|
214
|
+
if (!existsSync(abs)) return `plan file not found: ${abs}`;
|
|
215
|
+
if (!q.readIndex(cfg.paths.queue_index).includes(abs)) return `${abs} is not listed in ${cfg.paths.queue_index}`;
|
|
216
|
+
let text: string;
|
|
217
|
+
try {
|
|
218
|
+
text = readFileSync(abs, "utf8");
|
|
219
|
+
} catch {
|
|
220
|
+
return `cannot read ${abs}`;
|
|
221
|
+
}
|
|
222
|
+
if (!text.includes(`INCIDENT ${id}`)) return `${abs} must contain "INCIDENT ${id}" so the guards can see the fix names the incident`;
|
|
223
|
+
writeIncidentLines(file, [...store.lines, JSON.stringify({ type: "plan", id, plan: abs })]);
|
|
224
|
+
return null;
|
|
225
|
+
});
|
|
224
226
|
}
|
|
225
227
|
|
|
226
228
|
// The owner's own words, as the inject hook saw them (<queue_state>/owner-messages.jsonl, last
|
|
@@ -245,37 +247,67 @@ export interface OwnerAnswer {
|
|
|
245
247
|
answer: string;
|
|
246
248
|
}
|
|
247
249
|
|
|
248
|
-
export function ownerMessagesFile(): string {
|
|
249
|
-
return join(
|
|
250
|
+
export function ownerMessagesFile(paths: Pick<q.QueuePaths, "queue_state">): string {
|
|
251
|
+
return join(paths.queue_state, "owner-messages.jsonl");
|
|
250
252
|
}
|
|
251
253
|
|
|
252
|
-
export function readOwnerMessages(): OwnerMessage[] {
|
|
254
|
+
export function readOwnerMessages(paths: Pick<q.QueuePaths, "queue_state">): OwnerMessage[] {
|
|
253
255
|
try {
|
|
254
|
-
return readFileSync(ownerMessagesFile(), "utf8").split("\n").filter(Boolean)
|
|
256
|
+
return readFileSync(ownerMessagesFile(paths), "utf8").split("\n").filter(Boolean)
|
|
255
257
|
.map(line => JSON.parse(line) as OwnerMessage).filter(m => typeof m.text === "string");
|
|
256
258
|
} catch {
|
|
257
259
|
return [];
|
|
258
260
|
}
|
|
259
261
|
}
|
|
260
262
|
|
|
261
|
-
export function readOwnerAnswers(): OwnerAnswer[] {
|
|
263
|
+
export function readOwnerAnswers(paths: Pick<q.QueuePaths, "queue_state">): OwnerAnswer[] {
|
|
262
264
|
try {
|
|
263
|
-
return readFileSync(ownerMessagesFile(), "utf8").split("\n").filter(Boolean)
|
|
265
|
+
return readFileSync(ownerMessagesFile(paths), "utf8").split("\n").filter(Boolean)
|
|
264
266
|
.map(line => JSON.parse(line) as OwnerAnswer).filter(a => typeof a.answer === "string");
|
|
265
267
|
} catch {
|
|
266
268
|
return [];
|
|
267
269
|
}
|
|
268
270
|
}
|
|
269
271
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
272
|
+
function writeOwnerStoreUnlocked(messages: OwnerMessage[], answers: OwnerAnswer[], file: string): void {
|
|
273
|
+
writeAtomicSync(
|
|
274
|
+
file,
|
|
275
|
+
`${[...messages.slice(-50).map(m => JSON.stringify(m)), ...answers.slice(-20).map(a => JSON.stringify(a))].join("\n")}\n`,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Rewrites the store atomically under a lock: the newest 50 messages, then the newest 20 answers. */
|
|
280
|
+
export function writeOwnerStore(messages: OwnerMessage[], answers: OwnerAnswer[], paths: Pick<q.QueuePaths, "queue_state">): void {
|
|
281
|
+
const file = ownerMessagesFile(paths);
|
|
282
|
+
withFileLock(`${file}.lock`, () => {
|
|
283
|
+
writeOwnerStoreUnlocked(messages, answers, file);
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Append one owner message to the owner-messages store under its file lock.
|
|
289
|
+
*
|
|
290
|
+
* Args:
|
|
291
|
+
* text (string): Owner message text
|
|
292
|
+
* paths (Pick<q.QueuePaths, "queue_state">): Queue state directory paths
|
|
293
|
+
* at (number): Unix timestamp in seconds (defaults to `nowSec()`)
|
|
294
|
+
*
|
|
295
|
+
* Returns:
|
|
296
|
+
* void
|
|
297
|
+
*/
|
|
298
|
+
export function appendOwnerMessage(text: string, paths: Pick<q.QueuePaths, "queue_state">, at = nowSec()): void {
|
|
299
|
+
const file = ownerMessagesFile(paths);
|
|
300
|
+
withFileLock(`${file}.lock`, () => {
|
|
301
|
+
writeOwnerStoreUnlocked([...readOwnerMessages(paths), { at, text }], readOwnerAnswers(paths), file);
|
|
302
|
+
});
|
|
274
303
|
}
|
|
275
304
|
|
|
276
305
|
/** Records a real Main turn end as an answer in the same store (P5b); throws so the caller logs the failure. */
|
|
277
|
-
export function appendOwnerAnswer(answer: string, at = nowSec()): void {
|
|
278
|
-
|
|
306
|
+
export function appendOwnerAnswer(answer: string, paths: Pick<q.QueuePaths, "queue_state">, at = nowSec()): void {
|
|
307
|
+
const file = ownerMessagesFile(paths);
|
|
308
|
+
withFileLock(`${file}.lock`, () => {
|
|
309
|
+
writeOwnerStoreUnlocked(readOwnerMessages(paths), [...readOwnerAnswers(paths), { at, answer }], file);
|
|
310
|
+
});
|
|
279
311
|
}
|
|
280
312
|
|
|
281
313
|
export function ownerSaid(words: string, messages: OwnerMessage[], now: number): boolean {
|
|
@@ -291,15 +323,16 @@ const PROOF_REF = /\S+:\d+/;
|
|
|
291
323
|
* owner taps it on the phone page or a plan closes it. A newer proposal replaces the older one. */
|
|
292
324
|
export function proposeIncident(cfg: Config, id: string, why: string, now = nowSec()): string | null {
|
|
293
325
|
const file = incidentsFile(cfg);
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
326
|
+
return withFileLock(`${file}.lock`, () => {
|
|
327
|
+
const store = readStore(file);
|
|
328
|
+
const rec = store.incidents.find(i => i.id === id);
|
|
329
|
+
if (!rec) return `unknown incident ${id}`;
|
|
330
|
+
if (store.resolutions.some(r => r.id === id)) return `incident ${id} already has a resolution record`;
|
|
331
|
+
if (!isFindingKind(rec.kind)) return `refused: propose is for finding incidents (agent-finding, main-finding); ${id} is ${rec.kind}`;
|
|
332
|
+
if (!PROOF_REF.test(why)) return "refused: the why must cite the proof as file:line, e.g. src/core/guards.ts:40";
|
|
333
|
+
writeIncidentLines(file, [...store.lines, JSON.stringify({ type: "propose", id, why: why.trim(), ts: now })]);
|
|
334
|
+
return null;
|
|
335
|
+
});
|
|
303
336
|
}
|
|
304
337
|
|
|
305
338
|
/** Records why an incident is not a bug (or not worth fixing); an empty why is refused. A finding
|
|
@@ -309,21 +342,22 @@ export function proposeIncident(cfg: Config, id: string, why: string, now = nowS
|
|
|
309
342
|
* owner page's authenticated POST /incident ("false alarm" tap); only that handler passes it. */
|
|
310
343
|
export function dismissIncident(cfg: Config, id: string, why: string, owner?: string, tapLogin?: string): string | null {
|
|
311
344
|
const file = incidentsFile(cfg);
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
if (
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
345
|
+
return withFileLock(`${file}.lock`, () => {
|
|
346
|
+
const store = readStore(file);
|
|
347
|
+
const rec = store.incidents.find(i => i.id === id);
|
|
348
|
+
if (!rec) return `unknown incident ${id}`;
|
|
349
|
+
const res = store.resolutions.filter(r => r.id === id).at(-1);
|
|
350
|
+
if (res) return `incident ${id} already has a ${res.type} record`;
|
|
351
|
+
if (!why.trim()) return "refused: an empty --why would hide the incident without a recorded reason";
|
|
352
|
+
if (isFindingKind(rec.kind) && tapLogin === undefined) {
|
|
353
|
+
if (owner === undefined)
|
|
354
|
+
return `refused: a ${rec.kind} is dismissed only with the owner's words: --owner "<quote>" (part of an owner message of the last 24 h)`;
|
|
355
|
+
if (!ownerSaid(owner, readOwnerMessages(cfg.paths), nowSec()))
|
|
356
|
+
return "refused: the --owner quote is not part of an owner message of the last 24 h";
|
|
357
|
+
}
|
|
358
|
+
writeIncidentLines(file, [...store.lines, JSON.stringify({ type: "dismiss", id, why })]);
|
|
359
|
+
return null;
|
|
360
|
+
});
|
|
327
361
|
}
|
|
328
362
|
|
|
329
363
|
/**
|
|
@@ -334,12 +368,13 @@ export function dismissIncident(cfg: Config, id: string, why: string, owner?: st
|
|
|
334
368
|
*/
|
|
335
369
|
export function fixedIncident(cfg: Config, id: string, plan: string, now = nowSec()): string | null {
|
|
336
370
|
const file = incidentsFile(cfg);
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
371
|
+
return withFileLock(`${file}.lock`, () => {
|
|
372
|
+
const store = readStore(file);
|
|
373
|
+
if (!store.incidents.some(i => i.id === id)) return `unknown incident ${id}`;
|
|
374
|
+
const res = store.resolutions.filter(r => r.id === id).at(-1);
|
|
375
|
+
// A plan record is the normal path (planned, then fixed); dismissed or fixed is final.
|
|
376
|
+
if (res && res.type !== "plan") return `incident ${id} already has a ${res.type} record`;
|
|
377
|
+
writeIncidentLines(file, [...store.lines, JSON.stringify({ type: "fixed", id, plan, ts: now })]);
|
|
378
|
+
return null;
|
|
379
|
+
});
|
|
345
380
|
}
|
|
@@ -6,7 +6,14 @@ import { cpuCount } from "./host.ts";
|
|
|
6
6
|
|
|
7
7
|
const SCRIPT = join(dirname(fileURLToPath(import.meta.url)), "../../templates/machine-guard.sh");
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
export interface MachineGuardOptions {
|
|
10
|
+
/** Absolute path to the CLI entrypoint (`src/cli/main.ts`) invoked by `machine-guard.sh` for heavy-run status. */
|
|
11
|
+
heavyMain: string;
|
|
12
|
+
/** Runtime executable that runs `heavyMain` (default: `"bun"`). */
|
|
13
|
+
heavyStatus?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function envFor(cfg: Config, opts: MachineGuardOptions): NodeJS.ProcessEnv {
|
|
10
17
|
const cores = cpuCount();
|
|
11
18
|
const loadMax = cfg.machine.load_max > 0 ? cfg.machine.load_max : cores + cfg.machine.load_over_cores;
|
|
12
19
|
return {
|
|
@@ -21,8 +28,8 @@ function envFor(cfg: Config): NodeJS.ProcessEnv {
|
|
|
21
28
|
HEAVY_LOCK: cfg.paths.heavy_lock,
|
|
22
29
|
HEAVY_OWNER: cfg.paths.heavy_owner,
|
|
23
30
|
HEAVY_IDLE_STATE: cfg.paths.heavy_idle_state,
|
|
24
|
-
HEAVY_STATUS:
|
|
25
|
-
HEAVY_MAIN:
|
|
31
|
+
HEAVY_STATUS: opts.heavyStatus ?? "bun",
|
|
32
|
+
HEAVY_MAIN: opts.heavyMain,
|
|
26
33
|
DISK_PATH: cfg.machine.disk_path,
|
|
27
34
|
WORK_ROOT: cfg.paths.work_roots[0] ?? join(cfg.paths.home, "Work"),
|
|
28
35
|
WATCH_SEC: String(cfg.machine.watch_sec),
|
|
@@ -30,9 +37,20 @@ function envFor(cfg: Config): NodeJS.ProcessEnv {
|
|
|
30
37
|
};
|
|
31
38
|
}
|
|
32
39
|
|
|
33
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Runs `templates/machine-guard.sh` with thresholds derived from config and caller-supplied heavy-run runner paths.
|
|
42
|
+
*
|
|
43
|
+
* Args:
|
|
44
|
+
* args (string[]): CLI arguments (`--watch` optional)
|
|
45
|
+
* cfg (Config): Loaded neurastream configuration
|
|
46
|
+
* opts (MachineGuardOptions): Required `heavyMain` CLI path and optional `heavyStatus` runtime command
|
|
47
|
+
*
|
|
48
|
+
* Returns:
|
|
49
|
+
* number: Exit status from `machine-guard.sh`
|
|
50
|
+
*/
|
|
51
|
+
export function machineGuard(args: string[], cfg: Config, opts: MachineGuardOptions): number {
|
|
34
52
|
const r = spawnSync("bash", [SCRIPT, ...(args[0] === "--watch" ? ["--watch"] : [])], {
|
|
35
|
-
env: envFor(cfg),
|
|
53
|
+
env: envFor(cfg, opts),
|
|
36
54
|
encoding: "utf8",
|
|
37
55
|
});
|
|
38
56
|
if (r.stdout) process.stdout.write(r.stdout);
|
package/src/core/main-start.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
3
|
import { dirname, join } from "node:path";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
4
|
import type { RunSource, TraceEvent } from "./adapter.ts";
|
|
6
5
|
import { readEvents } from "./agent-score.ts";
|
|
7
6
|
import type { Config } from "./config.ts";
|
|
@@ -78,6 +77,8 @@ export interface MainStartDeps {
|
|
|
78
77
|
episodesFor?: (cutoff: number) => Episode[];
|
|
79
78
|
/** Finished task results parsed from Main's session file (the adapter's format knowledge). */
|
|
80
79
|
results?: TaskResult[];
|
|
80
|
+
/** Runs `neurastream doctor` and returns its combined output; absent omits the doctor body. */
|
|
81
|
+
doctor?: () => Promise<string> | string;
|
|
81
82
|
}
|
|
82
83
|
|
|
83
84
|
// Real-time detectors at main-start (plan 20261022_2330 P3): best effort, logged, never thrown.
|
|
@@ -203,15 +204,16 @@ export async function mainStartText(args: string[], cfg: Config, opts: { inherit
|
|
|
203
204
|
if (lines.length) say(lines.join("\n"));
|
|
204
205
|
} catch {}
|
|
205
206
|
say("=== doctor");
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
207
|
+
if (deps.doctor) {
|
|
208
|
+
try {
|
|
209
|
+
const text = await deps.doctor();
|
|
210
|
+
out.push(text.endsWith("\n") || text === "" ? text : `${text}\n`);
|
|
211
|
+
} catch (err) {
|
|
212
|
+
say(err instanceof Error ? err.message : String(err));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
213
215
|
try {
|
|
214
|
-
const sweep = mrSweep({
|
|
216
|
+
const sweep = mrSweep({ paths: cfg.paths });
|
|
215
217
|
if (sweep.lines.length) {
|
|
216
218
|
say("=== mr-sweep");
|
|
217
219
|
for (const line of sweep.lines) say(line);
|
package/src/core/mr-sweep.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { spawnSync } from "node:child_process";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
import { cmd } from "./proc.ts";
|
|
8
|
-
import { activePlans, gitTop } from "./queue/lib.ts";
|
|
8
|
+
import { activePlans, gitTop, type QueuePaths } from "./queue/lib.ts";
|
|
9
9
|
|
|
10
10
|
export const MR_SWEEP_NO_CLI = "mr-sweep: skipped (no forge CLI)";
|
|
11
11
|
const UNTRACKED_TAIL = "— no open plan mentions it";
|
|
@@ -42,6 +42,7 @@ export interface SweepResult {
|
|
|
42
42
|
|
|
43
43
|
export interface SweepOpts {
|
|
44
44
|
index?: string;
|
|
45
|
+
paths?: Pick<QueuePaths, "queue_index" | "blockers" | "agent_dir">;
|
|
45
46
|
env?: NodeJS.ProcessEnv;
|
|
46
47
|
runner?: CommandRunner;
|
|
47
48
|
}
|
|
@@ -270,7 +271,8 @@ export function listOpenMrs(repo: string, run: CommandRunner = defaultRunner, en
|
|
|
270
271
|
*/
|
|
271
272
|
export function mrSweep(opts: SweepOpts = {}): SweepResult {
|
|
272
273
|
const run = opts.runner ?? defaultRunner;
|
|
273
|
-
const
|
|
274
|
+
const paths = opts.paths ?? (opts.index ? { queue_index: opts.index, blockers: "", agent_dir: path.dirname(opts.index) } : null);
|
|
275
|
+
const plans = paths ? activePlans(paths) : [];
|
|
274
276
|
const repos: string[] = [];
|
|
275
277
|
for (const p of plans) if (!repos.includes(p.repo)) repos.push(p.repo);
|
|
276
278
|
const lines: string[] = [];
|
package/src/core/notify.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { configFile, type Config } from "./config.ts";
|
|
|
9
9
|
import { mainId } from "./guards.ts";
|
|
10
10
|
import { strftime } from "./clock.ts";
|
|
11
11
|
import { recentOwnerMessages, type MessageRow } from "./owner-ui.ts";
|
|
12
|
-
import {
|
|
12
|
+
import type { QueuePaths } from "./queue/lib.ts";
|
|
13
13
|
import { loadKnownSecrets, redactSecrets } from "./secret-redact.ts";
|
|
14
14
|
|
|
15
15
|
export type NotifyProviderName = "discord" | "slack";
|
|
@@ -248,8 +248,8 @@ export function postReply(text: string, cfg: Config): Promise<NotifyPostResult[]
|
|
|
248
248
|
// under the queue state, not in session memory: one-shot CLI calls and a reloaded plugin must
|
|
249
249
|
// agree, like the guard window file.
|
|
250
250
|
|
|
251
|
-
function ownerAnswerStatePath(): string {
|
|
252
|
-
return `${
|
|
251
|
+
function ownerAnswerStatePath(paths: Pick<QueuePaths, "queue_state">): string {
|
|
252
|
+
return `${paths.queue_state}/owner-answer.json`;
|
|
253
253
|
}
|
|
254
254
|
|
|
255
255
|
interface OwnerAnswerState {
|
|
@@ -259,9 +259,9 @@ interface OwnerAnswerState {
|
|
|
259
259
|
steered: string[];
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
-
function readOwnerAnswerState(): OwnerAnswerState {
|
|
262
|
+
function readOwnerAnswerState(paths: Pick<QueuePaths, "queue_state">): OwnerAnswerState {
|
|
263
263
|
try {
|
|
264
|
-
const s = JSON.parse(readFileSync(ownerAnswerStatePath(), "utf8")) as Partial<OwnerAnswerState>;
|
|
264
|
+
const s = JSON.parse(readFileSync(ownerAnswerStatePath(paths), "utf8")) as Partial<OwnerAnswerState>;
|
|
265
265
|
return {
|
|
266
266
|
lastHandBack: Number(s.lastHandBack) || 0,
|
|
267
267
|
steered: Array.isArray(s.steered) ? s.steered.filter((x): x is string => typeof x === "string") : [],
|
|
@@ -271,17 +271,17 @@ function readOwnerAnswerState(): OwnerAnswerState {
|
|
|
271
271
|
}
|
|
272
272
|
}
|
|
273
273
|
|
|
274
|
-
function writeOwnerAnswerState(state: OwnerAnswerState): void {
|
|
275
|
-
mkdirSync(
|
|
276
|
-
writeFileSync(ownerAnswerStatePath(), `${JSON.stringify(state)}\n`);
|
|
274
|
+
function writeOwnerAnswerState(state: OwnerAnswerState, paths: Pick<QueuePaths, "queue_state">): void {
|
|
275
|
+
mkdirSync(paths.queue_state, { recursive: true });
|
|
276
|
+
writeFileSync(ownerAnswerStatePath(paths), `${JSON.stringify(state)}\n`);
|
|
277
277
|
}
|
|
278
278
|
|
|
279
279
|
/** Phone-page owner messages waiting past notify.answer_within_min with no hand-back after them; none when the limit is 0. */
|
|
280
280
|
export function waitingOwnerMessages(cfg: Config, now: number): MessageRow[] {
|
|
281
281
|
const limit = cfg.notify.answer_within_min * 60;
|
|
282
282
|
if (!(limit > 0)) return [];
|
|
283
|
-
const { lastHandBack } = readOwnerAnswerState();
|
|
284
|
-
return recentOwnerMessages(now).filter((m) => now - m.ts > limit && m.ts > lastHandBack);
|
|
283
|
+
const { lastHandBack } = readOwnerAnswerState(cfg.paths);
|
|
284
|
+
return recentOwnerMessages(now, cfg.paths).filter((m) => now - m.ts > limit && m.ts > lastHandBack);
|
|
285
285
|
}
|
|
286
286
|
|
|
287
287
|
/** The steer for every waiting message this turn has not steered yet — one line per message, so
|
|
@@ -289,16 +289,16 @@ export function waitingOwnerMessages(cfg: Config, now: number): MessageRow[] {
|
|
|
289
289
|
export function ownerAnswerSteer(cfg: Config, now: number): string | null {
|
|
290
290
|
const limit = cfg.notify.answer_within_min * 60;
|
|
291
291
|
if (!(limit > 0)) return null;
|
|
292
|
-
const state = readOwnerAnswerState();
|
|
293
|
-
const due = recentOwnerMessages(now).filter((m) => now - m.ts > limit && m.ts > state.lastHandBack && !state.steered.includes(m.id));
|
|
292
|
+
const state = readOwnerAnswerState(cfg.paths);
|
|
293
|
+
const due = recentOwnerMessages(now, cfg.paths).filter((m) => now - m.ts > limit && m.ts > state.lastHandBack && !state.steered.includes(m.id));
|
|
294
294
|
if (due.length === 0) return null;
|
|
295
295
|
state.steered.push(...due.map((m) => m.id));
|
|
296
|
-
writeOwnerAnswerState(state);
|
|
296
|
+
writeOwnerAnswerState(state, cfg.paths);
|
|
297
297
|
return due.map((m) => `Owner asked at ${strftime(m.ts, "%H:%M")}; end your turn with the answer now`).join("\n");
|
|
298
298
|
}
|
|
299
299
|
|
|
300
300
|
/** A hand-back ends the wait: the reply just posted answers whatever waited, so the wait clears
|
|
301
301
|
* and the next owner message starts fresh. */
|
|
302
|
-
export function ownerAnswerHandback(now: number): void {
|
|
303
|
-
writeOwnerAnswerState({ lastHandBack: now, steered: [] });
|
|
302
|
+
export function ownerAnswerHandback(now: number, paths: Pick<QueuePaths, "queue_state">): void {
|
|
303
|
+
writeOwnerAnswerState({ lastHandBack: now, steered: [] }, paths);
|
|
304
304
|
}
|
package/src/core/owner-log.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { dirname, join } from "node:path";
|
|
|
9
9
|
import { nowSec } from "./clock.ts";
|
|
10
10
|
import type { Config } from "./config.ts";
|
|
11
11
|
import { ownerMessagesFile } from "./incidents.ts";
|
|
12
|
+
import type { QueuePaths } from "./queue/lib.ts";
|
|
12
13
|
import { loadKnownSecrets, redactSecrets } from "./secret-redact.ts";
|
|
13
14
|
import type { OwnerTurn } from "./adapter.ts";
|
|
14
15
|
import { isOwnerProse } from "./guards.ts";
|
|
@@ -23,16 +24,16 @@ export interface LogEntry {
|
|
|
23
24
|
text: string;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
export function ownerLogDir(): string {
|
|
27
|
-
return join(dirname(ownerMessagesFile()), "recall");
|
|
27
|
+
export function ownerLogDir(paths: Pick<QueuePaths, "queue_state">): string {
|
|
28
|
+
return join(dirname(ownerMessagesFile(paths)), "recall");
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
export function ownerLogFile(): string {
|
|
31
|
-
return join(ownerLogDir(), "owner-log.jsonl");
|
|
31
|
+
export function ownerLogFile(paths: Pick<QueuePaths, "queue_state">): string {
|
|
32
|
+
return join(ownerLogDir(paths), "owner-log.jsonl");
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
export function ownerBackfillFile(): string {
|
|
35
|
-
return join(ownerLogDir(), "owner-log.backfill.jsonl");
|
|
35
|
+
export function ownerBackfillFile(paths: Pick<QueuePaths, "queue_state">): string {
|
|
36
|
+
return join(ownerLogDir(paths), "owner-log.backfill.jsonl");
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
// Ids sort by time: nine base-36 digits of the millisecond clock, then eight hex digits of
|
|
@@ -70,11 +71,12 @@ export function markTopicsSpawned(): void {
|
|
|
70
71
|
export function appendOwnerLog(
|
|
71
72
|
role: LogRole,
|
|
72
73
|
text: string,
|
|
74
|
+
paths: Pick<QueuePaths, "queue_state">,
|
|
73
75
|
opts: { atMs?: number; rand?: (n: number) => Uint8Array } = {},
|
|
74
76
|
): string {
|
|
75
77
|
const atMs = opts.atMs ?? nowSec() * 1000;
|
|
76
78
|
const entry: LogEntry = { id: newEntryId(atMs, opts.rand), at: Math.floor(atMs / 1000), role, text };
|
|
77
|
-
appendJsonLines(ownerLogFile(), [entry]);
|
|
79
|
+
appendJsonLines(ownerLogFile(paths), [entry]);
|
|
78
80
|
appendedCount++;
|
|
79
81
|
return entry.id;
|
|
80
82
|
}
|
|
@@ -83,7 +85,7 @@ export function appendOwnerLog(
|
|
|
83
85
|
// on a failed write; the callers decide where that is logged.
|
|
84
86
|
export function recordOwnerLog(cfg: Config, role: LogRole, text: string): void {
|
|
85
87
|
const clean = redactSecrets(text, loadKnownSecrets(cfg), cfg.secrets.extra_patterns).text;
|
|
86
|
-
appendOwnerLog(role, clean);
|
|
88
|
+
appendOwnerLog(role, clean, cfg.paths);
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
// The first (newest) or last (oldest) parsable entry of one log file; undefined when it has none.
|
|
@@ -113,8 +115,8 @@ export function backfillOwnerLog(cfg: Config, turns: Iterable<OwnerTurn>): numbe
|
|
|
113
115
|
}
|
|
114
116
|
}
|
|
115
117
|
kept.sort((a, b) => a.at - b.at);
|
|
116
|
-
const newest = logEdge(ownerBackfillFile(), true);
|
|
117
|
-
const oldest = logEdge(ownerLogFile(), false);
|
|
118
|
+
const newest = logEdge(ownerBackfillFile(cfg.paths), true);
|
|
119
|
+
const oldest = logEdge(ownerLogFile(cfg.paths), false);
|
|
118
120
|
const known = loadKnownSecrets(cfg);
|
|
119
121
|
const rows: LogEntry[] = [];
|
|
120
122
|
for (const turn of kept) {
|
|
@@ -123,7 +125,7 @@ export function backfillOwnerLog(cfg: Config, turns: Iterable<OwnerTurn>): numbe
|
|
|
123
125
|
const clean = redactSecrets(turn.text, known, cfg.secrets.extra_patterns).text;
|
|
124
126
|
rows.push({ id: newEntryId(turn.at * 1000), at: turn.at, role: turn.role, text: clean });
|
|
125
127
|
}
|
|
126
|
-
if (rows.length > 0) appendJsonLines(ownerBackfillFile(), rows);
|
|
128
|
+
if (rows.length > 0) appendJsonLines(ownerBackfillFile(cfg.paths), rows);
|
|
127
129
|
return rows.length;
|
|
128
130
|
}
|
|
129
131
|
|
|
@@ -173,8 +175,8 @@ function parseEntry(line: string): LogEntry | undefined {
|
|
|
173
175
|
}
|
|
174
176
|
|
|
175
177
|
// Every entry, newest first: the live log, then the backfill (older by construction).
|
|
176
|
-
export function* readEntries(): Generator<LogEntry> {
|
|
177
|
-
for (const file of [ownerLogFile(), ownerBackfillFile()]) {
|
|
178
|
+
export function* readEntries(paths: Pick<QueuePaths, "queue_state">): Generator<LogEntry> {
|
|
179
|
+
for (const file of [ownerLogFile(paths), ownerBackfillFile(paths)]) {
|
|
178
180
|
for (const line of readLines(file)) {
|
|
179
181
|
const entry = parseEntry(line);
|
|
180
182
|
if (entry !== undefined) yield entry;
|
|
@@ -189,10 +191,10 @@ export interface SearchOpts {
|
|
|
189
191
|
}
|
|
190
192
|
|
|
191
193
|
// Entries whose text contains every word (case-insensitive, any order), newest first.
|
|
192
|
-
export function searchEntries(opts: SearchOpts): LogEntry[] {
|
|
194
|
+
export function searchEntries(opts: SearchOpts, paths: Pick<QueuePaths, "queue_state">): LogEntry[] {
|
|
193
195
|
const words = opts.words.map((w) => w.toLowerCase());
|
|
194
196
|
const out: LogEntry[] = [];
|
|
195
|
-
for (const e of readEntries()) {
|
|
197
|
+
for (const e of readEntries(paths)) {
|
|
196
198
|
if (opts.sinceSec !== undefined && e.at < opts.sinceSec) continue;
|
|
197
199
|
const text = e.text.toLowerCase();
|
|
198
200
|
if (!words.every((w) => text.includes(w))) continue;
|
|
@@ -205,10 +207,10 @@ export function searchEntries(opts: SearchOpts): LogEntry[] {
|
|
|
205
207
|
// The entry with this id plus the entry it pairs with, in chronological order: an owner message
|
|
206
208
|
// with the nearest newer Main answer, a Main answer with the nearest older owner message. Undefined
|
|
207
209
|
// when no entry has the id. One streaming pass, newest first.
|
|
208
|
-
export function showEntries(id: string): LogEntry[] | undefined {
|
|
210
|
+
export function showEntries(id: string, paths: Pick<QueuePaths, "queue_state">): LogEntry[] | undefined {
|
|
209
211
|
let target: LogEntry | undefined;
|
|
210
212
|
let newerMain: LogEntry | undefined;
|
|
211
|
-
for (const e of readEntries()) {
|
|
213
|
+
for (const e of readEntries(paths)) {
|
|
212
214
|
if (target === undefined) {
|
|
213
215
|
if (e.id === id) {
|
|
214
216
|
target = e;
|