@dzhechkov/harness-core 0.3.48 → 0.3.52

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/src/brain.ts CHANGED
@@ -15,7 +15,7 @@
15
15
  * @packageDocumentation
16
16
  */
17
17
 
18
- import { join } from 'node:path';
18
+ import { join, dirname } from 'node:path';
19
19
  import { homedir } from 'node:os';
20
20
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
21
21
  import { pathToFileURL } from 'node:url';
@@ -56,6 +56,10 @@ export interface BrainSource {
56
56
  readonly corpusVersion?: string;
57
57
  readonly lang?: string;
58
58
  readonly isbn?: string;
59
+ /** SPDX-ish license id — carried for repo sources (§8: refuse promoting unknown-license repos). */
60
+ readonly license?: string;
61
+ /** Relative path (from the brain home) to this source's capability card, e.g. `primers/<slug>.md`. */
62
+ readonly primer?: string;
59
63
  readonly addedTs: string;
60
64
  }
61
65
 
@@ -133,28 +137,58 @@ interface ProjRow {
133
137
  metadata: string | null;
134
138
  }
135
139
 
136
- /** Read the project's lexical book KB (all rows, or one `source`). Best-effort; honest errors. */
137
- async function readProjectRows(
138
- depsRoot: string,
139
- projectDbPath: string,
140
- source: string | undefined,
141
- ): Promise<{ rows: ProjRow[] } | { error: string }> {
142
- const sqlite = resolveSqlite(depsRoot);
143
- if ('error' in sqlite) return { error: sqlite.error };
140
+ /** Reconstruct a {@link BookKU} from a raw `book_knowledge` row (shared by every reader below). */
141
+ function rowToKu(r: ProjRow): BookKU {
142
+ const pages = jsonPages(r.pages);
143
+ const meta = jsonObj(r.metadata);
144
+ return {
145
+ book: r.book,
146
+ kuId: r.ku_id,
147
+ corpusVersion: r.corpus_version,
148
+ type: r.type,
149
+ name: r.name,
150
+ problem: r.problem,
151
+ content: r.content,
152
+ ...(r.chapter !== null ? { chapter: r.chapter } : {}),
153
+ ...(pages !== undefined ? { pages } : {}),
154
+ ...(Object.keys(meta).length > 0 ? { metadata: meta } : {}),
155
+ };
156
+ }
157
+
158
+ /**
159
+ * Read reconstructed KUs from ANY `books.sqlite`-format lexical store (a project store, the brain
160
+ * store, or a per-source slice) — the single shared reader behind {@link promoteProjectToBrain},
161
+ * {@link buildPrimer}, {@link exportBrainSlice}, and {@link importBrainSlice}. `source` narrows to
162
+ * one book; default reads all. Synchronous (native `better-sqlite3` via `createRequire`) and
163
+ * best-effort: an absent store or unresolved dependency returns an honest `error`, never throws.
164
+ */
165
+ export function readBookKus(opts: {
166
+ storePath: string;
167
+ depsRoot?: string;
168
+ source?: string;
169
+ }): { kus: BookKU[]; error?: string } {
170
+ const depsRoot = opts.depsRoot ?? process.cwd();
171
+ if (!existsSync(opts.storePath)) return { kus: [], error: `no book store at ${opts.storePath}` };
172
+ let Database: new (p: string, o?: object) => NativeDb;
173
+ try {
174
+ const req = createRequire(join(depsRoot, 'package.json'));
175
+ Database = req('better-sqlite3') as new (p: string, o?: object) => NativeDb;
176
+ } catch {
177
+ return { kus: [], error: 'better-sqlite3 not installed (run: dz setup --memory agentdb)' };
178
+ }
144
179
  try {
145
- const { default: Database } = (await import(sqlite.url)) as { default: new (p: string, o?: object) => NativeDb };
146
- const db = new Database(projectDbPath, { readonly: true });
180
+ const db = new Database(opts.storePath, { readonly: true });
147
181
  try {
148
182
  const cols = 'book, ku_id, corpus_version, type, name, problem, content, chapter, pages, metadata';
149
- const rows = (source !== undefined
150
- ? db.prepare(`SELECT ${cols} FROM book_knowledge WHERE book = ?`).all(source)
183
+ const rows = (opts.source !== undefined
184
+ ? db.prepare(`SELECT ${cols} FROM book_knowledge WHERE book = ?`).all(opts.source)
151
185
  : db.prepare(`SELECT ${cols} FROM book_knowledge`).all()) as ProjRow[];
152
- return { rows };
186
+ return { kus: rows.map(rowToKu) };
153
187
  } finally {
154
188
  db.close();
155
189
  }
156
190
  } catch (err) {
157
- return { error: `read project book KB failed: ${err instanceof Error ? err.message : String(err)}` };
191
+ return { kus: [], error: `read book KB failed: ${err instanceof Error ? err.message : String(err)}` };
158
192
  }
159
193
  }
160
194
 
@@ -220,108 +254,341 @@ function worthScore(meta: Record<string, unknown>): number {
220
254
  }
221
255
  }
222
256
 
257
+ // ──────────────────────────────────────── Primers ───────────────────────────────────────────
258
+
259
+ /** Clamp + whitespace-collapse a one-line problem for the primer (deterministic). */
260
+ function primerLine(s: string, max = 120): string {
261
+ const flat = s.replace(/\s+/g, ' ').trim();
262
+ return flat.length <= max ? flat : `${flat.slice(0, max - 1).trimEnd()}…`;
263
+ }
264
+
223
265
  /**
224
- * Promote a PROJECT's digitized book KB into the durable cross-project brain (ADR-001 P0
225
- * `book-brain-register`). Reads the project's lexical `books.sqlite`, groups KUs by book, and for
226
- * each book mirrors them into the brain's lexical store ({@link putBookKnowledge}) and re-embeds
227
- * them into the brain's vector store ({@link indexPatternsToAgentdb}), then refreshes the registry.
228
- *
229
- * **Non-clobbering:** promoting the same book twice does not duplicate — the lexical upsert keys on
230
- * `(book, ku_id, corpus_version)` and the vector rows are pre-deleted before re-index.
266
+ * Render a source's capability card (ADR-001 §5.4) from its registry entry + KUs — PURE and
267
+ * DETERMINISTIC (no clock, no query): a header (slug/kind/kuCount/corpusVersion/isbn/lang/license),
268
+ * a KU-TYPE histogram (count per type, count-desc then type-asc), and the top ~8 decision/framework
269
+ * KUs (name + one-line problem, page-anchored), ranked by declared `worth` then `kuId`.
231
270
  */
232
- export async function promoteProjectToBrain(opts: {
233
- projectRoot: string;
271
+ function buildPrimerMarkdown(source: BrainSource, kus: readonly BookKU[]): string {
272
+ const lines: string[] = [`# Primer: ${source.slug}`, ''];
273
+ lines.push(`- kind: ${source.kind}`);
274
+ lines.push(`- KUs: ${source.kuCount}`);
275
+ if (source.corpusVersion !== undefined && source.corpusVersion !== '') lines.push(`- corpusVersion: ${source.corpusVersion}`);
276
+ if (source.isbn !== undefined) lines.push(`- isbn: ${source.isbn}`);
277
+ if (source.lang !== undefined) lines.push(`- lang: ${source.lang}`);
278
+ if (source.license !== undefined) lines.push(`- license: ${source.license}`);
279
+ lines.push('');
280
+
281
+ // KU-type histogram — count per type, count-desc then type-asc (stable, deterministic).
282
+ const counts = new Map<string, number>();
283
+ for (const k of kus) counts.set(k.type, (counts.get(k.type) ?? 0) + 1);
284
+ const hist = [...counts.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
285
+ lines.push('## KU types');
286
+ for (const [type, n] of hist) lines.push(`- ${type}: ${n}`);
287
+ lines.push('');
288
+
289
+ // Top decision-grade KUs — worth-desc then kuId-asc, first 8; page-anchored provenance. Match the
290
+ // ACTUAL KU type vocabulary (decision-framework / tradeoff-table / methodology / heuristic), not the
291
+ // legacy 'decision'/'framework' shorthands that never occur in real KUs (primer showed _none_).
292
+ const DECISION_TYPES = new Set(['decision-framework', 'tradeoff-table', 'methodology', 'heuristic', 'decision', 'framework']);
293
+ const top = kus
294
+ .filter((k) => DECISION_TYPES.has(k.type))
295
+ .sort((a, b) => worthScore(b.metadata ?? {}) - worthScore(a.metadata ?? {}) || a.kuId.localeCompare(b.kuId))
296
+ .slice(0, 8);
297
+ lines.push('## Top decision moments');
298
+ if (top.length === 0) {
299
+ lines.push('_none_');
300
+ } else {
301
+ top.forEach((k, i) => {
302
+ const ch = k.chapter !== undefined && k.chapter !== '' ? ` гл.${k.chapter}` : '';
303
+ const pg = k.pages !== undefined && k.pages.length > 0 ? ` с.${k.pages.join('–')}` : '';
304
+ lines.push(`${i + 1}. ${k.name} — ${primerLine(k.problem)}${ch}${pg}`);
305
+ });
306
+ }
307
+ lines.push('');
308
+ return lines.join('\n');
309
+ }
310
+
311
+ /** Synthesize a {@link BrainSource} for a primer when the registry has no entry yet (metadata-derived). */
312
+ function sourceFromKus(slug: string, kus: readonly BookKU[]): BrainSource {
313
+ const meta0 = kus[0]?.metadata ?? {};
314
+ const corpusVersion = kus[0]?.corpusVersion ?? '';
315
+ return {
316
+ slug,
317
+ kind: 'book',
318
+ kuCount: kus.length,
319
+ addedTs: '',
320
+ ...(corpusVersion !== '' ? { corpusVersion } : {}),
321
+ ...(typeof meta0['lang'] === 'string' ? { lang: meta0['lang'] as string } : {}),
322
+ ...(typeof meta0['isbn'] === 'string' ? { isbn: meta0['isbn'] as string } : {}),
323
+ ...(typeof meta0['license'] === 'string' ? { license: meta0['license'] as string } : {}),
324
+ };
325
+ }
326
+
327
+ /**
328
+ * Build a source's capability card (ADR-001 §5.4) by reading its KUs from the brain lexical store
329
+ * ({@link readBookKus}) + its registry entry, then rendering {@link buildPrimerMarkdown}. Best-effort:
330
+ * a missing store/source returns an honest `error` with empty `markdown`, never throws.
331
+ */
332
+ export async function buildPrimer(opts: {
333
+ brainHome?: string;
234
334
  depsRoot?: string;
335
+ slug: string;
336
+ }): Promise<{ markdown: string; error?: string }> {
337
+ const home = opts.brainHome ?? brainHome();
338
+ const depsRoot = opts.depsRoot ?? process.cwd();
339
+ const read = readBookKus({ storePath: brainBooksPath(home), depsRoot, source: opts.slug });
340
+ if (read.error !== undefined) return { markdown: '', error: read.error };
341
+ if (read.kus.length === 0) return { markdown: '', error: `no KUs for source '${opts.slug}' in brain` };
342
+ const source = readRegistry(home).sources[opts.slug] ?? sourceFromKus(opts.slug, read.kus);
343
+ return { markdown: buildPrimerMarkdown(source, read.kus) };
344
+ }
345
+
346
+ /** Write a source's primer to `<brainHome>/primers/<slug>.md` (mkdir). Best-effort; honest error. */
347
+ export async function writePrimer(opts: {
235
348
  brainHome?: string;
236
- source?: string;
237
- addedTs: string;
238
- }): Promise<{ sources: string[]; kus: number; error?: string }> {
349
+ depsRoot?: string;
350
+ slug: string;
351
+ }): Promise<{ path: string; error?: string }> {
239
352
  const home = opts.brainHome ?? brainHome();
240
- const depsRoot = opts.depsRoot ?? opts.projectRoot;
241
- const projectDbPath = bookKbPath(opts.projectRoot);
242
- if (!existsSync(projectDbPath)) return { sources: [], kus: 0, error: 'no project book KB' };
353
+ const path = join(home, 'primers', `${opts.slug}.md`);
354
+ const built = await buildPrimer(opts);
355
+ if (built.error !== undefined) return { path, error: built.error };
356
+ try {
357
+ mkdirSync(dirname(path), { recursive: true });
358
+ writeFileSync(path, built.markdown);
359
+ return { path };
360
+ } catch (err) {
361
+ return { path, error: `write primer failed: ${err instanceof Error ? err.message : String(err)}` };
362
+ }
363
+ }
243
364
 
244
- const read = await readProjectRows(depsRoot, projectDbPath, opts.source);
245
- if ('error' in read) return { sources: [], kus: 0, error: read.error };
246
- if (read.rows.length === 0) return { sources: [], kus: 0 };
365
+ // ──────────────────────────────────────── Mirror ────────────────────────────────────────────
247
366
 
248
- // Group rows by book (each book is one promote unit + one registry entry).
249
- const byBook = new Map<string, ProjRow[]>();
250
- for (const r of read.rows) {
251
- const bucket = byBook.get(r.book);
252
- if (bucket) bucket.push(r);
253
- else byBook.set(r.book, [r]);
367
+ /**
368
+ * The ONE shared "mirror KUs into the brain + register + primer" path behind
369
+ * {@link promoteProjectToBrain}, {@link importBrainSlice}, and {@link registerKusToBrain}. Groups
370
+ * the KUs by `book` and, per book: (1) lexical-mirrors via the idempotent {@link putBookKnowledge},
371
+ * (2) re-embeds via {@link indexPatternsToAgentdb} after pre-deleting the book's stale vectors, and
372
+ * (3) writes/refreshes the registry entry — then generates `primers/<slug>.md` best-effort (a primer
373
+ * failure never fails the mirror) and stores its relative path on the entry.
374
+ *
375
+ * **Non-clobbering:** mirroring the same book twice does not duplicate — the lexical upsert keys on
376
+ * `(book, ku_id, corpus_version)` and the vector rows are pre-deleted before re-index.
377
+ */
378
+ async function mirrorKusToBrain(opts: {
379
+ home: string;
380
+ depsRoot: string;
381
+ kus: readonly BookKU[];
382
+ kind: 'book' | 'repo' | 'paper';
383
+ addedTs: string;
384
+ lang?: string;
385
+ license?: string;
386
+ }): Promise<{ sources: string[]; kus: number; error?: string }> {
387
+ const { home, depsRoot } = opts;
388
+ if (opts.kus.length === 0) return { sources: [], kus: 0 };
389
+
390
+ // Group KUs by book (each book is one mirror unit + one registry entry).
391
+ const byBook = new Map<string, BookKU[]>();
392
+ for (const k of opts.kus) {
393
+ const bucket = byBook.get(k.book);
394
+ if (bucket) bucket.push(k);
395
+ else byBook.set(k.book, [k]);
254
396
  }
255
397
 
256
398
  const reg = readRegistry(home);
257
399
  const sources: Record<string, BrainSource> = { ...reg.sources };
258
- const promoted: string[] = [];
400
+ const mirrored: string[] = [];
259
401
  let totalKus = 0;
260
402
  let firstError: string | undefined;
261
403
 
262
- for (const [book, rows] of byBook) {
404
+ for (const [book, kus] of byBook) {
263
405
  // 1. Lexical mirror — reuse the idempotent upsert, pointed at the brain store.
264
- const kus: BookKU[] = rows.map((r) => {
265
- const pages = jsonPages(r.pages);
266
- const meta = jsonObj(r.metadata);
267
- return {
268
- book: r.book,
269
- kuId: r.ku_id,
270
- corpusVersion: r.corpus_version,
271
- type: r.type,
272
- name: r.name,
273
- problem: r.problem,
274
- content: r.content,
275
- ...(r.chapter !== null ? { chapter: r.chapter } : {}),
276
- ...(pages !== undefined ? { pages } : {}),
277
- ...(Object.keys(meta).length > 0 ? { metadata: meta } : {}),
278
- };
279
- });
280
406
  const lex = await putBookKnowledge(depsRoot, kus, { dbPath: brainBooksPath(home) });
281
407
  if (lex.error !== undefined && firstError === undefined) firstError = lex.error;
282
408
 
283
409
  // 2. Vector re-embed — pre-delete this book's stale vectors (insert-only primitive), then index.
284
410
  await deleteBrainVectors(depsRoot, brainAgentdbPath(home), book);
285
- const vecRows: AgentdbRow[] = rows.map((r) => {
286
- const meta = jsonObj(r.metadata);
287
- return {
288
- taskType: 'book-knowledge',
289
- text: `${r.name}: ${r.problem}`,
290
- score: worthScore(meta),
291
- tags: ['book', r.book, r.type],
292
- metadata: {
293
- chapter: r.chapter,
294
- pages: jsonPages(r.pages) ?? null,
295
- ku_id: r.ku_id,
296
- corpus_version: r.corpus_version,
297
- },
298
- };
299
- });
411
+ const vecRows: AgentdbRow[] = kus.map((k) => ({
412
+ taskType: 'book-knowledge',
413
+ text: `${k.name}: ${k.problem}`,
414
+ score: worthScore(k.metadata ?? {}),
415
+ tags: ['book', k.book, k.type],
416
+ metadata: {
417
+ chapter: k.chapter ?? null,
418
+ pages: k.pages ?? null,
419
+ ku_id: k.kuId,
420
+ corpus_version: k.corpusVersion,
421
+ },
422
+ }));
300
423
  const vec = await indexPatternsToAgentdb(depsRoot, vecRows, { dbPath: brainAgentdbPath(home) });
301
424
  if (vec.error !== undefined && firstError === undefined) firstError = vec.error;
302
425
 
303
- // 3. Registry entry for this book — corpus_version/lang/isbn from the first row's metadata.
304
- const meta0 = jsonObj(rows[0]!.metadata);
305
- const corpusVersion = rows[0]!.corpus_version;
306
- const lang = typeof meta0['lang'] === 'string' ? (meta0['lang'] as string) : undefined;
426
+ // 3. Registry entry — corpus_version/lang/isbn/license from opts (explicit) then first KU's metadata.
427
+ const meta0 = kus[0]!.metadata ?? {};
428
+ const corpusVersion = kus[0]!.corpusVersion;
429
+ const lang = opts.lang ?? (typeof meta0['lang'] === 'string' ? (meta0['lang'] as string) : undefined);
307
430
  const isbn = typeof meta0['isbn'] === 'string' ? (meta0['isbn'] as string) : undefined;
308
- sources[book] = {
431
+ const license = opts.license ?? (typeof meta0['license'] === 'string' ? (meta0['license'] as string) : undefined);
432
+ const entry: BrainSource = {
309
433
  slug: book,
310
- kind: 'book',
311
- kuCount: rows.length,
434
+ kind: opts.kind,
435
+ kuCount: kus.length,
312
436
  addedTs: opts.addedTs,
313
437
  ...(corpusVersion !== undefined && corpusVersion !== '' ? { corpusVersion } : {}),
314
438
  ...(lang !== undefined ? { lang } : {}),
315
439
  ...(isbn !== undefined ? { isbn } : {}),
440
+ ...(license !== undefined ? { license } : {}),
316
441
  };
317
- promoted.push(book);
318
- totalKus += rows.length;
442
+
443
+ // 4. Primer — best-effort: a primer failure must not fail the mirror. Store the relative path.
444
+ let primerRel: string | undefined;
445
+ try {
446
+ const rel = join('primers', `${book}.md`);
447
+ const primerPath = join(home, rel);
448
+ mkdirSync(dirname(primerPath), { recursive: true });
449
+ writeFileSync(primerPath, buildPrimerMarkdown(entry, kus));
450
+ primerRel = rel;
451
+ } catch {
452
+ /* primer is advisory — never fail the mirror on it */
453
+ }
454
+
455
+ sources[book] = primerRel !== undefined ? { ...entry, primer: primerRel } : entry;
456
+ mirrored.push(book);
457
+ totalKus += kus.length;
319
458
  }
320
459
 
321
460
  writeRegistry(home, { version: 1, sources });
322
461
  return firstError !== undefined
323
- ? { sources: promoted, kus: totalKus, error: firstError }
324
- : { sources: promoted, kus: totalKus };
462
+ ? { sources: mirrored, kus: totalKus, error: firstError }
463
+ : { sources: mirrored, kus: totalKus };
464
+ }
465
+
466
+ // ──────────────────────────────────────── Promote ───────────────────────────────────────────
467
+
468
+ /**
469
+ * Promote a PROJECT's digitized book KB into the durable cross-project brain (ADR-001 P0
470
+ * `book-brain-register`). Reads the project's lexical `books.sqlite` ({@link readBookKus}) and mirrors
471
+ * every KU into the brain via the shared {@link mirrorKusToBrain} path (lexical + vector + registry +
472
+ * primer). `kind: 'book'`.
473
+ *
474
+ * **Non-clobbering:** promoting the same book twice does not duplicate — the lexical upsert keys on
475
+ * `(book, ku_id, corpus_version)` and the vector rows are pre-deleted before re-index.
476
+ */
477
+ export async function promoteProjectToBrain(opts: {
478
+ projectRoot: string;
479
+ depsRoot?: string;
480
+ brainHome?: string;
481
+ source?: string;
482
+ addedTs: string;
483
+ }): Promise<{ sources: string[]; kus: number; error?: string }> {
484
+ const home = opts.brainHome ?? brainHome();
485
+ const depsRoot = opts.depsRoot ?? opts.projectRoot;
486
+ const projectDbPath = bookKbPath(opts.projectRoot);
487
+ if (!existsSync(projectDbPath)) return { sources: [], kus: 0, error: 'no project book KB' };
488
+
489
+ const read = readBookKus({ storePath: projectDbPath, depsRoot, ...(opts.source !== undefined ? { source: opts.source } : {}) });
490
+ if (read.error !== undefined) return { sources: [], kus: 0, error: read.error };
491
+ if (read.kus.length === 0) return { sources: [], kus: 0 };
492
+
493
+ return mirrorKusToBrain({ home, depsRoot, kus: read.kus, kind: 'book', addedTs: opts.addedTs });
494
+ }
495
+
496
+ // ────────────────────────────────────── Slices / ingest ─────────────────────────────────────
497
+
498
+ /**
499
+ * Export ONE source's KUs from the brain as a STANDALONE lexical-only `books.sqlite` slice at
500
+ * `outPath` (ADR-001 §8.1). Reads via {@link readBookKus} and writes via {@link putBookKnowledge} —
501
+ * lexical only; vectors re-embed on import. This is the portable, per-book shareable unit.
502
+ */
503
+ export async function exportBrainSlice(opts: {
504
+ brainHome?: string;
505
+ depsRoot?: string;
506
+ slug: string;
507
+ outPath: string;
508
+ }): Promise<{ kuCount: number; error?: string }> {
509
+ const home = opts.brainHome ?? brainHome();
510
+ const depsRoot = opts.depsRoot ?? process.cwd();
511
+ const read = readBookKus({ storePath: brainBooksPath(home), depsRoot, source: opts.slug });
512
+ if (read.error !== undefined) return { kuCount: 0, error: read.error };
513
+ if (read.kus.length === 0) return { kuCount: 0, error: `no KUs for source '${opts.slug}' in brain` };
514
+ const put = await putBookKnowledge(depsRoot, read.kus, { dbPath: opts.outPath });
515
+ if (put.error !== undefined) return { kuCount: 0, error: put.error };
516
+ return { kuCount: read.kus.length };
517
+ }
518
+
519
+ /**
520
+ * Import a per-book slice ({@link exportBrainSlice} output, or a pack's `brain/<slug>.sqlite`) into
521
+ * the brain (ADR-001 §8.1). Reads the slice's KUs ({@link readBookKus}) and mirrors them via the same
522
+ * non-clobbering {@link mirrorKusToBrain} path as promote (upsert + re-embed + registry + primer).
523
+ */
524
+ export async function importBrainSlice(opts: {
525
+ brainHome?: string;
526
+ depsRoot?: string;
527
+ slicePath: string;
528
+ addedTs: string;
529
+ }): Promise<{ sources: string[]; kus: number; error?: string }> {
530
+ const home = opts.brainHome ?? brainHome();
531
+ const depsRoot = opts.depsRoot ?? process.cwd();
532
+ const read = readBookKus({ storePath: opts.slicePath, depsRoot });
533
+ if (read.error !== undefined) return { sources: [], kus: 0, error: read.error };
534
+ if (read.kus.length === 0) return { sources: [], kus: 0 };
535
+ return mirrorKusToBrain({ home, depsRoot, kus: read.kus, kind: 'book', addedTs: opts.addedTs });
536
+ }
537
+
538
+ /**
539
+ * Register an array of already-shaped KUs (from a repo deep-walk, §6, or raw JSON) into the brain via
540
+ * the shared {@link mirrorKusToBrain} path — the CLI `--from-kus` backend. `slug` is authoritative:
541
+ * every KU is registered under it (so a repo's `book` field is normalized to the source slug). The
542
+ * registry entry carries `kind` and, for `repo`, `license`.
543
+ */
544
+ /**
545
+ * SPDX ids the license gate treats as clearly-permissive (auto-pass for repo ingest). Anything else
546
+ * — including an absent license — is refused unless `override:true`. Kept deliberately small and
547
+ * conservative (ADR §8: repos carry their own licenses; the brain must not silently ingest
548
+ * unknown/incompatible source into a redistributable slice).
549
+ */
550
+ export const PERMISSIVE_LICENSES: readonly string[] = [
551
+ 'MIT', 'Apache-2.0', 'BSD-2-Clause', 'BSD-3-Clause', 'ISC', 'Unlicense', 'CC0-1.0', '0BSD', 'MIT-0',
552
+ ];
553
+
554
+ export async function registerKusToBrain(opts: {
555
+ brainHome?: string;
556
+ depsRoot?: string;
557
+ kus: readonly BookKU[];
558
+ slug: string;
559
+ kind: 'repo' | 'book' | 'paper';
560
+ addedTs: string;
561
+ lang?: string;
562
+ license?: string;
563
+ override?: boolean;
564
+ }): Promise<{ kus: number; error?: string }> {
565
+ const home = opts.brainHome ?? brainHome();
566
+ const depsRoot = opts.depsRoot ?? process.cwd();
567
+ if (opts.kus.length === 0) return { kus: 0 };
568
+ // License gate (ADR §8) — repo/paper sources must declare a permissive license, else refuse unless
569
+ // explicitly overridden. Books promoted from a digitized pack are exempt (own CP5 flow). No writes
570
+ // happen before this check, so a refusal never leaves a partial brain.
571
+ if (opts.kind !== 'book' && opts.override !== true) {
572
+ const lic = opts.license;
573
+ if (lic === undefined || lic === '') {
574
+ return { kus: 0, error: `refusing to ingest '${opts.slug}' (${opts.kind}) with no --license; pass a permissive SPDX id (${PERMISSIVE_LICENSES.join(', ')}) or --override` };
575
+ }
576
+ if (!PERMISSIVE_LICENSES.includes(lic)) {
577
+ return { kus: 0, error: `refusing to ingest '${opts.slug}': license '${lic}' is not in the permissive allow-list (${PERMISSIVE_LICENSES.join(', ')}); pass --override to ingest anyway` };
578
+ }
579
+ }
580
+ // Normalize every KU under the source slug so the batch shares one `book` (putBookKnowledge's key).
581
+ const kus = opts.kus.map((k) => (k.book === opts.slug ? k : { ...k, book: opts.slug }));
582
+ const mirror = await mirrorKusToBrain({
583
+ home,
584
+ depsRoot,
585
+ kus,
586
+ kind: opts.kind,
587
+ addedTs: opts.addedTs,
588
+ ...(opts.lang !== undefined ? { lang: opts.lang } : {}),
589
+ ...(opts.license !== undefined ? { license: opts.license } : {}),
590
+ });
591
+ return mirror.error !== undefined ? { kus: mirror.kus, error: mirror.error } : { kus: mirror.kus };
325
592
  }
326
593
 
327
594
  // ───────────────────────────────────────── Query ────────────────────────────────────────────
@@ -336,11 +603,137 @@ export async function queryBrain(opts: {
336
603
  depsRoot?: string;
337
604
  source?: string;
338
605
  limit?: number;
606
+ match?: 'all' | 'any';
339
607
  }): Promise<{ hits: BookKUHit[]; error?: string }> {
340
608
  const home = opts.brainHome ?? brainHome();
341
609
  const depsRoot = opts.depsRoot ?? process.cwd();
342
- const qopts: { dbPath: string; book?: string; limit?: number } = { dbPath: brainBooksPath(home) };
610
+ const qopts: { dbPath: string; book?: string; limit?: number; match?: 'all' | 'any' } = { dbPath: brainBooksPath(home) };
343
611
  if (opts.source !== undefined) qopts.book = opts.source;
344
612
  if (opts.limit !== undefined) qopts.limit = opts.limit;
613
+ if (opts.match !== undefined) qopts.match = opts.match;
345
614
  return queryBookKnowledge(depsRoot, opts.query, qopts);
346
615
  }
616
+
617
+ // ──────────────────────────────────────── Grounding ─────────────────────────────────────────
618
+
619
+ /**
620
+ * A small bilingual (RU+EN) stopword list. Deliberately tiny — the goal is only to strip the most
621
+ * common glue words so the residual tokens are CONTENT terms worth grounding on, not to do real NLP.
622
+ */
623
+ const STOPWORDS = new Set<string>([
624
+ // EN
625
+ 'the', 'and', 'for', 'are', 'but', 'not', 'you', 'all', 'any', 'can', 'her', 'was', 'one',
626
+ 'our', 'out', 'has', 'had', 'his', 'how', 'its', 'who', 'get', 'got', 'use', 'via', 'per',
627
+ 'with', 'this', 'that', 'from', 'into', 'over', 'your', 'they', 'them', 'then', 'than', 'have',
628
+ 'will', 'what', 'when', 'why', 'where', 'which', 'about', 'would', 'should', 'could', 'does',
629
+ 'did', 'yes', 'no', 'or', 'a', 'an', 'of', 'to', 'in', 'on', 'is', 'it', 'be', 'as', 'at', 'by',
630
+ // RU
631
+ 'и', 'в', 'на', 'как', 'для', 'что', 'это', 'или', 'но', 'же', 'то', 'по', 'из', 'за', 'от',
632
+ 'до', 'со', 'об', 'при', 'без', 'над', 'под', 'про', 'так', 'уже', 'еще', 'ещё', 'вот', 'бы',
633
+ 'ли', 'не', 'ни', 'да', 'нет', 'мне', 'мой', 'моя', 'вы', 'ты', 'он', 'она', 'они', 'оно',
634
+ 'кто', 'где', 'чем', 'чей', 'быть', 'если', 'чтобы', 'этот', 'эта', 'эти', 'все', 'всё', 'меня',
635
+ ]);
636
+
637
+ /**
638
+ * Extract meaningful CONTENT TERMS from a prompt: lowercase, keep `\p{L}\p{N}` tokens of length ≥ 3,
639
+ * and drop the small RU+EN stopword list. Deterministic; deduped preserving first-seen order.
640
+ */
641
+ function contentTerms(prompt: string): string[] {
642
+ const seen = new Set<string>();
643
+ const out: string[] = [];
644
+ // Split on anything that is not a Unicode letter or number (handles RU + EN + punctuation).
645
+ for (const raw of prompt.toLowerCase().split(/[^\p{L}\p{N}]+/u)) {
646
+ if (raw.length < 3) continue;
647
+ if (STOPWORDS.has(raw)) continue;
648
+ if (seen.has(raw)) continue;
649
+ seen.add(raw);
650
+ out.push(raw);
651
+ }
652
+ return out;
653
+ }
654
+
655
+ /** Clamp a snippet to a max length, collapsing whitespace, with an ellipsis when truncated. */
656
+ function snippet(s: string, max = 160): string {
657
+ const flat = s.replace(/\s+/g, ' ').trim();
658
+ return flat.length <= max ? flat : `${flat.slice(0, max - 1).trimEnd()}…`;
659
+ }
660
+
661
+ /** The directive line prepended to every grounding block (§7.1). */
662
+ const GROUNDING_DIRECTIVE =
663
+ 'Ground your answer in the KNOWLEDGE BRAIN below; prefer these ingested sources over ' +
664
+ 'training-data recall; cite [Kn] source+page per claim; if the brain is silent on a point, say so.';
665
+
666
+ /**
667
+ * The grounding-enforcement hook entrypoint (ADR-001 §7, P1). Given a user prompt, deterministically
668
+ * builds a GROUNDING DIRECTIVE block from the brain's top lexical hits — the mechanical half of
669
+ * ruvnet-brain's "answer from source, not drift" (the agent honoring it is the soft, agent-enforced
670
+ * half, §7.2). Best-effort and **silent by design**: grounding must never inject noise or block a
671
+ * prompt, so any generic prompt, empty brain, store error, or thrown exception yields
672
+ * `{ emitted:false, block:'', hitCount:0 }` rather than an error to the user.
673
+ *
674
+ * **Relevance gate (P1):** `≥ 1 content term AND ≥ 1 lexical hit` under OR/any-term matching. The
675
+ * query uses `match:'any'` (OR), not AND: a natural-language prompt carries terms not in the KB
676
+ * (a verb like "проектирую", filler), and AND-ing them all returns 0 even when the topical terms
677
+ * (репликация, кворум) match — the too-strict gate that made real prompts silent. OR lets the
678
+ * matching terms surface KUs, FTS5-rank-ordered; the stopword filter still keeps "how are you"
679
+ * silent (no content terms). A numeric `--min-score` threshold (§7.1/§7.3) is deferred to **P3**:
680
+ * FTS5's `rank` isn't exposed and a meaningful score needs the vector (cosine) path the sync hook
681
+ * doesn't run. Until then, "≥1 content-term OR-hit clears the gate" is the honest P1 rule.
682
+ */
683
+ export async function groundPrompt(opts: {
684
+ prompt: string;
685
+ brainHome?: string;
686
+ depsRoot?: string;
687
+ k?: number;
688
+ source?: string;
689
+ }): Promise<{ emitted: boolean; block: string; hitCount: number; error?: string }> {
690
+ const silent = { emitted: false, block: '', hitCount: 0 } as const;
691
+ try {
692
+ // Gate 1: at least one CONTENT term (all-stopword prompts like "how are you" → silent).
693
+ const terms = contentTerms(opts.prompt ?? '');
694
+ if (terms.length < 1) return silent;
695
+
696
+ // Best-effort OR/any-term lexical recall over the whole brain. Never throws. `match:'any'` so a
697
+ // matching topical term surfaces KUs even when other prompt terms (a verb, filler) miss — the
698
+ // fix for real-prompt silence (AND required every term to co-occur). Rank ordering + top-K keep
699
+ // it precise; the stopword filter keeps generic prompts silent.
700
+ const query: { query: string; brainHome?: string; depsRoot?: string; source?: string; limit: number; match: 'any' } = {
701
+ query: terms.join(' '),
702
+ limit: opts.k ?? 5,
703
+ match: 'any',
704
+ };
705
+ if (opts.brainHome !== undefined) query.brainHome = opts.brainHome;
706
+ if (opts.depsRoot !== undefined) query.depsRoot = opts.depsRoot;
707
+ if (opts.source !== undefined) query.source = opts.source;
708
+ const res = await queryBrain(query);
709
+
710
+ // Gate 2: any lexical hit? On error or zero hits, stay silent (no noise, no block).
711
+ if (res.error !== undefined) return silent;
712
+ if (res.hits.length === 0) return silent;
713
+
714
+ // Gate 3 — COVERAGE (the OR balance): OR recall alone over-fires when a single common term
715
+ // ("today", "data") incidentally matches a KU. Require the retrieved hits to actually cover the
716
+ // prompt's intent: for a single-content-term prompt ("кворум") one hit suffices; for a
717
+ // multi-term prompt, ≥2 DISTINCT content terms must prefix-appear in the hit set — so an
718
+ // off-topic prompt whose only matching word is incidental ("weather today" → only "today"
719
+ // matches) stays silent, while a real one ("репликацию single multi leader" → 4 covered) grounds.
720
+ if (terms.length >= 2) {
721
+ const hay = res.hits.map((h) => `${h.name} ${h.problem} ${h.content}`.toLowerCase()).join(' ');
722
+ const covered = terms.filter((t) => hay.includes(t.toLowerCase())).length;
723
+ if (covered < 2) return silent;
724
+ }
725
+
726
+ // Build the GROUNDING DIRECTIVE block (§7.1): directive line + numbered citations.
727
+ const lines: string[] = [GROUNDING_DIRECTIVE, ''];
728
+ res.hits.forEach((h, i) => {
729
+ const ch = h.chapter !== undefined && h.chapter !== '' ? ` гл.${h.chapter}` : '';
730
+ const pg = h.pages !== undefined && h.pages.length > 0 ? ` с.${h.pages.join('–')}` : '';
731
+ const body = snippet(h.problem !== '' ? h.problem : h.content);
732
+ lines.push(`[K${i + 1}] ${h.book}${ch}${pg} — ${h.name}: ${body}`);
733
+ });
734
+ return { emitted: true, block: lines.join('\n'), hitCount: res.hits.length };
735
+ } catch {
736
+ // Grounding is advisory — a bug here must never surface to the user or block the prompt.
737
+ return silent;
738
+ }
739
+ }
package/src/index.ts CHANGED
@@ -41,6 +41,13 @@ export {
41
41
  listBrain,
42
42
  promoteProjectToBrain,
43
43
  queryBrain,
44
+ groundPrompt,
45
+ buildPrimer,
46
+ writePrimer,
47
+ readBookKus,
48
+ exportBrainSlice,
49
+ importBrainSlice,
50
+ registerKusToBrain,
44
51
  } from './brain.js';
45
52
  export type { BrainRegistry, BrainSource } from './brain.js';
46
53
  export type { AgentdbRow, AgentdbIndexResult } from './agentdb-index.js';