@seanhogg/builderforce-memory 2026.10.39 → 2026.10.41
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/ingest/docsExtract.d.ts +10 -0
- package/dist/ingest/docsExtract.d.ts.map +1 -1
- package/dist/ingest/docsExtract.js +48 -4
- package/dist/ingest/docsExtract.js.map +1 -1
- package/dist/ingest/fences.d.ts +15 -0
- package/dist/ingest/fences.d.ts.map +1 -0
- package/dist/ingest/fences.js +46 -0
- package/dist/ingest/fences.js.map +1 -0
- package/dist/ingest/textParsers.d.ts.map +1 -1
- package/dist/ingest/textParsers.js +8 -7
- package/dist/ingest/textParsers.js.map +1 -1
- package/dist/knowledge/build.d.ts.map +1 -1
- package/dist/knowledge/build.js +3 -0
- package/dist/knowledge/build.js.map +1 -1
- package/dist/knowledge/index.d.ts +2 -1
- package/dist/knowledge/index.d.ts.map +1 -1
- package/dist/knowledge/index.js +1 -0
- package/dist/knowledge/index.js.map +1 -1
- package/dist/knowledge/model.d.ts +41 -0
- package/dist/knowledge/model.d.ts.map +1 -1
- package/dist/knowledge/outline.d.ts +23 -0
- package/dist/knowledge/outline.d.ts.map +1 -0
- package/dist/knowledge/outline.js +103 -0
- package/dist/knowledge/outline.js.map +1 -0
- package/dist/knowledge/sources.d.ts +1 -1
- package/dist/knowledge/sources.d.ts.map +1 -1
- package/dist/knowledge/sources.js +6 -2
- package/dist/knowledge/sources.js.map +1 -1
- package/package.json +3 -3
- package/src/ingest/docsExtract.ts +43 -4
- package/src/ingest/fences.ts +45 -0
- package/src/ingest/textParsers.ts +8 -6
- package/src/knowledge/build.ts +3 -0
- package/src/knowledge/index.ts +4 -1
- package/src/knowledge/model.ts +43 -0
- package/src/knowledge/outline.test.ts +163 -0
- package/src/knowledge/outline.ts +108 -0
- package/src/knowledge/scripts.test.ts +125 -0
- package/src/knowledge/sources.ts +7 -2
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* knowledge/outline.ts — the whole graph's files and file-to-file references, kept when the
|
|
3
|
+
* graph is truncated so an overview (stations, layers, lines) never depends on which nodes
|
|
4
|
+
* the cut kept. Built from the FULL graph; everything is sorted, so equal input is byte-identical.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { EdgeVerdict, GraphOutline, KnowledgeGraph, NodeRef, OutlineFile } from './model.js';
|
|
8
|
+
import { byImportance } from './importance.js';
|
|
9
|
+
import { compareStr, fileRefOf } from './refs.js';
|
|
10
|
+
|
|
11
|
+
export const OUTLINE_MAX_FILES = 20000;
|
|
12
|
+
export const OUTLINE_MAX_LINKS = 60000;
|
|
13
|
+
/**
|
|
14
|
+
* The only badges an outline file carries. Mirrors agent-tools' CODE_BADGES plus the live-overlay
|
|
15
|
+
* badges (added/removed/status); memory cannot import agent-tools, so the list is kept here.
|
|
16
|
+
*/
|
|
17
|
+
export const OUTLINE_BADGE_KEYS: readonly string[] = [
|
|
18
|
+
'complexity', 'maxComplexity', 'coverage', 'churn', 'owner', 'patterns', 'cloneGroup', 'unused', 'unusedExports',
|
|
19
|
+
'entry', 'layerRule', 'unusedDependencies', 'added', 'removed', 'changed', 'status',
|
|
20
|
+
];
|
|
21
|
+
/** Verdict codes of a link, least to worst; code 0 is "no verdict". */
|
|
22
|
+
export const OUTLINE_VERDICTS: readonly ('none' | EdgeVerdict)[] = ['none', 'same', 'allowed', 'skips', 'upward'];
|
|
23
|
+
/** Bit 1 of a link's flags. */
|
|
24
|
+
export const OUTLINE_FLAG_CYCLE = 1;
|
|
25
|
+
const HASH_CHARS = 12;
|
|
26
|
+
|
|
27
|
+
/** The ref of an outline file. */
|
|
28
|
+
export function outlineRef(o: Pick<GraphOutline, 'prefix'>, f: Pick<OutlineFile, 'path'>): NodeRef {
|
|
29
|
+
return (o.prefix + f.path) as NodeRef;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The ONE entry point for consumers: the attached outline of a truncated graph, else one built from the graph itself. */
|
|
33
|
+
export function outlineOf(g: KnowledgeGraph): GraphOutline {
|
|
34
|
+
return g.outline ?? buildOutline(g);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function commonPrefix(refs: readonly string[]): string {
|
|
38
|
+
const first = refs[0];
|
|
39
|
+
if (first === undefined) return '';
|
|
40
|
+
const sep = first.indexOf('::');
|
|
41
|
+
if (sep < 0) return '';
|
|
42
|
+
const prefix = first.slice(0, sep + 2);
|
|
43
|
+
return refs.every((r) => r.startsWith(prefix)) ? prefix : '';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface Merged { from: number; to: number; verdict: number; cycle: boolean; weight: number }
|
|
47
|
+
|
|
48
|
+
export function buildOutline(full: KnowledgeGraph): GraphOutline {
|
|
49
|
+
let capped = false;
|
|
50
|
+
let fileNodes = full.nodes.filter((n) => n.kind === 'file');
|
|
51
|
+
if (fileNodes.length > OUTLINE_MAX_FILES) {
|
|
52
|
+
fileNodes = [...fileNodes].sort(byImportance).slice(0, OUTLINE_MAX_FILES);
|
|
53
|
+
capped = true;
|
|
54
|
+
}
|
|
55
|
+
fileNodes = [...fileNodes].sort((a, b) => compareStr(a.ref, b.ref));
|
|
56
|
+
|
|
57
|
+
const prefix = commonPrefix(fileNodes.map((n) => n.ref));
|
|
58
|
+
const areas = [...new Set(fileNodes.flatMap((n) => (n.area !== undefined ? [n.area] : [])))].sort(compareStr);
|
|
59
|
+
const partitions = [...new Set(fileNodes.flatMap((n) => (n.partition !== undefined ? [n.partition] : [])))].sort(compareStr);
|
|
60
|
+
const areaIndex = new Map<NodeRef, number>(areas.map((a, i) => [a, i] as const));
|
|
61
|
+
const partitionIndex = new Map<string, number>(partitions.map((p, i) => [p, i] as const));
|
|
62
|
+
|
|
63
|
+
const files: OutlineFile[] = fileNodes.map((n) => {
|
|
64
|
+
const badges: Record<string, number | string> = {};
|
|
65
|
+
for (const k of OUTLINE_BADGE_KEYS) {
|
|
66
|
+
const v = n.badges?.[k];
|
|
67
|
+
if (v !== undefined) badges[k] = v;
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
path: n.ref.slice(prefix.length),
|
|
71
|
+
...(n.area !== undefined ? { area: areaIndex.get(n.area)! } : {}),
|
|
72
|
+
...(n.partition !== undefined ? { partition: partitionIndex.get(n.partition)! } : {}),
|
|
73
|
+
importance: n.importance,
|
|
74
|
+
...(n.sourceHash !== undefined ? { hash: n.sourceHash.slice(0, HASH_CHARS) } : {}),
|
|
75
|
+
...(Object.keys(badges).length > 0 ? { badges } : {}),
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const index = new Map<NodeRef, number>(fileNodes.map((n, i) => [n.ref, i] as const));
|
|
80
|
+
const merged = new Map<string, Merged>();
|
|
81
|
+
for (const e of full.edges) {
|
|
82
|
+
if (e.kind !== 'references') continue;
|
|
83
|
+
const from = index.get(fileRefOf(e.from));
|
|
84
|
+
const to = index.get(fileRefOf(e.to));
|
|
85
|
+
if (from === undefined || to === undefined || from === to) continue;
|
|
86
|
+
const key = `${from}>${to}`;
|
|
87
|
+
const verdict = e.verdict === undefined ? 0 : OUTLINE_VERDICTS.indexOf(e.verdict);
|
|
88
|
+
const hit = merged.get(key);
|
|
89
|
+
if (hit) {
|
|
90
|
+
hit.verdict = Math.max(hit.verdict, verdict);
|
|
91
|
+
hit.cycle ||= e.cycle === true;
|
|
92
|
+
hit.weight += e.weight ?? 0;
|
|
93
|
+
} else {
|
|
94
|
+
merged.set(key, { from, to, verdict, cycle: e.cycle === true, weight: e.weight ?? 0 });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
let list = [...merged.values()];
|
|
98
|
+
if (list.length > OUTLINE_MAX_LINKS) {
|
|
99
|
+
const imp = (m: Merged) => fileNodes[m.from]!.importance + fileNodes[m.to]!.importance;
|
|
100
|
+
list = list
|
|
101
|
+
.sort((a, b) => b.weight - a.weight || imp(b) - imp(a) || a.from - b.from || a.to - b.to)
|
|
102
|
+
.slice(0, OUTLINE_MAX_LINKS);
|
|
103
|
+
capped = true;
|
|
104
|
+
}
|
|
105
|
+
list.sort((a, b) => a.from - b.from || a.to - b.to);
|
|
106
|
+
const links = list.map((m) => [m.from, m.to, m.verdict, m.cycle ? OUTLINE_FLAG_CYCLE : 0] as [number, number, number, number]);
|
|
107
|
+
return { prefix, areas, partitions, files, links, capped };
|
|
108
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* src/knowledge/scripts.test.ts
|
|
3
|
+
* Shell scripts are never documents; `[[Name]]` and links inside markdown code are not edges.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { InMemoryIngestManifest } from '../ingest/IngestionPipeline.js';
|
|
7
|
+
import { markdownParser, plainTextParser } from '../ingest/parsers.js';
|
|
8
|
+
import type { SourceDocument } from '../ingest/types.js';
|
|
9
|
+
import type { ExtractContext, KnowledgePack } from './model.js';
|
|
10
|
+
import { DOCS_PACK } from './packs/docs.js';
|
|
11
|
+
import { planReview } from './plan.js';
|
|
12
|
+
import { asPathSource, resolveRelativePath } from './sources.js';
|
|
13
|
+
import { ROOT } from './testkit.test-util.js';
|
|
14
|
+
|
|
15
|
+
const ctx: ExtractContext = { rootId: ROOT, knownKeys: new Map(), resolvePath: resolveRelativePath };
|
|
16
|
+
|
|
17
|
+
function srcOf(path: string, text: string): SourceDocument {
|
|
18
|
+
return asPathSource({ id: path, tenantId: 't', content: { kind: 'text', text } }, path, 'h');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function linksOf(text: string, parser = markdownParser, path = 'a.md') {
|
|
22
|
+
const src = srcOf(path, text);
|
|
23
|
+
return parser.extract!(src, parser.parse(src), ctx).edges.filter((e) => e.kind === 'links');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function listing(files: Record<string, string>) {
|
|
27
|
+
return {
|
|
28
|
+
rootId: ROOT, title: 't',
|
|
29
|
+
async *list() { for (const [path, text] of Object.entries(files)) yield { path, hash: `h:${path}`, size: text.length }; },
|
|
30
|
+
async read(path: string): Promise<SourceDocument> {
|
|
31
|
+
return { id: path, tenantId: 't', content: { kind: 'text', text: files[path]! } };
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const SHELL = 'if [[ -f x ]]; then :; fi';
|
|
37
|
+
|
|
38
|
+
describe('scripts are not documents', () => {
|
|
39
|
+
it.each(['run.sh', 'run.bash', 'run.zsh', 'run.ps1', 'run.bat', 'run.cmd', 'RUN.SH'])('%s is not claimed by the docs pack', async (name) => {
|
|
40
|
+
const plan = await planReview(listing({ [name]: SHELL, 'a.md': '# A\n' }), new InMemoryIngestManifest(), [DOCS_PACK]);
|
|
41
|
+
expect(plan.skipped).toContainEqual({ path: name, reason: 'no-pack' });
|
|
42
|
+
expect(plan.files).toBe(1);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('a host parser can still claim a .sh file', async () => {
|
|
46
|
+
const hostPack: KnowledgePack = {
|
|
47
|
+
id: 'host', findings: [],
|
|
48
|
+
parsers: [{ id: 'sh', accepts: (s) => s.id.endsWith('.sh'), parse: () => [], extract: () => ({ sourceRef: 'x', sourceHash: 'h', nodes: [], edges: [], keys: [] }) } as never],
|
|
49
|
+
} as never;
|
|
50
|
+
const plan = await planReview(listing({ 'run.sh': SHELL }), new InMemoryIngestManifest(), [DOCS_PACK, hostPack]);
|
|
51
|
+
expect(plan.files).toBe(1);
|
|
52
|
+
expect(plan.byPack['host']).toBe(1);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('wiki and markdown links ignore code', () => {
|
|
57
|
+
it('prose links still resolve', () => {
|
|
58
|
+
expect(linksOf('See [[notes]].')).toHaveLength(1);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('fenced, tilde-fenced, tab- and space-indented, and inline code make no link', () => {
|
|
62
|
+
expect(linksOf('# T\n```bash\nif [[ -f x ]]; then\n```\n')).toHaveLength(0);
|
|
63
|
+
expect(linksOf('# T\n~~~\n[[x]]\n~~~\n')).toHaveLength(0);
|
|
64
|
+
expect(linksOf('# T\n\n [[x]]\n')).toHaveLength(0);
|
|
65
|
+
expect(linksOf('# T\n\n\t[[x]]\n')).toHaveLength(0);
|
|
66
|
+
expect(linksOf('# T\nuse `[[x]]` here\n')).toHaveLength(0);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('a double-backtick span and a span over a soft line break are code', () => {
|
|
70
|
+
expect(linksOf('# T\nuse ``a ` [[x]]`` here\n')).toHaveLength(0);
|
|
71
|
+
expect(linksOf('# T\nuse `a\n[[x]]` here\n')).toHaveLength(0);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('handles CRLF input', () => {
|
|
75
|
+
expect(linksOf('# T\r\n```\r\n[[x]]\r\n```\r\nand [[y]]\r\n')).toHaveLength(1);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('a closed fence ends: later prose [[y]] is a link', () => {
|
|
79
|
+
const links = linksOf('# T\n```\n[[x]]\n```\nafter [[y]]\n');
|
|
80
|
+
expect(links).toHaveLength(1);
|
|
81
|
+
expect(links[0]!.to).toContain('y');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('a four-backtick fence holding ``` stays open; ``` inside ~~~ too', () => {
|
|
85
|
+
expect(linksOf('# T\n````\n```\n[[x]]\n```\n````\n')).toHaveLength(0);
|
|
86
|
+
expect(linksOf('# T\n~~~\n```\n[[x]]\n```\n~~~\n')).toHaveLength(0);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('an unclosed fence runs to the end', () => {
|
|
90
|
+
expect(linksOf('# T\n```\n[[x]]\n# not a heading\n[[z]]\n')).toHaveLength(0);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('a ~~~ block holding a # line is one section and leaks no link', () => {
|
|
94
|
+
const text = '# T\n~~~bash\n# check\nif [[ -f x ]]; then\n~~~\n';
|
|
95
|
+
expect(markdownParser.parse(srcOf('a.md', text))).toHaveLength(1);
|
|
96
|
+
expect(linksOf(text)).toHaveLength(0);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('a fence inside a list item and a blockquote fence are fences', () => {
|
|
100
|
+
expect(linksOf('# T\n- item\n\n ```\n [[x]]\n ```\n')).toHaveLength(0);
|
|
101
|
+
expect(linksOf('# T\n> ```\n> [[x]]\n> ```\n')).toHaveLength(0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('a markdown link inside a fence is not an edge', () => {
|
|
105
|
+
expect(linksOf('# T\n```\n[g](guide.md)\n```\n')).toHaveLength(0);
|
|
106
|
+
expect(linksOf('# T\n[g](guide.md)\n')).toHaveLength(1);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('a loose-list continuation keeps its link', () => {
|
|
110
|
+
expect(linksOf('# T\n- item\n\n continues with [[y]]\n')).toHaveLength(1);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('plain text keeps an indented paragraph link', () => {
|
|
114
|
+
expect(linksOf('# T\n\n see [[y]]\n', plainTextParser, 'a.txt')).toHaveLength(1);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('markdown section text', () => {
|
|
119
|
+
it('keeps a section first-line indentation; a column-0 section is unchanged', () => {
|
|
120
|
+
const indented = markdownParser.parse(srcOf('a.md', '# T\n\n code\n'));
|
|
121
|
+
expect(indented[0]!.text).toBe(' code');
|
|
122
|
+
const plain = markdownParser.parse(srcOf('a.md', '# T\n\nhello\nworld\n\n'));
|
|
123
|
+
expect(plain[0]!.text).toBe('hello\nworld');
|
|
124
|
+
});
|
|
125
|
+
});
|
package/src/knowledge/sources.ts
CHANGED
|
@@ -20,7 +20,7 @@ export const DEFAULT_MAX_FILE_BYTES = 2 * 1024 * 1024;
|
|
|
20
20
|
* re-extraction (judged sections with unchanged text are re-anchored, not judged again; see
|
|
21
21
|
* owedFiles.ts for how an unfinished one is carried to the next review).
|
|
22
22
|
*/
|
|
23
|
-
export const EXTRACTOR_VERSION =
|
|
23
|
+
export const EXTRACTOR_VERSION = 2;
|
|
24
24
|
/** Always ignored: VCS internals, installed dependencies, builderforce config. */
|
|
25
25
|
const DEFAULT_IGNORES = ['.git/', 'node_modules/', '.builderforce/', '.gitignore'];
|
|
26
26
|
|
|
@@ -52,6 +52,9 @@ const BINARY_EXT = new Set([
|
|
|
52
52
|
'ttf', 'otf', 'woff', 'woff2', 'eot', 'sqlite', 'db', 'vsix', 'lock',
|
|
53
53
|
]);
|
|
54
54
|
|
|
55
|
+
/** Shell and batch scripts are code, never documents (their `[[ … ]]` tests are not links). */
|
|
56
|
+
const SCRIPT_EXT = new Set(['sh', 'bash', 'zsh', 'ps1', 'bat', 'cmd']);
|
|
57
|
+
|
|
55
58
|
const extOf = (path: string): string => {
|
|
56
59
|
const m = /\.([A-Za-z0-9]+)$/.exec(path);
|
|
57
60
|
return m ? m[1]!.toLowerCase() : '';
|
|
@@ -113,7 +116,9 @@ const BUILTIN_TEXT_PARSERS: ReadonlySet<KnowledgeParser> = new Set<KnowledgePars
|
|
|
113
116
|
* keeps the builtins as they are.
|
|
114
117
|
*/
|
|
115
118
|
const guardBuiltin = (p: KnowledgeParser): KnowledgeParser =>
|
|
116
|
-
BUILTIN_TEXT_PARSERS.has(p)
|
|
119
|
+
BUILTIN_TEXT_PARSERS.has(p)
|
|
120
|
+
? { ...p, accepts: (s) => !SCRIPT_EXT.has(extOf(s.id)) && !isBinaryDocumentType(mediaTypeOf(s)) && p.accepts(s) }
|
|
121
|
+
: p;
|
|
117
122
|
|
|
118
123
|
export function composePacks(packs: readonly KnowledgePack[]): ComposedPacks {
|
|
119
124
|
const owner = new Map<KnowledgeParser, KnowledgePack>();
|