neurastream 0.3.17 → 0.3.20
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/omp/index.ts +66 -54
- package/src/adapters/omp/launch.ts +4 -6
- package/src/cli/claim.ts +1 -3
- package/src/cli/dispatch.ts +25 -21
- 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 +3 -6
- package/src/cli/results-build.ts +1 -3
- package/src/cli/workspace.ts +0 -3
- package/src/core/autonomy.ts +24 -21
- package/src/core/guards.ts +192 -195
- package/src/core/incidents.ts +13 -12
- package/src/core/main-start.ts +1 -1
- 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 +36 -35
- package/src/core/owner-ui.ts +54 -51
- package/src/core/pages.ts +3 -4
- package/src/core/plan-approval.ts +18 -18
- package/src/core/queue/lib.ts +89 -105
- package/src/core/queue-cancel.ts +20 -10
- package/src/core/queue-check.ts +5 -7
- package/src/core/queue-close.ts +8 -8
- 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/team-client.ts +5 -4
- package/src/core/watch.ts +3 -2
- package/src/core/queue/env.ts +0 -14
package/src/core/incidents.ts
CHANGED
|
@@ -245,22 +245,22 @@ export interface OwnerAnswer {
|
|
|
245
245
|
answer: string;
|
|
246
246
|
}
|
|
247
247
|
|
|
248
|
-
export function ownerMessagesFile(): string {
|
|
249
|
-
return join(
|
|
248
|
+
export function ownerMessagesFile(paths: Pick<q.QueuePaths, "queue_state">): string {
|
|
249
|
+
return join(paths.queue_state, "owner-messages.jsonl");
|
|
250
250
|
}
|
|
251
251
|
|
|
252
|
-
export function readOwnerMessages(): OwnerMessage[] {
|
|
252
|
+
export function readOwnerMessages(paths: Pick<q.QueuePaths, "queue_state">): OwnerMessage[] {
|
|
253
253
|
try {
|
|
254
|
-
return readFileSync(ownerMessagesFile(), "utf8").split("\n").filter(Boolean)
|
|
254
|
+
return readFileSync(ownerMessagesFile(paths), "utf8").split("\n").filter(Boolean)
|
|
255
255
|
.map(line => JSON.parse(line) as OwnerMessage).filter(m => typeof m.text === "string");
|
|
256
256
|
} catch {
|
|
257
257
|
return [];
|
|
258
258
|
}
|
|
259
259
|
}
|
|
260
260
|
|
|
261
|
-
export function readOwnerAnswers(): OwnerAnswer[] {
|
|
261
|
+
export function readOwnerAnswers(paths: Pick<q.QueuePaths, "queue_state">): OwnerAnswer[] {
|
|
262
262
|
try {
|
|
263
|
-
return readFileSync(ownerMessagesFile(), "utf8").split("\n").filter(Boolean)
|
|
263
|
+
return readFileSync(ownerMessagesFile(paths), "utf8").split("\n").filter(Boolean)
|
|
264
264
|
.map(line => JSON.parse(line) as OwnerAnswer).filter(a => typeof a.answer === "string");
|
|
265
265
|
} catch {
|
|
266
266
|
return [];
|
|
@@ -268,14 +268,15 @@ export function readOwnerAnswers(): OwnerAnswer[] {
|
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
/** Rewrites the store: the newest 50 messages, then the newest 20 answers. */
|
|
271
|
-
export function writeOwnerStore(messages: OwnerMessage[], answers: OwnerAnswer[]): void {
|
|
272
|
-
|
|
273
|
-
|
|
271
|
+
export function writeOwnerStore(messages: OwnerMessage[], answers: OwnerAnswer[], paths: Pick<q.QueuePaths, "queue_state">): void {
|
|
272
|
+
const file = ownerMessagesFile(paths);
|
|
273
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
274
|
+
writeFileSync(file, [...messages.slice(-50).map(m => JSON.stringify(m)), ...answers.slice(-20).map(a => JSON.stringify(a))].join("\n") + "\n");
|
|
274
275
|
}
|
|
275
276
|
|
|
276
277
|
/** 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
|
-
writeOwnerStore(readOwnerMessages(), [...readOwnerAnswers(), { at, answer }]);
|
|
278
|
+
export function appendOwnerAnswer(answer: string, paths: Pick<q.QueuePaths, "queue_state">, at = nowSec()): void {
|
|
279
|
+
writeOwnerStore(readOwnerMessages(paths), [...readOwnerAnswers(paths), { at, answer }], paths);
|
|
279
280
|
}
|
|
280
281
|
|
|
281
282
|
export function ownerSaid(words: string, messages: OwnerMessage[], now: number): boolean {
|
|
@@ -318,7 +319,7 @@ export function dismissIncident(cfg: Config, id: string, why: string, owner?: st
|
|
|
318
319
|
if (isFindingKind(rec.kind) && tapLogin === undefined) {
|
|
319
320
|
if (owner === undefined)
|
|
320
321
|
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)`;
|
|
321
|
-
if (!ownerSaid(owner, readOwnerMessages(), nowSec()))
|
|
322
|
+
if (!ownerSaid(owner, readOwnerMessages(cfg.paths), nowSec()))
|
|
322
323
|
return "refused: the --owner quote is not part of an owner message of the last 24 h";
|
|
323
324
|
}
|
|
324
325
|
mkdirSync(dirname(file), { recursive: true });
|
package/src/core/main-start.ts
CHANGED
|
@@ -211,7 +211,7 @@ export async function mainStartText(args: string[], cfg: Config, opts: { inherit
|
|
|
211
211
|
out.push(text.endsWith("\n") || text === "" ? text : `${text}\n`);
|
|
212
212
|
if (doctor.error) say(doctor.error.message);
|
|
213
213
|
try {
|
|
214
|
-
const sweep = mrSweep({
|
|
214
|
+
const sweep = mrSweep({ paths: cfg.paths });
|
|
215
215
|
if (sweep.lines.length) {
|
|
216
216
|
say("=== mr-sweep");
|
|
217
217
|
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;
|
package/src/core/owner-topics.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { nowSec } from "./clock.ts";
|
|
|
8
8
|
import type { Config } from "./config.ts";
|
|
9
9
|
import { tryLock } from "./file-lock.ts";
|
|
10
10
|
import { appendJsonLines, entryIdSec, type LogEntry, ownerLogDir, ownerLogFile, readEntries, readLines } from "./owner-log.ts";
|
|
11
|
+
import type { QueuePaths } from "./queue/lib.ts";
|
|
11
12
|
import { loadKnownSecrets, redactSecrets } from "./secret-redact.ts";
|
|
12
13
|
|
|
13
14
|
export interface CompleteRequest {
|
|
@@ -102,13 +103,13 @@ const SYSTEM = [
|
|
|
102
103
|
"- continues: always null.",
|
|
103
104
|
].join("\n");
|
|
104
105
|
|
|
105
|
-
export function topicsFile(): string {
|
|
106
|
-
return join(ownerLogDir(), TOPICS_FILE);
|
|
106
|
+
export function topicsFile(paths: Pick<QueuePaths, "queue_state">): string {
|
|
107
|
+
return join(ownerLogDir(paths), TOPICS_FILE);
|
|
107
108
|
}
|
|
108
109
|
|
|
109
110
|
// stdout and stderr of the background `recall topics` run: stats and `topics: fail` lines only.
|
|
110
|
-
export function topicsLogFile(): string {
|
|
111
|
-
return join(ownerLogDir(), TOPICS_LOG_FILE);
|
|
111
|
+
export function topicsLogFile(paths: Pick<QueuePaths, "queue_state">): string {
|
|
112
|
+
return join(ownerLogDir(paths), TOPICS_LOG_FILE);
|
|
112
113
|
}
|
|
113
114
|
|
|
114
115
|
export function topicsDue(count: number, every: number): boolean {
|
|
@@ -172,9 +173,9 @@ function parseRow(line: string): TopicRow | undefined {
|
|
|
172
173
|
|
|
173
174
|
// The index as a reader sees it: file order, where a line replaces every earlier line that reaches into its
|
|
174
175
|
// ids (a re-cut of the open segment, or the closed copy that --close writes). Unparsable lines are skipped.
|
|
175
|
-
function effectiveRows(): TopicRow[] {
|
|
176
|
+
function effectiveRows(paths: Pick<QueuePaths, "queue_state">): TopicRow[] {
|
|
176
177
|
const out: TopicRow[] = [];
|
|
177
|
-
for (const line of [...readLines(topicsFile())].toReversed()) {
|
|
178
|
+
for (const line of [...readLines(topicsFile(paths))].toReversed()) {
|
|
178
179
|
const row = parseRow(line);
|
|
179
180
|
if (row === undefined) continue;
|
|
180
181
|
while (out.length > 0 && out.at(-1)!.to >= row.from) out.pop();
|
|
@@ -183,8 +184,8 @@ function effectiveRows(): TopicRow[] {
|
|
|
183
184
|
return out;
|
|
184
185
|
}
|
|
185
186
|
|
|
186
|
-
export function readTopics(): Segment[] {
|
|
187
|
-
return effectiveRows().filter(isSegment);
|
|
187
|
+
export function readTopics(paths: Pick<QueuePaths, "queue_state">): Segment[] {
|
|
188
|
+
return effectiveRows(paths).filter(isSegment);
|
|
188
189
|
}
|
|
189
190
|
|
|
190
191
|
// The id the next window starts after: the last segment that is closed, or a skipped window.
|
|
@@ -389,7 +390,7 @@ function appendSegments(cfg: Config, segments: readonly AnswerSegment[]): void {
|
|
|
389
390
|
names: s.names.map(clean),
|
|
390
391
|
continues: s.continues,
|
|
391
392
|
}));
|
|
392
|
-
appendJsonLines(topicsFile(), rows);
|
|
393
|
+
appendJsonLines(topicsFile(cfg.paths), rows);
|
|
393
394
|
}
|
|
394
395
|
|
|
395
396
|
// One model call on the window. On a good answer the segments are appended; otherwise nothing is written.
|
|
@@ -442,42 +443,42 @@ async function segmentWindow(
|
|
|
442
443
|
const second = await attemptOnce(cfg, complete, window, retry, earlier, tally);
|
|
443
444
|
if (second.kind === "ok") return undefined;
|
|
444
445
|
if (second.kind === "error") return { reason: second.reason, in: second.in, out: 0 };
|
|
445
|
-
appendJsonLines(topicsFile(), [
|
|
446
|
+
appendJsonLines(topicsFile(cfg.paths), [
|
|
446
447
|
{ from: window[0]!.id, to: window.at(-1)!.id, at: nowSec(), skipped: true, why: second.reason },
|
|
447
448
|
]);
|
|
448
449
|
return { reason: second.reason, in: second.in, out: second.out };
|
|
449
450
|
}
|
|
450
451
|
|
|
451
452
|
// `--close`: the open segment becomes closed (a copy with open:false), so the next window starts after it.
|
|
452
|
-
function closeOpen(): void {
|
|
453
|
-
const last = effectiveRows().at(-1);
|
|
454
|
-
if (last !== undefined && isSegment(last) && last.open) appendJsonLines(topicsFile(), [{ ...last, open: false }]);
|
|
453
|
+
function closeOpen(paths: Pick<QueuePaths, "queue_state">): void {
|
|
454
|
+
const last = effectiveRows(paths).at(-1);
|
|
455
|
+
if (last !== undefined && isSegment(last) && last.open) appendJsonLines(topicsFile(paths), [{ ...last, open: false }]);
|
|
455
456
|
}
|
|
456
457
|
|
|
457
|
-
function oldestFirst(): LogEntry[] {
|
|
458
|
-
return [...readEntries()].toReversed();
|
|
458
|
+
function oldestFirst(paths: Pick<QueuePaths, "queue_state">): LogEntry[] {
|
|
459
|
+
return [...readEntries(paths)].toReversed();
|
|
459
460
|
}
|
|
460
461
|
|
|
461
462
|
// The entries after `afterId`, oldest first. No id means every entry.
|
|
462
|
-
export function newEntries(afterId?: string): LogEntry[] {
|
|
463
|
-
const all = oldestFirst();
|
|
463
|
+
export function newEntries(paths: Pick<QueuePaths, "queue_state">, afterId?: string): LogEntry[] {
|
|
464
|
+
const all = oldestFirst(paths);
|
|
464
465
|
if (afterId === undefined) return all;
|
|
465
466
|
const i = all.findIndex(e => e.id === afterId);
|
|
466
467
|
return i < 0 ? all : all.slice(i + 1);
|
|
467
468
|
}
|
|
468
469
|
|
|
469
470
|
// The entries from `from` to `to`, both inclusive, oldest first. Empty when either id is missing.
|
|
470
|
-
export function rangeEntries(from: string, to: string): LogEntry[] {
|
|
471
|
-
const all = oldestFirst();
|
|
471
|
+
export function rangeEntries(from: string, to: string, paths: Pick<QueuePaths, "queue_state">): LogEntry[] {
|
|
472
|
+
const all = oldestFirst(paths);
|
|
472
473
|
const a = all.findIndex(e => e.id === from);
|
|
473
474
|
const b = all.findIndex(e => e.id === to);
|
|
474
475
|
return a < 0 || b < 0 || b < a ? [] : all.slice(a, b + 1);
|
|
475
476
|
}
|
|
476
477
|
|
|
477
478
|
// Topics whose text holds every word (case-insensitive, any order), in file order.
|
|
478
|
-
export function searchTopics(query: readonly string[]): Segment[] {
|
|
479
|
+
export function searchTopics(query: readonly string[], paths: Pick<QueuePaths, "queue_state">): Segment[] {
|
|
479
480
|
const want = query.map(w => w.toLowerCase());
|
|
480
|
-
return readTopics().filter(s => {
|
|
481
|
+
return readTopics(paths).filter(s => {
|
|
481
482
|
const hay = [s.title, ...s.keywords, ...s.decisions.map(d => d.text), ...s.questions.map(q => q.text), ...s.names]
|
|
482
483
|
.join(" ")
|
|
483
484
|
.toLowerCase();
|
|
@@ -502,9 +503,9 @@ function spread(values: number[]): { mean: number; median: number; p90: number;
|
|
|
502
503
|
}
|
|
503
504
|
|
|
504
505
|
// The ids of the live log file (not the backfill): a segment's lag is measured only where its `to` is live.
|
|
505
|
-
function liveIds(): Set<string> {
|
|
506
|
+
function liveIds(paths: Pick<QueuePaths, "queue_state">): Set<string> {
|
|
506
507
|
const ids = new Set<string>();
|
|
507
|
-
for (const line of readLines(ownerLogFile())) {
|
|
508
|
+
for (const line of readLines(ownerLogFile(paths))) {
|
|
508
509
|
try {
|
|
509
510
|
const e = JSON.parse(line) as Partial<LogEntry>;
|
|
510
511
|
if (typeof e.id === "string") ids.add(e.id);
|
|
@@ -517,18 +518,18 @@ function liveIds(): Set<string> {
|
|
|
517
518
|
|
|
518
519
|
// The one stats line every run ends with. Counts come from the index as it stands; tokens and calls are
|
|
519
520
|
// this run's. Lag is the age, in hours, of each segment's `to` entry, for the segments whose `to` is live.
|
|
520
|
-
function statsLine(tally: Tally): string {
|
|
521
|
-
const rows = effectiveRows();
|
|
521
|
+
function statsLine(tally: Tally, paths: Pick<QueuePaths, "queue_state">): string {
|
|
522
|
+
const rows = effectiveRows(paths);
|
|
522
523
|
const segments = rows.filter(isSegment);
|
|
523
524
|
const skipped = rows.length - segments.length;
|
|
524
|
-
const all = oldestFirst();
|
|
525
|
+
const all = oldestFirst(paths);
|
|
525
526
|
const byId = new Map(all.map((e, index) => [e.id, { index, at: e.at }]));
|
|
526
527
|
const counts = segments.flatMap(s => {
|
|
527
528
|
const a = byId.get(s.from);
|
|
528
529
|
const b = byId.get(s.to);
|
|
529
530
|
return a !== undefined && b !== undefined && b.index >= a.index ? [b.index - a.index + 1] : [];
|
|
530
531
|
});
|
|
531
|
-
const live = liveIds();
|
|
532
|
+
const live = liveIds(paths);
|
|
532
533
|
const lags = segments.flatMap(s => {
|
|
533
534
|
const b = byId.get(s.to);
|
|
534
535
|
return b !== undefined && live.has(s.to) ? [(nowSec() - b.at) / 3600] : [];
|
|
@@ -547,7 +548,7 @@ function statsLine(tally: Tally): string {
|
|
|
547
548
|
// the lines and exits by `failures`.
|
|
548
549
|
export async function runTopics(cfg: Config, complete: Complete, opts: { close: boolean }): Promise<TopicsRun> {
|
|
549
550
|
if (!opts.close && cfg.recall.topics_model === "") return { lines: ["topics: off"], failures: 0 };
|
|
550
|
-
const dir = ownerLogDir();
|
|
551
|
+
const dir = ownerLogDir(cfg.paths);
|
|
551
552
|
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
552
553
|
chmodSync(dir, 0o700);
|
|
553
554
|
const lock = tryLock(join(dir, LOCK_FILE));
|
|
@@ -556,19 +557,19 @@ export async function runTopics(cfg: Config, complete: Complete, opts: { close:
|
|
|
556
557
|
try {
|
|
557
558
|
const tally: Tally = { calls: 0, tokensIn: 0, tokensOut: 0 };
|
|
558
559
|
if (opts.close) {
|
|
559
|
-
closeOpen();
|
|
560
|
-
return { lines: [statsLine(tally)], failures: 0 };
|
|
560
|
+
closeOpen(cfg.paths);
|
|
561
|
+
return { lines: [statsLine(tally, cfg.paths)], failures: 0 };
|
|
561
562
|
}
|
|
562
|
-
const rows = effectiveRows();
|
|
563
|
-
const window = newEntries(afterClosed(rows)).slice(0, WINDOW);
|
|
564
|
-
if (window.length === 0) return { lines: [statsLine(tally)], failures: 0 };
|
|
563
|
+
const rows = effectiveRows(cfg.paths);
|
|
564
|
+
const window = newEntries(cfg.paths, afterClosed(rows)).slice(0, WINDOW);
|
|
565
|
+
if (window.length === 0) return { lines: [statsLine(tally, cfg.paths)], failures: 0 };
|
|
565
566
|
const last = rows.at(-1);
|
|
566
|
-
if (last !== undefined && isSegment(last) && last.open && window.at(-1)!.id === last.to) return { lines: [statsLine(tally)], failures: 0 };
|
|
567
|
+
if (last !== undefined && isSegment(last) && last.open && window.at(-1)!.id === last.to) return { lines: [statsLine(tally, cfg.paths)], failures: 0 };
|
|
567
568
|
const failed = await segmentWindow(cfg, complete, window, rows, tally);
|
|
568
569
|
const lines = failed
|
|
569
570
|
? [`topics: fail ${window[0]!.id}..${window.at(-1)!.id} ${failed.reason} in=${failed.in} out=${failed.out}`]
|
|
570
571
|
: [];
|
|
571
|
-
lines.push(statsLine(tally));
|
|
572
|
+
lines.push(statsLine(tally, cfg.paths));
|
|
572
573
|
return { lines, failures: failed ? 1 : 0 };
|
|
573
574
|
} finally {
|
|
574
575
|
lock.release();
|