@seanhogg/builderforce-memory 2026.10.56 → 2026.10.58

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.
Files changed (37) hide show
  1. package/dist/knowledge/build.d.ts +2 -1
  2. package/dist/knowledge/build.d.ts.map +1 -1
  3. package/dist/knowledge/build.js +5 -2
  4. package/dist/knowledge/build.js.map +1 -1
  5. package/dist/knowledge/communities.d.ts +74 -0
  6. package/dist/knowledge/communities.d.ts.map +1 -0
  7. package/dist/knowledge/communities.js +143 -0
  8. package/dist/knowledge/communities.js.map +1 -0
  9. package/dist/knowledge/index.d.ts +3 -1
  10. package/dist/knowledge/index.d.ts.map +1 -1
  11. package/dist/knowledge/index.js +1 -0
  12. package/dist/knowledge/index.js.map +1 -1
  13. package/dist/knowledge/louvain.d.ts +38 -0
  14. package/dist/knowledge/louvain.d.ts.map +1 -0
  15. package/dist/knowledge/louvain.js +164 -0
  16. package/dist/knowledge/louvain.js.map +1 -0
  17. package/dist/knowledge/model.d.ts +17 -0
  18. package/dist/knowledge/model.d.ts.map +1 -1
  19. package/dist/knowledge/outline.d.ts.map +1 -1
  20. package/dist/knowledge/outline.js +4 -0
  21. package/dist/knowledge/outline.js.map +1 -1
  22. package/dist/knowledge/trace.d.ts.map +1 -1
  23. package/dist/knowledge/trace.js +24 -3
  24. package/dist/knowledge/trace.js.map +1 -1
  25. package/package.json +3 -3
  26. package/src/contract/exports.snapshot.json +9 -0
  27. package/src/contract/types.contract.test.ts +9 -1
  28. package/src/knowledge/build.test.ts +1 -1
  29. package/src/knowledge/build.ts +5 -2
  30. package/src/knowledge/communities.test.ts +322 -0
  31. package/src/knowledge/communities.ts +167 -0
  32. package/src/knowledge/index.ts +6 -1
  33. package/src/knowledge/louvain.ts +147 -0
  34. package/src/knowledge/model.ts +21 -1
  35. package/src/knowledge/outline.test.ts +17 -5
  36. package/src/knowledge/outline.ts +4 -0
  37. package/src/knowledge/trace.ts +24 -4
@@ -284,7 +284,13 @@ export interface KnowledgeGraph {
284
284
  findings: Finding[];
285
285
  /** Present only when the root held systems to report (see {@link GraphSystem}). */
286
286
  meta?: { systems?: GraphSystem[] };
287
- stats: { sources: number; nodes: number; edges: number; stale: number; truncated: boolean };
287
+ stats: {
288
+ sources: number; nodes: number; edges: number; stale: number; truncated: boolean;
289
+ /** Q of the file communities (PRD 38 P1), 6 decimals; absent on graphs stored before it existed. */
290
+ modularity?: number;
291
+ /** Which algorithm produced the communities (label propagation above COMMUNITY_LOUVAIN_MAX_NODES connected files). */
292
+ communityMethod?: 'louvain' | 'label-propagation';
293
+ };
288
294
  /**
289
295
  * Attached only when the graph was truncated (`stats.truncated`): the whole graph's files,
290
296
  * so an overview never depends on the cut. Consumers read it through `outlineOf`, never directly.
@@ -310,6 +316,11 @@ export interface GraphOutline {
310
316
  apps?: string[];
311
317
  /** The root's systems with their file counts, copies and graph reads — `KnowledgeGraph.meta.systems`, carried so the outline stands alone. */
312
318
  systemInfo?: GraphSystem[];
319
+ /**
320
+ * The communities the files' `communityId` badge indexes (PRD 38 P1), sorted by id. Absent when no file has one.
321
+ * Ids are per build, not stable across builds.
322
+ */
323
+ communities?: OutlineCommunity[];
313
324
  /** File nodes of the full graph, sorted by ref. */
314
325
  files: OutlineFile[];
315
326
  /**
@@ -322,6 +333,15 @@ export interface GraphOutline {
322
333
  capped: boolean;
323
334
  }
324
335
 
336
+ /** One community of the file reference graph: `id` is the `communityId` badge; `label` the members' common directory (else the top member's path). */
337
+ export interface OutlineCommunity {
338
+ id: number;
339
+ /** Unique within the table. */
340
+ label: string;
341
+ /** Members counted in THIS outline: smaller than the full community when the outline is capped (`capped`). */
342
+ size: number;
343
+ }
344
+
325
345
  export interface OutlineFile {
326
346
  /** The ref without the outline's prefix. */
327
347
  path: string;
@@ -13,9 +13,9 @@ import { edge, extractedOf, fileNode, ref, ROOT } from './testkit.test-util.js';
13
13
  const PACK: KnowledgePack = { id: 'docs', parsers: [] };
14
14
  const pad = (i: number) => `p/${String(i).padStart(6, '0')}.ts`;
15
15
 
16
- function chain(extra: Partial<KNode> = {}): BuildGraphInput {
16
+ function chain(extra: Partial<KNode> = {}, kind = 'references'): BuildGraphInput {
17
17
  const paths = Array.from({ length: 40 }, (_, i) => `d${i % 4}/f${String(i).padStart(2, '0')}.md`);
18
- const extracted = paths.map((p, i) => extractedOf(fileNode(p, extra), i + 1 < paths.length ? [edge(p, paths[i + 1]!, 'references')] : []));
18
+ const extracted = paths.map((p, i) => extractedOf(fileNode(p, extra), i + 1 < paths.length ? [edge(p, paths[i + 1]!, kind)] : []));
19
19
  return { rootId: ROOT, packs: [PACK], extracted, facts: [], relations: [] };
20
20
  }
21
21
 
@@ -77,19 +77,31 @@ describe('buildGraph outline', () => {
77
77
  expect(JSON.stringify(a.outline)).toBe(JSON.stringify(b.outline));
78
78
  });
79
79
 
80
+ // 'mentions' edges are not references, so these files have no community badges (PRD 38 P1) and the cases below are about the whitelist alone.
80
81
  it('keeps only the whitelisted badges', () => {
81
- const g = buildGraph(chain({ badges: { complexity: 7, entry: 'main', secret: 'x', added: 3, status: 'M' } }), { maxNodes: 6 });
82
+ const g = buildGraph(chain({ badges: { complexity: 7, entry: 'main', secret: 'x', added: 3, status: 'M' } }, 'mentions'), { maxNodes: 6 });
82
83
  for (const f of g.outline!.files) expect(f.badges).toEqual({ complexity: 7, entry: 'main', added: 3, status: 'M' });
83
84
  });
84
85
 
85
86
  it('keeps the externals string badge and still drops unlisted ones', () => {
86
- const g = buildGraph(chain({ badges: { externals: 'react,zod', notListed: 'x' } }), { maxNodes: 6 });
87
+ const g = buildGraph(chain({ badges: { externals: 'react,zod', notListed: 'x' } }, 'mentions'), { maxNodes: 6 });
87
88
  for (const f of g.outline!.files) expect(f.badges).toEqual({ externals: 'react,zod' });
88
89
  });
89
90
 
90
91
  it('omits badges when none of the whitelisted keys are present', () => {
91
- const g = buildGraph(chain({ badges: { other: 3 } }), { maxNodes: 6 });
92
+ const g = buildGraph(chain({ badges: { other: 3 } }, 'mentions'), { maxNodes: 6 });
92
93
  for (const f of g.outline!.files) expect(f.badges).toBeUndefined();
94
+ expect(g.outline!.communities).toBeUndefined();
95
+ });
96
+
97
+ it('carries the community badges and the communities table of referenced files', () => {
98
+ const g = buildGraph(chain(), { maxNodes: 6 });
99
+ for (const f of g.outline!.files) expect(Object.keys(f.badges ?? {}).sort()).toEqual(['communityConfidence', 'communityId']);
100
+ const table = g.outline!.communities!;
101
+ expect(table.length).toBeGreaterThan(0);
102
+ expect(table.reduce((s, c) => s + c.size, 0)).toBe(g.outline!.files.length);
103
+ expect(table.map((c) => c.id)).toEqual([...table.map((c) => c.id)].sort((a, b) => a - b));
104
+ for (const f of g.outline!.files) expect(table.some((c) => c.id === f.badges!['communityId'])).toBe(true);
93
105
  });
94
106
  });
95
107
 
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  import type { EdgeVerdict, GraphOutline, KnowledgeGraph, NodeRef, OutlineFile } from './model.js';
8
+ import { communityTable } from './communities.js';
8
9
  import { byImportance } from './importance.js';
9
10
  import { compareStr, fileRefOf } from './refs.js';
10
11
 
@@ -17,6 +18,7 @@ export const OUTLINE_MAX_LINKS = 60000;
17
18
  export const OUTLINE_BADGE_KEYS: readonly string[] = [
18
19
  'complexity', 'maxComplexity', 'coverage', 'churn', 'owner', 'patterns', 'cloneGroup', 'unused', 'unusedExports',
19
20
  'entry', 'layerRule', 'unusedDependencies', 'externals', 'added', 'removed', 'changed', 'status',
21
+ 'communityId', 'communityConfidence',
20
22
  ];
21
23
  /** Verdict codes of a link, least to worst; code 0 is "no verdict". */
22
24
  export const OUTLINE_VERDICTS: readonly ('none' | EdgeVerdict)[] = ['none', 'same', 'allowed', 'skips', 'upward'];
@@ -112,8 +114,10 @@ export function buildOutline(full: KnowledgeGraph): GraphOutline {
112
114
  }
113
115
  list.sort((a, b) => a.from - b.from || a.to - b.to);
114
116
  const links = list.map((m) => [m.from, m.to, m.verdict, m.cycle ? OUTLINE_FLAG_CYCLE : 0] as [number, number, number, number]);
117
+ const communities = communityTable(fileNodes, prefix);
115
118
  return {
116
119
  prefix, areas, partitions,
120
+ ...(communities.length > 0 ? { communities } : {}),
117
121
  ...(systems.length > 0 ? { systems } : {}),
118
122
  ...(apps.length > 0 ? { apps } : {}),
119
123
  ...(full.meta?.systems ? { systemInfo: full.meta.systems } : {}),
@@ -7,7 +7,8 @@
7
7
  * touches — code layers or doc folders — and `toMermaid` draws them as a sequence diagram.
8
8
  */
9
9
 
10
- import type { KEdge, KnowledgeGraph, NodeRef, PartitionRegistry, Trace, TraceStep } from './model.js';
10
+ import { placementOf } from './communities.js';
11
+ import type { KEdge, KNode, KnowledgeGraph, NodeRef, PartitionRegistry, Trace, TraceStep } from './model.js';
11
12
  import { partitionOrder } from './partitions.js';
12
13
  import { compareStr } from './refs.js';
13
14
 
@@ -30,11 +31,24 @@ export interface TraceOptions {
30
31
  registry?: PartitionRegistry;
31
32
  }
32
33
 
34
+ /** The lane of a community: its name, and the id the lanes are sorted by (numerically, not as text). */
35
+ const communityLane = (id: number): string => `community ${id}`;
36
+
37
+ function placementLane(n: KNode, communities: Map<string, number>): string | undefined {
38
+ const p = placementOf(n);
39
+ if (p === undefined) return undefined;
40
+ if (p.kind === 'partition') return p.id;
41
+ communities.set(communityLane(p.id), p.id);
42
+ return communityLane(p.id);
43
+ }
44
+
33
45
  export function traceGraph(g: KnowledgeGraph, from: NodeRef, opts: TraceOptions = {}): LanedTrace {
34
46
  const kinds = new Set(opts.edgeKinds ?? DEFAULT_TRACE_EDGE_KINDS);
35
47
  const maxDepth = opts.maxDepth ?? DEFAULT_TRACE_DEPTH;
36
48
  const maxSteps = opts.maxSteps ?? DEFAULT_TRACE_STEPS;
37
- const partition = new Map(g.nodes.map((n) => [n.ref, n.partition] as const));
49
+ // A node's lane is its partition, else its community (PRD 38 P1), else none.
50
+ const communityIds = new Map<string, number>();
51
+ const partition = new Map(g.nodes.map((n) => [n.ref, placementLane(n, communityIds)] as const));
38
52
  const out = new Map<NodeRef, KEdge[]>();
39
53
  for (const e of g.edges) {
40
54
  if (!kinds.has(e.kind)) continue;
@@ -77,8 +91,14 @@ export function traceGraph(g: KnowledgeGraph, from: NodeRef, opts: TraceOptions
77
91
  }
78
92
  if (opts.registry) {
79
93
  const reg = opts.registry;
80
- const order = (l: string) => partitionOrder(l, reg) ?? Number.MAX_SAFE_INTEGER;
81
- lanes.sort((a, b) => order(a) - order(b) || compareStr(a, b));
94
+ // Partitions in the registry's order, then communities by numeric id, then everything else by name.
95
+ const key = (l: string): [number, number] => {
96
+ const order = partitionOrder(l, reg);
97
+ if (order !== undefined) return [0, order];
98
+ const community = communityIds.get(l);
99
+ return community !== undefined ? [1, community] : [2, 0];
100
+ };
101
+ lanes.sort((a, b) => key(a)[0] - key(b)[0] || key(a)[1] - key(b)[1] || compareStr(a, b));
82
102
  }
83
103
  return { from, lanes, steps, truncated, laneOf };
84
104
  }