@seanhogg/builderforce-memory 2026.10.50 → 2026.10.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/dist/knowledge/backfillNoExtract.d.ts +37 -0
- package/dist/knowledge/backfillNoExtract.d.ts.map +1 -0
- package/dist/knowledge/backfillNoExtract.js +97 -0
- package/dist/knowledge/backfillNoExtract.js.map +1 -0
- package/dist/knowledge/extractFile.d.ts +5 -0
- package/dist/knowledge/extractFile.d.ts.map +1 -1
- package/dist/knowledge/extractFile.js +9 -0
- package/dist/knowledge/extractFile.js.map +1 -1
- package/dist/knowledge/findingBuilder.d.ts +18 -0
- package/dist/knowledge/findingBuilder.d.ts.map +1 -0
- package/dist/knowledge/findingBuilder.js +29 -0
- package/dist/knowledge/findingBuilder.js.map +1 -0
- package/dist/knowledge/findings.d.ts +2 -11
- package/dist/knowledge/findings.d.ts.map +1 -1
- package/dist/knowledge/findings.js +4 -22
- package/dist/knowledge/findings.js.map +1 -1
- package/dist/knowledge/godModule.d.ts +21 -0
- package/dist/knowledge/godModule.d.ts.map +1 -0
- package/dist/knowledge/godModule.js +42 -0
- package/dist/knowledge/godModule.js.map +1 -0
- package/dist/knowledge/index.d.ts +3 -1
- package/dist/knowledge/index.d.ts.map +1 -1
- package/dist/knowledge/index.js +2 -1
- package/dist/knowledge/index.js.map +1 -1
- package/dist/knowledge/model.d.ts +11 -0
- package/dist/knowledge/model.d.ts.map +1 -1
- package/dist/knowledge/review.d.ts.map +1 -1
- package/dist/knowledge/review.js +25 -5
- package/dist/knowledge/review.js.map +1 -1
- package/dist/knowledge/stores/localSnapshotStore.d.ts +9 -8
- package/dist/knowledge/stores/localSnapshotStore.d.ts.map +1 -1
- package/dist/knowledge/stores/localSnapshotStore.js +29 -43
- package/dist/knowledge/stores/localSnapshotStore.js.map +1 -1
- package/dist/knowledge/stores/snapshotFormat.d.ts +32 -0
- package/dist/knowledge/stores/snapshotFormat.d.ts.map +1 -0
- package/dist/knowledge/stores/snapshotFormat.js +49 -0
- package/dist/knowledge/stores/snapshotFormat.js.map +1 -0
- package/package.json +3 -3
- package/src/contract/exports.snapshot.json +4 -0
- package/src/contract/regression.review.test.ts +121 -1
- package/src/contract/regression.store.test.ts +13 -0
- package/src/contract/types.contract.test.ts +11 -6
- package/src/knowledge/backfillNoExtract.ts +85 -0
- package/src/knowledge/extractFile.ts +9 -0
- package/src/knowledge/findingBuilder.ts +37 -0
- package/src/knowledge/findings.ts +4 -27
- package/src/knowledge/godModule.test.ts +64 -0
- package/src/knowledge/godModule.ts +46 -0
- package/src/knowledge/index.ts +3 -1
- package/src/knowledge/model.ts +11 -0
- package/src/knowledge/pack/pack.roundtrip.test.ts +1 -1
- package/src/knowledge/review.ts +20 -4
- package/src/knowledge/stores/localSnapshotStore.ts +28 -60
- package/src/knowledge/stores/snapshotFormat.ts +69 -0
|
@@ -22,6 +22,9 @@ import type {
|
|
|
22
22
|
Extracted, KnowledgeStore, NodeRef, Scope, ScopeRecord, StoredFact, StoredRelation,
|
|
23
23
|
} from '../model.js';
|
|
24
24
|
import { canonicalJson, compareStr, pathOfRef } from '../refs.js';
|
|
25
|
+
import { emptySnapshot, parse, parseLog, type KnowledgeSnapshot, type ParsedLog } from './snapshotFormat.js';
|
|
26
|
+
|
|
27
|
+
export type { KnowledgeSnapshot } from './snapshotFormat.js';
|
|
25
28
|
|
|
26
29
|
export interface SnapshotPersistence {
|
|
27
30
|
read(): Promise<string | undefined>;
|
|
@@ -42,15 +45,6 @@ export interface ExtractedLogPersistence {
|
|
|
42
45
|
replace?(text: string): Promise<void>;
|
|
43
46
|
}
|
|
44
47
|
|
|
45
|
-
export interface KnowledgeSnapshot {
|
|
46
|
-
version: 1;
|
|
47
|
-
scopes: ScopeRecord[];
|
|
48
|
-
facts: Record<string, StoredFact[]>;
|
|
49
|
-
relations: Record<string, StoredRelation[]>;
|
|
50
|
-
/** Present only when the store has no `extractedLog` (or in a snapshot written before the log existed). */
|
|
51
|
-
extracted?: Record<string, Extracted[]>;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
48
|
export interface LocalSnapshotStoreOptions {
|
|
55
49
|
/** When given, the extract cache lives in this log and the snapshot omits `extracted`. */
|
|
56
50
|
extractedLog?: ExtractedLogPersistence;
|
|
@@ -64,57 +58,6 @@ export interface LocalSnapshotStoreOptions {
|
|
|
64
58
|
|
|
65
59
|
const relKey = (r: Pick<StoredRelation, 'from' | 'to' | 'kind'>) => `${r.from}\u0000${r.to}\u0000${r.kind}`;
|
|
66
60
|
|
|
67
|
-
function asRecord<T>(v: unknown): Record<string, T[]> {
|
|
68
|
-
return v && typeof v === 'object' && !Array.isArray(v) ? v as Record<string, T[]> : {};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const emptySnapshot = (): KnowledgeSnapshot => ({ version: 1, scopes: [], facts: {}, relations: {} });
|
|
72
|
-
|
|
73
|
-
/** Reads a snapshot's text. THROWS when it does not parse, so the caller can keep the bytes aside before starting empty. */
|
|
74
|
-
function parse(text: string | undefined): KnowledgeSnapshot {
|
|
75
|
-
if (!text) return emptySnapshot();
|
|
76
|
-
const s = JSON.parse(text) as Partial<KnowledgeSnapshot>;
|
|
77
|
-
return {
|
|
78
|
-
version: 1,
|
|
79
|
-
scopes: Array.isArray(s.scopes) ? s.scopes : [],
|
|
80
|
-
facts: asRecord(s.facts),
|
|
81
|
-
relations: asRecord(s.relations),
|
|
82
|
-
...(s.extracted && typeof s.extracted === 'object' ? { extracted: s.extracted } : {}),
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
interface ParsedLog {
|
|
87
|
-
entries: Map<string, Extracted[]>;
|
|
88
|
-
/** The live (last) line of each hash, newline-terminated, in log order. */
|
|
89
|
-
liveLines: Map<string, string>;
|
|
90
|
-
liveBytes: number;
|
|
91
|
-
/** Superseded lines, torn lines and blank lines. */
|
|
92
|
-
deadBytes: number;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* The log's entries, last line per hash winning. A line that does not parse (torn by a crash or a failed
|
|
97
|
-
* append) is skipped on its own: it never costs the other lines.
|
|
98
|
-
*/
|
|
99
|
-
function parseLog(text: string | undefined): ParsedLog {
|
|
100
|
-
const out: ParsedLog = { entries: new Map(), liveLines: new Map(), liveBytes: 0, deadBytes: 0 };
|
|
101
|
-
if (!text) return out;
|
|
102
|
-
for (const line of text.split('\n')) {
|
|
103
|
-
if (!line.trim()) continue;
|
|
104
|
-
try {
|
|
105
|
-
const row = JSON.parse(line) as { h?: unknown; e?: unknown };
|
|
106
|
-
if (typeof row.h === 'string' && Array.isArray(row.e)) {
|
|
107
|
-
out.entries.set(row.h, row.e as Extracted[]);
|
|
108
|
-
out.liveLines.delete(row.h); // re-set below so the map stays in log order of last occurrence
|
|
109
|
-
out.liveLines.set(row.h, `${line}\n`);
|
|
110
|
-
}
|
|
111
|
-
} catch { /* torn line */ }
|
|
112
|
-
}
|
|
113
|
-
for (const l of out.liveLines.values()) out.liveBytes += l.length;
|
|
114
|
-
out.deadBytes = Math.max(0, text.length - out.liveBytes);
|
|
115
|
-
return out;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
61
|
type Index = Map<string, Set<string>>;
|
|
119
62
|
|
|
120
63
|
export class LocalSnapshotStore implements KnowledgeStore {
|
|
@@ -127,6 +70,8 @@ export class LocalSnapshotStore implements KnowledgeStore {
|
|
|
127
70
|
/** Per root: source path → keys of the relations touching it (`from` or `to`). */
|
|
128
71
|
private readonly relByPath = new Map<string, Index>();
|
|
129
72
|
private readonly extracted = new Map<string, Extracted[]>();
|
|
73
|
+
/** Hashes the next review re-checks (persisted with the snapshot). */
|
|
74
|
+
private readonly recheck = new Set<string>();
|
|
130
75
|
/** Extract entries not yet appended to the log. */
|
|
131
76
|
private pending = new Map<string, Extracted[]>();
|
|
132
77
|
/** The log's last line is torn (no newline), so the next append starts on a fresh line. */
|
|
@@ -173,6 +118,7 @@ export class LocalSnapshotStore implements KnowledgeStore {
|
|
|
173
118
|
}
|
|
174
119
|
const logText = this.log ? await this.log.read() : undefined;
|
|
175
120
|
this.scopeList = snap.scopes;
|
|
121
|
+
for (const h of snap.recheck ?? []) this.recheck.add(h);
|
|
176
122
|
for (const [rootId, rows] of Object.entries(snap.facts)) {
|
|
177
123
|
const m = this.factsOf(rootId);
|
|
178
124
|
if (Array.isArray(rows)) for (const f of rows) m.set(f.key, f);
|
|
@@ -387,6 +333,27 @@ export class LocalSnapshotStore implements KnowledgeStore {
|
|
|
387
333
|
return this.extracted.get(hash);
|
|
388
334
|
}
|
|
389
335
|
|
|
336
|
+
/** Asks the next review to re-check the files with these content hashes (persisted until {@link clearRecheck}). */
|
|
337
|
+
markRecheck(hashes: readonly string[]): Promise<void> {
|
|
338
|
+
// The snapshot is dirty only when the set really changed (a repeated mark costs no write).
|
|
339
|
+
return this.mutate(() => { for (const h of hashes) if (!this.recheck.has(h)) { this.recheck.add(h); this.dirty = true; } }, false);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
async hasRecheck(hash: string): Promise<boolean> {
|
|
343
|
+
await this.load();
|
|
344
|
+
return this.recheck.has(hash);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
async recheckHashes(): Promise<string[]> {
|
|
348
|
+
await this.load();
|
|
349
|
+
return [...this.recheck].sort(compareStr);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Forgets hashes a review has handled. */
|
|
353
|
+
clearRecheck(hashes: readonly string[]): Promise<void> {
|
|
354
|
+
return this.mutate(() => { for (const h of hashes) if (this.recheck.delete(h)) this.dirty = true; }, false);
|
|
355
|
+
}
|
|
356
|
+
|
|
390
357
|
/** Every cached entry by content hash (the log's, else the snapshot's `extracted`), for a host that packs the cache. */
|
|
391
358
|
async extractedEntries(): Promise<Record<string, Extracted[]>> {
|
|
392
359
|
await this.load();
|
|
@@ -414,6 +381,7 @@ export class LocalSnapshotStore implements KnowledgeStore {
|
|
|
414
381
|
facts: rows(this.factMaps, (a, b) => compareStr(a.key, b.key)),
|
|
415
382
|
relations: rows(this.relMaps, (a, b) => compareStr(relKey(a), relKey(b))),
|
|
416
383
|
...(this.log ? {} : { extracted: Object.fromEntries(this.extracted) }),
|
|
384
|
+
...(this.recheck.size > 0 ? { recheck: [...this.recheck].sort(compareStr) } : {}),
|
|
417
385
|
});
|
|
418
386
|
}
|
|
419
387
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/stores/snapshotFormat.ts — the on-disk shapes of {@link LocalSnapshotStore}: the snapshot JSON and the
|
|
3
|
+
* extract log's JSON Lines, and the pure functions that read them. No state, no persistence: the store owns both.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Extracted, ScopeRecord, StoredFact, StoredRelation } from '../model.js';
|
|
7
|
+
|
|
8
|
+
export interface KnowledgeSnapshot {
|
|
9
|
+
version: 1;
|
|
10
|
+
scopes: ScopeRecord[];
|
|
11
|
+
facts: Record<string, StoredFact[]>;
|
|
12
|
+
relations: Record<string, StoredRelation[]>;
|
|
13
|
+
/** Present only when the store has no `extractedLog` (or in a snapshot written before the log existed). */
|
|
14
|
+
extracted?: Record<string, Extracted[]>;
|
|
15
|
+
/** Content hashes a replica asked the next review to re-check (see `KnowledgeStore.markRecheck`); absent when none. */
|
|
16
|
+
recheck?: string[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function asRecord<T>(v: unknown): Record<string, T[]> {
|
|
20
|
+
return v && typeof v === 'object' && !Array.isArray(v) ? v as Record<string, T[]> : {};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const emptySnapshot = (): KnowledgeSnapshot => ({ version: 1, scopes: [], facts: {}, relations: {} });
|
|
24
|
+
|
|
25
|
+
/** Reads a snapshot's text. THROWS when it does not parse, so the caller can keep the bytes aside before starting empty. */
|
|
26
|
+
export function parse(text: string | undefined): KnowledgeSnapshot {
|
|
27
|
+
if (!text) return emptySnapshot();
|
|
28
|
+
const s = JSON.parse(text) as Partial<KnowledgeSnapshot>;
|
|
29
|
+
return {
|
|
30
|
+
version: 1,
|
|
31
|
+
scopes: Array.isArray(s.scopes) ? s.scopes : [],
|
|
32
|
+
facts: asRecord(s.facts),
|
|
33
|
+
relations: asRecord(s.relations),
|
|
34
|
+
...(s.extracted && typeof s.extracted === 'object' ? { extracted: s.extracted } : {}),
|
|
35
|
+
...(Array.isArray(s.recheck) ? { recheck: s.recheck.filter((h): h is string => typeof h === 'string') } : {}),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ParsedLog {
|
|
40
|
+
entries: Map<string, Extracted[]>;
|
|
41
|
+
/** The live (last) line of each hash, newline-terminated, in log order. */
|
|
42
|
+
liveLines: Map<string, string>;
|
|
43
|
+
liveBytes: number;
|
|
44
|
+
/** Superseded lines, torn lines and blank lines. */
|
|
45
|
+
deadBytes: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The log's entries, last line per hash winning. A line that does not parse (torn by a crash or a failed
|
|
50
|
+
* append) is skipped on its own: it never costs the other lines.
|
|
51
|
+
*/
|
|
52
|
+
export function parseLog(text: string | undefined): ParsedLog {
|
|
53
|
+
const out: ParsedLog = { entries: new Map(), liveLines: new Map(), liveBytes: 0, deadBytes: 0 };
|
|
54
|
+
if (!text) return out;
|
|
55
|
+
for (const line of text.split('\n')) {
|
|
56
|
+
if (!line.trim()) continue;
|
|
57
|
+
try {
|
|
58
|
+
const row = JSON.parse(line) as { h?: unknown; e?: unknown };
|
|
59
|
+
if (typeof row.h === 'string' && Array.isArray(row.e)) {
|
|
60
|
+
out.entries.set(row.h, row.e as Extracted[]);
|
|
61
|
+
out.liveLines.delete(row.h); // re-set below so the map stays in log order of last occurrence
|
|
62
|
+
out.liveLines.set(row.h, `${line}\n`);
|
|
63
|
+
}
|
|
64
|
+
} catch { /* torn line */ }
|
|
65
|
+
}
|
|
66
|
+
for (const l of out.liveLines.values()) out.liveBytes += l.length;
|
|
67
|
+
out.deadBytes = Math.max(0, text.length - out.liveBytes);
|
|
68
|
+
return out;
|
|
69
|
+
}
|