Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(repair): A-65 captured-facts counts and lists corpus files it could not read
garrytan80de2d812fe0
diff --git a/docs/guides/repair.md b/docs/guides/repair.mdindex 7406200baa..257fb7fc8c 100644--- a/docs/guides/repair.md+++ b/docs/guides/repair.md@@ -1248,7 +1248,10 @@ gbrain doctor| `excluded:legitimate_duplicate` | The same claim and entity also has an active fact from another lane or an unsuspected session, so the claim is legitimate. | Never |Sessions that cannot be classified on this host (the harness transcript was-pruned, the corpus file is gone) are counted as `unclassifiable` and kept.+pruned, the corpus file is gone, or a corpus file or directory it could not+read) are counted as `unclassifiable` and kept. Each unreadable corpus file or+directory is listed as `unreadable_corpus_file` and counted in the+`unreadable_corpus_files` residual; fix its permissions and preview again.`captured_facts_active` counts facts; the `self_capture` check counts corpusfiles. Clear both: quarantine the files with the commands `self_capture`prints, then expire the facts here.diff --git a/src/core/repair/captured-facts.ts b/src/core/repair/captured-facts.tsindex 5b639c7f4b..1cff4c4499 100644--- a/src/core/repair/captured-facts.ts+++ b/src/core/repair/captured-facts.ts@@ -34,7 +34,7 @@* `changed_since_preview` and kept.*/import { existsSync, readdirSync, readFileSync, type Dirent } from 'node:fs';-import { basename, dirname, join } from 'node:path';+import { basename, dirname, join, relative } from 'node:path';import type { BrainEngine } from '../engine.ts';import { opError, OperationError } from '../ops/contract.ts';import { readFix } from '../ops/op-fix.ts';@@ -82,6 +82,8 @@ export interface CapturedFactsClassification {candidates: CapturedFactCandidate[];unclassifiable: number;not_suspect: number;+ /** GBRA-130 A-65: corpus directories and session files the classification could not read (relative to the corpus dir). */+ unreadable_corpus_files: string[];}export interface CapturedFactsPage {@@ -108,13 +110,13 @@ function words(text: string): Set<string> {return new Set((text.toLowerCase().match(/[\p{L}\p{N}]{3,}/gu) ?? []).filter(w => !STOPWORDS.has(w)));}-/** Every `.txt` under `root` (dream discovery's pruning), keyed by its session id. */-function sessionFiles(root: string | null | undefined): Map<string, string[]> {+/** Every `.txt` under `root` (dream discovery's pruning), keyed by its session id; a directory it cannot list goes to `unreadable`. */+function sessionFiles(root: string | null | undefined, unreadable: string[]): Map<string, string[]> {const bySession = new Map<string, string[]>();if (!root || !existsSync(root)) return bySession;const walk = (dir: string) => {let entries: Dirent[];- try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return; }+ try { entries = readdirSync(dir, { withFileTypes: true }); } catch { unreadable.push(dir); return; }for (const entry of entries) {const full = join(dir, entry.name);if (entry.isDirectory()) { if (pruneDir(entry.name, dir)) walk(full); }@@ -163,20 +165,22 @@ export async function classifyCapturedFacts(db: BrainEngine, sourceIds: string[]WHERE f.source_id=ANY($1::text[]) AND f.source=ANY($2::text[]) AND f.expired_at IS NULL AND f.superseded_by IS NULLAND ($3::bigint[] IS NULL OR f.id=ANY($3::bigint[]))ORDER BY f.source_id, f.id`, [sourceIds, [...CAPTURE_LANES], opts.ids ?? null]);- if (!rows.length) return { candidates: [], unclassifiable: 0, not_suspect: 0 };+ if (!rows.length) return { candidates: [], unclassifiable: 0, not_suspect: 0, unreadable_corpus_files: [] };const projectsRoot = opts.projectsRoot ?? claudeProjectsDir();const selfDirs = claudeCliSelfProjectDirs(projectsRoot);const known = harnessSessionIds(projectsRoot);const corpusDir = opts.corpusDir === undefined ? await db.getConfig(CORPUS_KEY) : opts.corpusDir;- const corpus = sessionFiles(corpusDir);- const quarantine = sessionFiles(corpusDir ? join(dirname(corpusDir), `${basename(corpusDir)}.quarantine`) : null);+ const unreadable: string[] = [];+ const corpus = sessionFiles(corpusDir, unreadable);+ const quarantine = sessionFiles(corpusDir ? join(dirname(corpusDir), `${basename(corpusDir)}.quarantine`) : null, unreadable);+ const unreadSessions = new Set<string>();const pasteWords = new Map<string, { file: string; paste: Set<string> } | null>();const pasteOnlyWords = (session: string) => {if (pasteWords.has(session)) return pasteWords.get(session)!;let found: { file: string; paste: Set<string> } | null = null;for (const file of corpus.get(session) ?? []) {let raw: string;- try { raw = readFileSync(file, 'utf8'); } catch { continue; }+ try { raw = readFileSync(file, 'utf8'); } catch { unreadable.push(file); unreadSessions.add(session); continue; }if (!raw.includes('<pasted_content')) continue;const own = words(corpusTextForExtraction(file, raw));const paste = new Set([...words(raw)].filter(w => !own.has(w)));@@ -204,7 +208,7 @@ export async function classifyCapturedFacts(db: BrainEngine, sourceIds: string[]return { klass: 'ambiguous', reason: 'paste_heuristic', session, evidence: `${basename(paste.file)}: ${covered}/${claim.length} words only in paste blocks` };}}- return corpus.has(session) || known.has(session) ? 'not_suspect' : 'unclassifiable';+ return !unreadSessions.has(session) && (corpus.has(session) || known.has(session)) ? 'not_suspect' : 'unclassifiable';});// A suspect claim that also has an active copy not proven suspect (another lane, or any other capture row) is kept.const twinKey = (row: FactRow) => JSON.stringify([row.source_id, row.visibility, row.entity_slug, row.fingerprint]);@@ -219,7 +223,8 @@ export async function classifyCapturedFacts(db: BrainEngine, sourceIds: string[]evidence: hit.evidence, fact_hash: factHash(row) });});candidates.sort((a, b) => a.source_id.localeCompare(b.source_id) || a.slug.localeCompare(b.slug) || a.id - b.id);- return { candidates, unclassifiable: classified.filter(c => c === 'unclassifiable').length, not_suspect: classified.filter(c => c === 'not_suspect').length };+ return { candidates, unclassifiable: classified.filter(c => c === 'unclassifiable').length, not_suspect: classified.filter(c => c === 'not_suspect').length,+ unreadable_corpus_files: unreadable.map(path => !corpusDir ? path : path.startsWith(`${corpusDir}/`) ? relative(corpusDir, path) : relative(dirname(corpusDir), path)) };}function previewCommand(scope: RepairScope, all: string[], includeAmbiguous: boolean): string {@@ -243,7 +248,7 @@ export const capturedFactsRepair: RepairHandler = {const includeAmbiguous = opts?.includeAmbiguous === true;const command = previewCommand(scope, await activeSources(engine), includeAmbiguous);if (!opts?.apply) {- const { candidates, unclassifiable, not_suspect } = await classifyCapturedFacts(engine, scope.source_ids);+ const { candidates, unclassifiable, not_suspect, unreadable_corpus_files } = await classifyCapturedFacts(engine, scope.source_ids);const [brain] = await engine.executeRaw<{ brain_id: string }>('SELECT brain_id FROM persistence_brain WHERE singleton=1').catch(() => []);const hash = previewHash(['captured-facts-v1', brain?.brain_id ?? 'host', scope.source_ids, includeAmbiguous,candidates.map(c => [c.id, c.class, c.reason, c.fact_hash])]);@@ -260,8 +265,11 @@ export const capturedFactsRepair: RepairHandler = {const count = (klass: CapturedFactClass) => candidates.filter(c => c.class === klass).length;const listing: RepairListing[] = candidates.map(c => ({ item: `${c.source_id}:${c.slug}#${c.id}`,class: c.class === 'excluded' ? c.reason : c.class, detail: `${c.reason === 'paste_heuristic' ? 'paste candidate' : c.reason}; session ${c.session}; ${c.evidence}` }));+ for (const path of unreadable_corpus_files) listing.push({ item: path, class: 'unreadable_corpus_file',+ detail: 'not read (check its permissions); facts of its sessions are counted unclassifiable and kept' });return { items: approved.map((page, i) => pageItem(page, hash, i, i === approved.length - 1)), preview_hash: hash, listing,- residuals: { evidenced: count('evidenced'), ambiguous: count('ambiguous'), excluded: count('excluded'), unclassifiable, not_suspect } };+ residuals: { evidenced: count('evidenced'), ambiguous: count('ambiguous'), excluded: count('excluded'), unclassifiable, not_suspect,+ ...(unreadable_corpus_files.length ? { unreadable_corpus_files: unreadable_corpus_files.length } : {}) } };}if (!opts.expect) {throw new OperationError('invalid_params', 'gbrain repair captured-facts --apply expires only the set a preview printed.',