-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.ts
More file actions
432 lines (400 loc) · 15.1 KB
/
Copy pathstore.ts
File metadata and controls
432 lines (400 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/**
* Knowledge-base storage layer.
*
* Mirrors the soul store's discipline — every mutation runs under a
* cross-process advisory write-lock (withFileLock) and records a best-effort
* git commit — but keeps the KB in its own dir/repo (see paths.ts). Entries are
* markdown files with a small frontmatter block; sources are append-only
* captures, wiki pages are upserted. index.md (the always-on table of contents)
* is regenerated on every mutation.
*/
import fs from "node:fs/promises";
import { ensureDir, atomicWrite, pathExists, readTextOrEmpty } from "../fs-utils.js";
import { withFileLock } from "../soul/lock.js";
import { assertSafeSlug } from "../soul/slug.js";
import { kbSlug } from "./slug.js";
import { commitKb } from "./git.js";
import { ensureSchema } from "./schema.js";
import { buildGraph, graphToJson, sortedTags } from "./links.js";
import {
kbGraphFile,
kbIndexFile,
kbLockPath,
kbSourcesDir,
kbWikiDir,
entryFile,
layerDir,
type KbLayer,
} from "./paths.js";
export interface KbEntry {
layer: KbLayer;
slug: string;
title: string;
tags: string[];
/** Sources: ISO capture time. */
created?: string;
/** Wiki: ISO last-update time. */
updated?: string;
/** Sources: where it came from (session id | "manual" | "chat:<id>"). */
origin?: string;
/** Wiki: source slugs this page draws on. */
sources?: string[];
/**
* Any other frontmatter key, preserved verbatim on read and written back on
* save. This is where ingest provenance lives (url / site / author /
* published / lang / hash / via / supersedes) without the store needing to
* know about any of it — and it means a key a future version adds survives a
* round-trip through an older one rather than being silently dropped.
*/
extra?: Record<string, string>;
/** Markdown body (frontmatter stripped). */
body: string;
}
/** Frontmatter keys the store owns; everything else lands in `extra`. */
const KNOWN_KEYS = new Set(["title", "tags", "created", "updated", "origin", "sources"]);
export interface KbEntryMeta {
layer: KbLayer;
slug: string;
title: string;
tags: string[];
created?: string;
updated?: string;
origin?: string;
sources?: string[];
extra?: Record<string, string>;
/** First ~160 chars of the body, whitespace-collapsed. */
excerpt: string;
}
// ── frontmatter ───────────────────────────────────────────────────────
function parseFrontmatter(raw: string): {
meta: Record<string, string | string[]>;
body: string;
} {
const lines = raw.split("\n");
if (lines[0] !== "---") return { meta: {}, body: raw };
const meta: Record<string, string | string[]> = {};
let i = 1;
for (; i < lines.length; i++) {
if (lines[i] === "---") {
i++;
break;
}
const m = lines[i]!.match(/^([a-zA-Z_][a-zA-Z0-9_]*):\s*(.*)$/);
if (!m) continue;
const key = m[1]!;
const val = m[2]!.trim();
if (key === "tags" || key === "sources") {
meta[key] = val
.replace(/^\[|\]$/g, "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
} else {
meta[key] = val;
}
}
if (lines[i] === "") i++; // drop one blank line after the closing ---
return { meta, body: lines.slice(i).join("\n") };
}
// A newline in any frontmatter value would forge extra frontmatter lines on the
// next read. `title` and `extra` are the paths that can carry untrusted, remote-
// authored text (e.g. an ingested page <title> / og:title), so collapse whitespace
// on everything written into the block.
const flattenFm = (v: unknown): string => String(v).replace(/\s+/g, " ").trim();
function serializeEntry(entry: KbEntry): string {
const fm: string[] = ["---", `title: ${flattenFm(entry.title)}`];
if (entry.tags.length) fm.push(`tags: [${entry.tags.join(", ")}]`);
if (entry.layer === "sources") {
if (entry.created) fm.push(`created: ${entry.created}`);
if (entry.origin) fm.push(`origin: ${entry.origin}`);
} else {
if (entry.updated) fm.push(`updated: ${entry.updated}`);
if (entry.sources?.length) fm.push(`sources: [${entry.sources.join(", ")}]`);
}
for (const [k, v] of Object.entries(entry.extra ?? {})) {
// Collapse whitespace in values, and validate keys: `extra` is a pass-through
// bag, so a key derived from untrusted data must not inject a line either.
const flat = flattenFm(v);
if (flat && !KNOWN_KEYS.has(k) && /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(k)) {
fm.push(`${k}: ${flat}`);
}
}
fm.push("---", "");
return fm.join("\n") + entry.body.trimEnd() + "\n";
}
function parseEntry(layer: KbLayer, slug: string, raw: string): KbEntry {
const { meta, body } = parseFrontmatter(raw);
const extra: Record<string, string> = {};
for (const [k, v] of Object.entries(meta)) {
if (!KNOWN_KEYS.has(k) && typeof v === "string") extra[k] = v;
}
return {
layer,
slug,
title: (meta.title as string) || slug,
tags: (meta.tags as string[]) ?? [],
created: meta.created as string | undefined,
updated: meta.updated as string | undefined,
origin: meta.origin as string | undefined,
sources: (meta.sources as string[]) ?? undefined,
extra: Object.keys(extra).length ? extra : undefined,
// Serialization always appends a trailing newline; strip it on read so a
// write→read round-trip is stable (body "x" stores as "x\n", reads as "x").
body: body.trimEnd(),
};
}
function metaOf(entry: KbEntry): KbEntryMeta {
return {
layer: entry.layer,
slug: entry.slug,
title: entry.title,
tags: entry.tags,
created: entry.created,
updated: entry.updated,
origin: entry.origin,
sources: entry.sources,
extra: entry.extra,
excerpt: entry.body.replace(/\s+/g, " ").trim().slice(0, 160),
};
}
// ── scaffold ──────────────────────────────────────────────────────────
/** Create the KB layout (dirs + default schema) if missing. Idempotent. */
export async function ensureKbScaffold(): Promise<void> {
await ensureDir(kbSourcesDir());
await ensureDir(kbWikiDir());
await ensureSchema();
}
/**
* A slug that doesn't collide with an existing entry. `base` is already minted
* (kbSlug) — this only appends -2, -3 … when the file is taken.
*/
async function uniqueSlug(layer: KbLayer, base: string): Promise<string> {
const root = base || `entry-${Date.now()}`;
let slug = root;
let n = 2;
while (await pathExists(entryFile(layer, slug))) {
slug = `${root}-${n++}`;
}
return slug;
}
// ── reads ─────────────────────────────────────────────────────────────
export async function readEntry(layer: KbLayer, slug: string): Promise<KbEntry | null> {
const file = entryFile(layer, slug);
if (!(await pathExists(file))) return null;
return parseEntry(layer, slug, await fs.readFile(file, "utf8"));
}
/** Full entries (with bodies), newest first. Omit `layer` for all. */
export async function listFullEntries(layer?: KbLayer): Promise<KbEntry[]> {
const layers: KbLayer[] = layer ? [layer] : ["wiki", "sources"];
const out: KbEntry[] = [];
for (const L of layers) {
const dir = layerDir(L);
if (!(await pathExists(dir))) continue;
for (const f of await fs.readdir(dir)) {
if (!f.endsWith(".md")) continue;
const slug = f.slice(0, -3);
try {
out.push(parseEntry(L, slug, await fs.readFile(`${dir}/${f}`, "utf8")));
} catch {
// skip unparseable
}
}
}
out.sort((a, b) =>
(b.updated || b.created || "").localeCompare(a.updated || a.created || ""),
);
return out;
}
/** List entry metadata (no full body), newest first. Omit `layer` for all. */
export async function listEntries(layer?: KbLayer): Promise<KbEntryMeta[]> {
return (await listFullEntries(layer)).map(metaOf);
}
export async function readIndex(): Promise<string> {
return readTextOrEmpty(kbIndexFile());
}
// ── index regeneration ────────────────────────────────────────────────
/** How many of each section the index shows before it stops. */
const INDEX_LIMITS = { hubs: 40, tags: 24, sources: 25, orphans: 8, broken: 6 };
/**
* Render index.md — a ranked map-of-content, not a flat listing.
*
* index.md is injected into EVERY system prompt and hard-capped there
* (prompt.ts), so once the KB grows past the cap a flat list gets truncated at
* an arbitrary point and whatever was below the line silently stops existing as
* far as Lisa is concerned. Ordering wiki pages by (backlinks × recency) means
* the cut always falls on the least-connected tail.
*
* Sources are listed by TITLE ONLY, deliberately. Layer 1 is raw captured
* material — including, after link ingest, whole web pages. index.md is
* always-on prompt text, so putting the opening of an arbitrary web page in
* there is a direct "any page on the internet → Lisa's system prompt" path.
* Titles are enough for a map; the body is one kb_read away.
* (Wiki pages DO show a gist: Lisa wrote those herself.)
*
* Pure so the format is testable without touching disk.
*/
export function renderIndex(entries: KbEntry[], opts: { now?: number } = {}): string {
const graph = buildGraph(entries, opts);
const wiki = entries.filter((e) => e.layer === "wiki");
const sources = entries.filter((e) => e.layer === "sources");
const lines = [
"# Knowledge base index",
"",
`_${wiki.length} wiki page(s) · ${sources.length} source(s)_`,
"",
];
if (graph.hubs.length) {
lines.push("## Wiki pages (most-linked first)", "");
for (const hub of graph.hubs.slice(0, INDEX_LIMITS.hubs)) {
const n = graph.nodes.get(hub.key)!;
const tags = n.tags.length ? ` · ${n.tags.map((t) => `#${t}`).join(" ")}` : "";
const links = hub.backlinks ? ` ↔${hub.backlinks}` : "";
lines.push(`- **${n.title}** (\`${n.slug}\`)${links}${tags} — ${n.gist.slice(0, 100)}`);
}
if (graph.hubs.length > INDEX_LIMITS.hubs) {
lines.push(`- … ${graph.hubs.length - INDEX_LIMITS.hubs} more (kb_list / kb_search)`);
}
lines.push("");
}
const tags = sortedTags(graph);
if (tags.length) {
lines.push(
"## Tags",
"",
tags
.slice(0, INDEX_LIMITS.tags)
.map((t) => `#${t.tag}(${t.count})`)
.join(" "),
"",
);
}
if (sources.length) {
lines.push("## Recent sources", "");
for (const s of sources.slice(0, INDEX_LIMITS.sources)) {
const tags2 = s.tags.length ? ` · ${s.tags.map((t) => `#${t}`).join(" ")}` : "";
const date = (s.created ?? "").slice(0, 10);
lines.push(`- ${date ? `${date} · ` : ""}${s.title} (\`${s.slug}\`)${tags2}`);
}
if (sources.length > INDEX_LIMITS.sources) {
lines.push(`- … ${sources.length - INDEX_LIMITS.sources} older (kb_list sources)`);
}
lines.push("");
}
// The wiki's to-do list: pages nothing connects to, and links that point at
// nothing. This is what the idle "tend the wiki" pass acts on.
if (graph.orphans.length) {
const shown = graph.orphans.slice(0, INDEX_LIMITS.orphans).map((k) => `\`${k}\``).join(", ");
const more = graph.orphans.length > INDEX_LIMITS.orphans ? ", …" : "";
lines.push(`_Unlinked pages (worth connecting): ${shown}${more}_`, "");
}
if (graph.broken.length) {
const shown = graph.broken
.slice(0, INDEX_LIMITS.broken)
.map((b) => `${b.from} → \`${b.target}\``)
.join(", ");
const more = graph.broken.length > INDEX_LIMITS.broken ? ", …" : "";
lines.push(`_Links to pages that don't exist yet: ${shown}${more}_`, "");
}
return lines.join("\n").trimEnd() + "\n";
}
/**
* Rebuild index.md + index.json from the current wiki + sources.
* Assumes the lock is held.
*/
async function regenerateIndexLocked(): Promise<void> {
const entries = await listFullEntries();
const now = new Date();
await atomicWrite(kbIndexFile(), renderIndex(entries, { now: now.getTime() }));
await atomicWrite(
kbGraphFile(),
JSON.stringify(
graphToJson(buildGraph(entries, { now: now.getTime() }), now.toISOString()),
null,
2,
) + "\n",
);
}
/** Public index rebuild (acquires the lock). */
export function regenerateIndex(): Promise<void> {
return withFileLock(kbLockPath(), regenerateIndexLocked);
}
// ── writes ────────────────────────────────────────────────────────────
/**
* Capture a Layer-1 source (immutable raw). Each call writes a NEW file — the
* slug is made unique so nothing is ever overwritten. Backs the web "add to KB".
*/
export async function addSource(opts: {
title: string;
body: string;
tags?: string[];
origin?: string;
/** Provenance frontmatter (url / site / author / published / hash / via …). */
extra?: Record<string, string>;
}): Promise<KbEntry> {
await ensureKbScaffold();
return withFileLock(kbLockPath(), async () => {
const title = opts.title.trim() || "untitled";
const created = new Date().toISOString();
const slug = await uniqueSlug(
"sources",
kbSlug({ title, url: opts.extra?.url, date: created }),
);
const entry: KbEntry = {
layer: "sources",
slug,
title,
tags: opts.tags ?? [],
created,
origin: opts.origin,
extra: opts.extra,
body: opts.body,
};
await atomicWrite(entryFile("sources", slug), serializeEntry(entry));
await regenerateIndexLocked();
await commitKb(`kb: add source ${slug}`);
return entry;
});
}
/**
* Create or update a Layer-2 wiki page (upsert by slug). Lisa's curation tool.
*/
export async function writeWiki(opts: {
slug?: string;
title: string;
body: string;
tags?: string[];
sources?: string[];
extra?: Record<string, string>;
}): Promise<KbEntry> {
await ensureKbScaffold();
return withFileLock(kbLockPath(), async () => {
const slug = opts.slug
? assertSafeSlug(opts.slug)
: kbSlug({ title: opts.title }) || `page-${Date.now()}`;
const entry: KbEntry = {
layer: "wiki",
slug,
title: opts.title.trim() || slug,
tags: opts.tags ?? [],
updated: new Date().toISOString(),
sources: opts.sources ?? [],
extra: opts.extra,
body: opts.body,
};
await atomicWrite(entryFile("wiki", slug), serializeEntry(entry));
await regenerateIndexLocked();
await commitKb(`kb: write wiki ${slug}`);
return entry;
});
}
/** Delete an entry (user-initiated). Returns false if it didn't exist. */
export async function removeEntry(layer: KbLayer, slug: string): Promise<boolean> {
return withFileLock(kbLockPath(), async () => {
const file = entryFile(layer, slug);
if (!(await pathExists(file))) return false;
await fs.rm(file, { force: true });
await regenerateIndexLocked();
await commitKb(`kb: remove ${layer}/${slug}`);
return true;
});
}