neurastream 0.3.3 → 0.3.5
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 +11 -0
- package/package.json +1 -1
- package/src/adapters/index.ts +4 -1
- package/src/adapters/omp/adapter.ts +2 -1
- package/src/adapters/omp/index.ts +43 -1
- package/src/adapters/omp/launch.ts +111 -1
- package/src/adapters/omp/sessions.ts +70 -3
- package/src/cli/dispatch.ts +6 -0
- package/src/cli/recall.ts +187 -0
- package/src/core/adapter.ts +7 -0
- package/src/core/config.ts +4 -0
- package/src/core/guards.ts +11 -2
- package/src/core/owner-log.ts +218 -0
- package/src/core/owner-topics.ts +525 -0
- package/templates/AGENTS.md +1 -0
- package/templates/config.yml +4 -0
- package/templates/help/recall.txt +18 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// The recall log (plan 20261109_2335, P2): one JSON line per owner message and per Main answer,
|
|
2
|
+
// kept in <state>/recall/owner-log.jsonl. The inject path and the turn end write it; `neurastream
|
|
3
|
+
// recall` reads it through readLines, which walks the file backwards in 64 KiB blocks so a search
|
|
4
|
+
// over a long log never loads it whole. The backfill file (P3) is read after the live log.
|
|
5
|
+
|
|
6
|
+
import { appendFileSync, chmodSync, closeSync, fstatSync, mkdirSync, openSync, readSync } from "node:fs";
|
|
7
|
+
import { randomBytes } from "node:crypto";
|
|
8
|
+
import { dirname, join } from "node:path";
|
|
9
|
+
import { nowSec } from "./clock.ts";
|
|
10
|
+
import type { Config } from "./config.ts";
|
|
11
|
+
import { ownerMessagesFile } from "./incidents.ts";
|
|
12
|
+
import { loadKnownSecrets, redactSecrets } from "./secret-redact.ts";
|
|
13
|
+
import type { OwnerTurn } from "./adapter.ts";
|
|
14
|
+
import { isOwnerProse } from "./guards.ts";
|
|
15
|
+
import { ownerText } from "./rollover.ts";
|
|
16
|
+
|
|
17
|
+
export type LogRole = "owner" | "main";
|
|
18
|
+
|
|
19
|
+
export interface LogEntry {
|
|
20
|
+
id: string;
|
|
21
|
+
at: number;
|
|
22
|
+
role: LogRole;
|
|
23
|
+
text: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function ownerLogDir(): string {
|
|
27
|
+
return join(dirname(ownerMessagesFile()), "recall");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function ownerLogFile(): string {
|
|
31
|
+
return join(ownerLogDir(), "owner-log.jsonl");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function ownerBackfillFile(): string {
|
|
35
|
+
return join(ownerLogDir(), "owner-log.backfill.jsonl");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Ids sort by time: nine base-36 digits of the millisecond clock, then eight hex digits of
|
|
39
|
+
// randomness, so two entries written in the same millisecond collide with probability 2^-32.
|
|
40
|
+
export function newEntryId(atMs: number, rand: (n: number) => Uint8Array = randomBytes): string {
|
|
41
|
+
return atMs.toString(36).padStart(9, "0") + Buffer.from(rand(4)).toString("hex");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// The one writer: the recall dir is 0700 and the file 0600, also when they pre-exist looser.
|
|
45
|
+
export function appendJsonLines(file: string, rows: readonly object[]): void {
|
|
46
|
+
const dir = dirname(file);
|
|
47
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
48
|
+
chmodSync(dir, 0o700);
|
|
49
|
+
appendFileSync(file, rows.map((row) => `${JSON.stringify(row)}\n`).join(""), { mode: 0o600 });
|
|
50
|
+
chmodSync(file, 0o600);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Entries appended in this process since the last topics spawn. The turn-end trigger reads it.
|
|
54
|
+
let appendedCount = 0;
|
|
55
|
+
|
|
56
|
+
export function appendedSinceSpawn(): number {
|
|
57
|
+
return appendedCount;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function markTopicsSpawned(): void {
|
|
61
|
+
appendedCount = 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function appendOwnerLog(
|
|
65
|
+
role: LogRole,
|
|
66
|
+
text: string,
|
|
67
|
+
opts: { atMs?: number; rand?: (n: number) => Uint8Array } = {},
|
|
68
|
+
): string {
|
|
69
|
+
const atMs = opts.atMs ?? nowSec() * 1000;
|
|
70
|
+
const entry: LogEntry = { id: newEntryId(atMs, opts.rand), at: Math.floor(atMs / 1000), role, text };
|
|
71
|
+
appendJsonLines(ownerLogFile(), [entry]);
|
|
72
|
+
appendedCount++;
|
|
73
|
+
return entry.id;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Redacts the text (the configured known secrets and extra patterns) before it is written. Throws
|
|
77
|
+
// on a failed write; the callers decide where that is logged.
|
|
78
|
+
export function recordOwnerLog(cfg: Config, role: LogRole, text: string): void {
|
|
79
|
+
const clean = redactSecrets(text, loadKnownSecrets(cfg), cfg.secrets.extra_patterns).text;
|
|
80
|
+
appendOwnerLog(role, clean);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The first (newest) or last (oldest) parsable entry of one log file; undefined when it has none.
|
|
84
|
+
function logEdge(file: string, newest: boolean): LogEntry | undefined {
|
|
85
|
+
let edge: LogEntry | undefined;
|
|
86
|
+
for (const line of readLines(file)) {
|
|
87
|
+
const entry = parseEntry(line);
|
|
88
|
+
if (entry === undefined) continue;
|
|
89
|
+
edge = entry;
|
|
90
|
+
if (newest) break;
|
|
91
|
+
}
|
|
92
|
+
return edge;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Writes the session files' owner prompts and Main replies to the backfill file, once. A turn is kept
|
|
96
|
+
// only strictly between the newest backfill entry and the oldest live entry, so a rerun adds nothing and
|
|
97
|
+
// the live log is never touched. Owner prompts pass the live path's gates (owner prose, then the owner's
|
|
98
|
+
// part of a handoff or unanswered prompt). Returns the number of entries written.
|
|
99
|
+
export function backfillOwnerLog(cfg: Config, turns: Iterable<OwnerTurn>): number {
|
|
100
|
+
const kept: OwnerTurn[] = [];
|
|
101
|
+
for (const turn of turns) {
|
|
102
|
+
if (turn.role === "main") {
|
|
103
|
+
kept.push(turn);
|
|
104
|
+
} else if (isOwnerProse(turn.text)) {
|
|
105
|
+
const owner = ownerText(turn.text);
|
|
106
|
+
if (owner !== undefined) kept.push({ at: turn.at, role: "owner", text: owner });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
kept.sort((a, b) => a.at - b.at);
|
|
110
|
+
const newest = logEdge(ownerBackfillFile(), true);
|
|
111
|
+
const oldest = logEdge(ownerLogFile(), false);
|
|
112
|
+
const known = loadKnownSecrets(cfg);
|
|
113
|
+
const rows: LogEntry[] = [];
|
|
114
|
+
for (const turn of kept) {
|
|
115
|
+
if (newest !== undefined && turn.at <= newest.at) continue;
|
|
116
|
+
if (oldest !== undefined && turn.at >= oldest.at) continue;
|
|
117
|
+
const clean = redactSecrets(turn.text, known, cfg.secrets.extra_patterns).text;
|
|
118
|
+
rows.push({ id: newEntryId(turn.at * 1000), at: turn.at, role: turn.role, text: clean });
|
|
119
|
+
}
|
|
120
|
+
if (rows.length > 0) appendJsonLines(ownerBackfillFile(), rows);
|
|
121
|
+
return rows.length;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const BLOCK = 64 * 1024;
|
|
125
|
+
const LF = 0x0a;
|
|
126
|
+
|
|
127
|
+
// The file's lines, newest first. A missing file yields nothing; empty lines are skipped.
|
|
128
|
+
export function* readLines(file: string): Generator<string> {
|
|
129
|
+
let fd: number;
|
|
130
|
+
try {
|
|
131
|
+
fd = openSync(file, "r");
|
|
132
|
+
} catch {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
let pos = fstatSync(fd).size;
|
|
137
|
+
let pending = Buffer.alloc(0);
|
|
138
|
+
while (pos > 0) {
|
|
139
|
+
const n = Math.min(BLOCK, pos);
|
|
140
|
+
pos -= n;
|
|
141
|
+
const block = Buffer.alloc(n);
|
|
142
|
+
readSync(fd, block, 0, n, pos);
|
|
143
|
+
const data = pending.length > 0 ? Buffer.concat([block, pending]) : block;
|
|
144
|
+
let end = data.length;
|
|
145
|
+
let nl = data.lastIndexOf(LF);
|
|
146
|
+
while (nl >= 0) {
|
|
147
|
+
if (end > nl + 1) yield data.toString("utf8", nl + 1, end);
|
|
148
|
+
end = nl;
|
|
149
|
+
nl = end > 0 ? data.lastIndexOf(LF, end - 1) : -1;
|
|
150
|
+
}
|
|
151
|
+
// What is left precedes the block's first newline: the head of a line that starts earlier.
|
|
152
|
+
pending = data.subarray(0, end);
|
|
153
|
+
}
|
|
154
|
+
if (pending.length > 0) yield pending.toString("utf8");
|
|
155
|
+
} finally {
|
|
156
|
+
closeSync(fd);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function parseEntry(line: string): LogEntry | undefined {
|
|
161
|
+
try {
|
|
162
|
+
return JSON.parse(line) as LogEntry;
|
|
163
|
+
} catch {
|
|
164
|
+
// A torn last line (the writer was killed mid-append) is skipped, not fatal to a search.
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Every entry, newest first: the live log, then the backfill (older by construction).
|
|
170
|
+
export function* readEntries(): Generator<LogEntry> {
|
|
171
|
+
for (const file of [ownerLogFile(), ownerBackfillFile()]) {
|
|
172
|
+
for (const line of readLines(file)) {
|
|
173
|
+
const entry = parseEntry(line);
|
|
174
|
+
if (entry !== undefined) yield entry;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export interface SearchOpts {
|
|
180
|
+
words: string[];
|
|
181
|
+
sinceSec?: number;
|
|
182
|
+
limit: number;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Entries whose text contains every word (case-insensitive, any order), newest first.
|
|
186
|
+
export function searchEntries(opts: SearchOpts): LogEntry[] {
|
|
187
|
+
const words = opts.words.map((w) => w.toLowerCase());
|
|
188
|
+
const out: LogEntry[] = [];
|
|
189
|
+
for (const e of readEntries()) {
|
|
190
|
+
if (opts.sinceSec !== undefined && e.at < opts.sinceSec) continue;
|
|
191
|
+
const text = e.text.toLowerCase();
|
|
192
|
+
if (!words.every((w) => text.includes(w))) continue;
|
|
193
|
+
out.push(e);
|
|
194
|
+
if (out.length >= opts.limit) break;
|
|
195
|
+
}
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// The entry with this id plus the entry it pairs with, in chronological order: an owner message
|
|
200
|
+
// with the nearest newer Main answer, a Main answer with the nearest older owner message. Undefined
|
|
201
|
+
// when no entry has the id. One streaming pass, newest first.
|
|
202
|
+
export function showEntries(id: string): LogEntry[] | undefined {
|
|
203
|
+
let target: LogEntry | undefined;
|
|
204
|
+
let newerMain: LogEntry | undefined;
|
|
205
|
+
for (const e of readEntries()) {
|
|
206
|
+
if (target === undefined) {
|
|
207
|
+
if (e.id === id) {
|
|
208
|
+
target = e;
|
|
209
|
+
if (e.role === "owner") return newerMain ? [e, newerMain] : [e];
|
|
210
|
+
} else if (e.role === "main") {
|
|
211
|
+
newerMain = e;
|
|
212
|
+
}
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (e.role === "owner") return [e, target];
|
|
216
|
+
}
|
|
217
|
+
return target ? [target] : undefined;
|
|
218
|
+
}
|