@unerr-ai/unerr 0.2.1 → 0.2.2
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/README.md +6 -0
- package/dist/cli.js +37236 -35793
- package/package.json +1 -1
- package/dist/behaviors/agent-llm-bridge.js +0 -166
- package/dist/behaviors/architecture-guard.js +0 -256
- package/dist/behaviors/auto-doc.js +0 -247
- package/dist/behaviors/cascade-guard.js +0 -289
- package/dist/behaviors/change-narrative.js +0 -270
- package/dist/behaviors/convention-drift.js +0 -290
- package/dist/behaviors/framework.js +0 -235
- package/dist/behaviors/guard-formatter.js +0 -44
- package/dist/behaviors/incomplete-work.js +0 -270
- package/dist/behaviors/loop-breaker.js +0 -300
- package/dist/behaviors/session-continuity.js +0 -208
- package/dist/commands/branches.js +0 -97
- package/dist/commands/check-commit.js +0 -225
- package/dist/commands/compress-output.js +0 -64
- package/dist/commands/config-verify.js +0 -243
- package/dist/commands/daemon.js +0 -905
- package/dist/commands/dashboard.js +0 -52
- package/dist/commands/debug.js +0 -200
- package/dist/commands/enrich.js +0 -184
- package/dist/commands/exec.js +0 -233
- package/dist/commands/gain.js +0 -156
- package/dist/commands/hook.js +0 -88
- package/dist/commands/index.js +0 -88
- package/dist/commands/init.js +0 -74
- package/dist/commands/install.js +0 -505
- package/dist/commands/learn.js +0 -116
- package/dist/commands/manifest.js +0 -193
- package/dist/commands/rewind.js +0 -103
- package/dist/commands/serve.js +0 -19
- package/dist/commands/setup-wizard.js +0 -414
- package/dist/commands/skills.js +0 -64
- package/dist/commands/stats.js +0 -20
- package/dist/commands/status.js +0 -654
- package/dist/commands/timeline.js +0 -139
- package/dist/commands/uninstall.js +0 -230
- package/dist/components/App.js +0 -109
- package/dist/components/Banner.js +0 -12
- package/dist/components/ConfirmPrompt.js +0 -25
- package/dist/components/DriftSummary.js +0 -23
- package/dist/components/GradeBadge.js +0 -15
- package/dist/components/HealthCard.js +0 -18
- package/dist/components/InkSpinner.js +0 -22
- package/dist/components/InputBox.js +0 -17
- package/dist/components/KeyValue.js +0 -13
- package/dist/components/MessageList.js +0 -14
- package/dist/components/ProgressBar.js +0 -26
- package/dist/components/Section.js +0 -16
- package/dist/components/SessionSummaryCard.js +0 -73
- package/dist/components/StartupDisplay.js +0 -24
- package/dist/components/StatusDashboard.js +0 -57
- package/dist/components/StatusLine.js +0 -8
- package/dist/components/StepLine.js +0 -22
- package/dist/components/Theme.js +0 -20
- package/dist/components/ToolProgress.js +0 -8
- package/dist/components/ViolationList.js +0 -21
- package/dist/components/render.js +0 -13
- package/dist/config/agent-registry.js +0 -237
- package/dist/config/claude-settings-hooks.js +0 -304
- package/dist/config/hook-installer.js +0 -65
- package/dist/config/instruction-writer.js +0 -388
- package/dist/config/mcp-config-writer.js +0 -266
- package/dist/config/settings.js +0 -174
- package/dist/config/tool-detector.js +0 -42
- package/dist/config/value-surfacing.js +0 -119
- package/dist/core/context-assembly.js +0 -108
- package/dist/core/conversation.js +0 -33
- package/dist/core/local-chat-provider.js +0 -475
- package/dist/core/provider-factory.js +0 -55
- package/dist/core/providers.js +0 -90
- package/dist/core/query-engine.js +0 -174
- package/dist/daemon/api.js +0 -312
- package/dist/daemon/autostart.js +0 -119
- package/dist/daemon/bootstrap.js +0 -39
- package/dist/daemon/client.js +0 -164
- package/dist/daemon/detect-ci.js +0 -81
- package/dist/daemon/platform-linux.js +0 -146
- package/dist/daemon/platform-macos.js +0 -134
- package/dist/daemon/platform-windows.js +0 -116
- package/dist/daemon/process-manager.js +0 -299
- package/dist/daemon/protocol.js +0 -23
- package/dist/daemon/registry.js +0 -270
- package/dist/daemon/settings-schema.js +0 -72
- package/dist/daemon/system-health.js +0 -134
- package/dist/daemon/version-checker.js +0 -262
- package/dist/daemon/warm-start.js +0 -223
- package/dist/entrypoints/cli.js +0 -1043
- package/dist/entrypoints/daemon.js +0 -380
- package/dist/entrypoints/repl.js +0 -147
- package/dist/hooks/adapters/claude-code.js +0 -90
- package/dist/hooks/adapters/cline.js +0 -100
- package/dist/hooks/adapters/cursor.js +0 -98
- package/dist/hooks/hook-dedup.js +0 -79
- package/dist/hooks/hook-runner.js +0 -113
- package/dist/hooks/navigation-hooks.js +0 -175
- package/dist/hooks/prompt-hooks.js +0 -63
- package/dist/hooks/shell-hooks.js +0 -47
- package/dist/ignore.js +0 -111
- package/dist/intelligence/approach-suggester.js +0 -61
- package/dist/intelligence/ast-extractor.js +0 -2615
- package/dist/intelligence/ast-worker.js +0 -34
- package/dist/intelligence/background-indexer.js +0 -121
- package/dist/intelligence/blast-radius.js +0 -200
- package/dist/intelligence/community-detection.js +0 -691
- package/dist/intelligence/community-detector.js +0 -184
- package/dist/intelligence/computation-scheduler.js +0 -75
- package/dist/intelligence/confidence-propagation.js +0 -47
- package/dist/intelligence/convention-detector.js +0 -242
- package/dist/intelligence/convention-learner.js +0 -205
- package/dist/intelligence/convention-matcher.js +0 -205
- package/dist/intelligence/cozo-schema.js +0 -376
- package/dist/intelligence/decision-point-detector.js +0 -90
- package/dist/intelligence/deep-dive-tools.js +0 -586
- package/dist/intelligence/durability-scorer.js +0 -84
- package/dist/intelligence/exploration-cost.js +0 -204
- package/dist/intelligence/exploration-pattern-tracker.js +0 -61
- package/dist/intelligence/fact-generator.js +0 -322
- package/dist/intelligence/facts-schema.js +0 -90
- package/dist/intelligence/file-intelligence.js +0 -59
- package/dist/intelligence/graph-holder.js +0 -220
- package/dist/intelligence/graph-temporal-joiner.js +0 -238
- package/dist/intelligence/health-grade.js +0 -423
- package/dist/intelligence/health-grader.js +0 -200
- package/dist/intelligence/health-map-data.js +0 -259
- package/dist/intelligence/import-symbols.js +0 -136
- package/dist/intelligence/incremental-indexer.js +0 -658
- package/dist/intelligence/indexer/centrality.js +0 -62
- package/dist/intelligence/indexer/cfg-context.js +0 -95
- package/dist/intelligence/indexer/confidence.js +0 -34
- package/dist/intelligence/indexer/cross-file-resolver.js +0 -104
- package/dist/intelligence/indexer/edge-repair.js +0 -89
- package/dist/intelligence/indexer/entity-key.js +0 -17
- package/dist/intelligence/indexer/export-map.js +0 -132
- package/dist/intelligence/indexer/git-cochange.js +0 -128
- package/dist/intelligence/indexer/graph-patch.js +0 -147
- package/dist/intelligence/indexer/incremental.js +0 -78
- package/dist/intelligence/indexer/ingest.js +0 -160
- package/dist/intelligence/indexer/language-detect.js +0 -226
- package/dist/intelligence/indexer/metadata.js +0 -63
- package/dist/intelligence/indexer/mutation-tracker.js +0 -79
- package/dist/intelligence/indexer/orchestrator.js +0 -155
- package/dist/intelligence/indexer/plugin-interface.js +0 -31
- package/dist/intelligence/indexer/plugins/csharp.js +0 -440
- package/dist/intelligence/indexer/plugins/go.js +0 -335
- package/dist/intelligence/indexer/plugins/java.js +0 -370
- package/dist/intelligence/indexer/plugins/python.js +0 -358
- package/dist/intelligence/indexer/plugins/regex-fallback.js +0 -82
- package/dist/intelligence/indexer/plugins/ruby.js +0 -290
- package/dist/intelligence/indexer/plugins/rust.js +0 -484
- package/dist/intelligence/indexer/plugins/tier2-generic.js +0 -310
- package/dist/intelligence/indexer/plugins/typescript.js +0 -456
- package/dist/intelligence/indexer/resource-monitor.js +0 -93
- package/dist/intelligence/indexer/scip/decoder.js +0 -253
- package/dist/intelligence/indexer/scip/detector.js +0 -232
- package/dist/intelligence/indexer/scip/downloader.js +0 -427
- package/dist/intelligence/indexer/scip/fallback.js +0 -34
- package/dist/intelligence/indexer/scip/merger.js +0 -109
- package/dist/intelligence/indexer/scip/orchestrator.js +0 -433
- package/dist/intelligence/indexer/scip/runner.js +0 -98
- package/dist/intelligence/indexer/snapshot.js +0 -66
- package/dist/intelligence/indexer/test-detector.js +0 -196
- package/dist/intelligence/indexer/watch-integration.js +0 -61
- package/dist/intelligence/indexer/worker.js +0 -85
- package/dist/intelligence/local-convention-detector.js +0 -437
- package/dist/intelligence/local-embeddings.js +0 -190
- package/dist/intelligence/local-graph.js +0 -1946
- package/dist/intelligence/local-indexer.js +0 -1575
- package/dist/intelligence/local-llm.js +0 -163
- package/dist/intelligence/local-rule-generator.js +0 -154
- package/dist/intelligence/local-snapshot.js +0 -213
- package/dist/intelligence/negative-knowledge.js +0 -103
- package/dist/intelligence/persistent-db.js +0 -85
- package/dist/intelligence/query-router.js +0 -2556
- package/dist/intelligence/risk-classifier.js +0 -116
- package/dist/intelligence/rule-evaluator.js +0 -380
- package/dist/intelligence/rule-generator.js +0 -49
- package/dist/intelligence/search-index.js +0 -173
- package/dist/intelligence/semantic/docstring-extractor.js +0 -67
- package/dist/intelligence/semantic/embedding-store.js +0 -52
- package/dist/intelligence/semantic/enrichment-orchestrator.js +0 -48
- package/dist/intelligence/semantic/git-message-miner.js +0 -114
- package/dist/intelligence/semantic/identifier-tokenizer.js +0 -51
- package/dist/intelligence/semantic/node2vec-embeddings.js +0 -71
- package/dist/intelligence/semantic/node2vec-walks.js +0 -103
- package/dist/intelligence/semantic/path-domain-inference.js +0 -112
- package/dist/intelligence/semantic/similarity-engine.js +0 -60
- package/dist/intelligence/semantic/tfidf-vectors.js +0 -88
- package/dist/intelligence/session-brief-builder.js +0 -159
- package/dist/intelligence/session-context.js +0 -221
- package/dist/intelligence/session-health-monitor.js +0 -211
- package/dist/intelligence/session-narrative.js +0 -197
- package/dist/intelligence/session-pattern-analyzer.js +0 -218
- package/dist/intelligence/signal-scorer.js +0 -390
- package/dist/intelligence/signal-show-store.js +0 -182
- package/dist/intelligence/smart-truncate.js +0 -158
- package/dist/intelligence/subgraph-cache.js +0 -88
- package/dist/intelligence/temporal-facts.js +0 -494
- package/dist/intelligence/token-estimator.js +0 -100
- package/dist/intelligence/tool-injector.js +0 -87
- package/dist/intelligence/tree-sitter-loader.js +0 -71
- package/dist/intelligence/worker-pool.js +0 -116
- package/dist/proxy/arg-validator.js +0 -79
- package/dist/proxy/auto-bootstrap.js +0 -167
- package/dist/proxy/bridge.js +0 -147
- package/dist/proxy/budget-enforcer.js +0 -70
- package/dist/proxy/compression-quality-monitor.js +0 -160
- package/dist/proxy/compression-stats.js +0 -51
- package/dist/proxy/context-rot-detector.js +0 -137
- package/dist/proxy/drift-detector.js +0 -139
- package/dist/proxy/efficiency-tracker.js +0 -79
- package/dist/proxy/fact-ranking.js +0 -154
- package/dist/proxy/format-encoder.js +0 -266
- package/dist/proxy/http-transport.js +0 -90
- package/dist/proxy/lifecycle-actor.js +0 -55
- package/dist/proxy/lifecycle-machine.js +0 -187
- package/dist/proxy/log-tailer.js +0 -265
- package/dist/proxy/model-pricing.js +0 -98
- package/dist/proxy/network-firewall.js +0 -141
- package/dist/proxy/nudge-state.js +0 -93
- package/dist/proxy/output-compressor.js +0 -185
- package/dist/proxy/pid-lock.js +0 -291
- package/dist/proxy/proxy-context.js +0 -11
- package/dist/proxy/proxy.js +0 -2633
- package/dist/proxy/response-enrichment.js +0 -32
- package/dist/proxy/response-envelope.js +0 -313
- package/dist/proxy/session-dedup.js +0 -82
- package/dist/proxy/session-legend.js +0 -30
- package/dist/proxy/session-persistence.js +0 -210
- package/dist/proxy/session-resume.js +0 -94
- package/dist/proxy/session-stats.js +0 -513
- package/dist/proxy/shell-classifier.js +0 -1346
- package/dist/proxy/shell-compression-log.js +0 -93
- package/dist/proxy/shell-compressor.js +0 -390
- package/dist/proxy/shell-graph-boost.js +0 -202
- package/dist/proxy/shell-monitor-map.js +0 -18
- package/dist/proxy/shell-stats.js +0 -54
- package/dist/proxy/shell-strategies/cloud.js +0 -215
- package/dist/proxy/shell-strategies/diff.js +0 -159
- package/dist/proxy/shell-strategies/error-diagnostic.js +0 -796
- package/dist/proxy/shell-strategies/filter-dsl.js +0 -358
- package/dist/proxy/shell-strategies/git-status.js +0 -177
- package/dist/proxy/shell-strategies/key-value.js +0 -193
- package/dist/proxy/shell-strategies/log-text.js +0 -154
- package/dist/proxy/shell-strategies/omni.js +0 -188
- package/dist/proxy/shell-strategies/progress.js +0 -55
- package/dist/proxy/shell-strategies/redact.js +0 -76
- package/dist/proxy/shell-strategies/structured.js +0 -241
- package/dist/proxy/shell-strategies/tabular.js +0 -243
- package/dist/proxy/shell-strategies/test-results-types.js +0 -13
- package/dist/proxy/shell-strategies/test-results.js +0 -784
- package/dist/proxy/shell-strategies/tree-paths.js +0 -144
- package/dist/proxy/shell-strategies/yaml.js +0 -182
- package/dist/proxy/shell-tee.js +0 -111
- package/dist/proxy/signal-dedup.js +0 -171
- package/dist/proxy/startup-renderer.js +0 -158
- package/dist/proxy/task-token-display.js +0 -38
- package/dist/proxy/token-counter.js +0 -61
- package/dist/proxy/tool-clusters.js +0 -273
- package/dist/proxy/tool-definitions.js +0 -525
- package/dist/proxy/transport-mux.js +0 -229
- package/dist/proxy/wire-cap.js +0 -268
- package/dist/rules/developer.mozilla.org.json +0 -9
- package/dist/rules/github.com.json +0 -21
- package/dist/schemas/api/skills.js +0 -19
- package/dist/schemas/common/errors.js +0 -7
- package/dist/schemas/common/headers.js +0 -5
- package/dist/schemas/entities/edge.js +0 -25
- package/dist/schemas/entities/entity.js +0 -22
- package/dist/schemas/entities/rule.js +0 -18
- package/dist/schemas/index.js +0 -14
- package/dist/server/event-bus.js +0 -59
- package/dist/server/http.js +0 -156
- package/dist/server/middleware.js +0 -70
- package/dist/server/routes/drift.js +0 -97
- package/dist/server/routes/intelligence.js +0 -1217
- package/dist/server/routes/reasoning-quality.js +0 -444
- package/dist/server/routes/session.js +0 -86
- package/dist/server/routes/stream.js +0 -120
- package/dist/server/routes/system.js +0 -73
- package/dist/server/routes/temporal.js +0 -170
- package/dist/server/routes/timeline.js +0 -232
- package/dist/server/routes/token-flow.js +0 -403
- package/dist/skills/effectiveness-tracker.js +0 -93
- package/dist/skills/local-pack.js +0 -380
- package/dist/skills/resolver.js +0 -495
- package/dist/state-detector.js +0 -83
- package/dist/timeline/intent-detector.js +0 -263
- package/dist/timeline/loop-miner.js +0 -140
- package/dist/timeline/open-threads.js +0 -49
- package/dist/timeline/signal-reinforcer.js +0 -62
- package/dist/timeline/timeline-bootstrap.js +0 -151
- package/dist/timeline/timeline-store.js +0 -618
- package/dist/tools/coding/bash.js +0 -49
- package/dist/tools/coding/file-edit.js +0 -72
- package/dist/tools/coding/file-outline.js +0 -227
- package/dist/tools/coding/file-read-protocol.js +0 -425
- package/dist/tools/coding/file-read.js +0 -35
- package/dist/tools/coding/file-write.js +0 -43
- package/dist/tools/coding/glob-tool.js +0 -109
- package/dist/tools/coding/grep.js +0 -162
- package/dist/tools/coding/index.js +0 -27
- package/dist/tools/intelligence/index.js +0 -269
- package/dist/tools/intelligence/record-fact.js +0 -48
- package/dist/tools/intelligence/timeline-markers.js +0 -130
- package/dist/tools/registry.js +0 -47
- package/dist/tools/types.js +0 -8
- package/dist/tracking/auto-snapshot-triggers.js +0 -246
- package/dist/tracking/branch-context.js +0 -115
- package/dist/tracking/branch-snapshot.js +0 -217
- package/dist/tracking/causal-bridge.js +0 -317
- package/dist/tracking/circuit-breaker.js +0 -147
- package/dist/tracking/commit-watcher.js +0 -114
- package/dist/tracking/context-ledger.js +0 -119
- package/dist/tracking/correction-detector.js +0 -324
- package/dist/tracking/drift-tracker.js +0 -874
- package/dist/tracking/durability-tracker.js +0 -94
- package/dist/tracking/entity-rewind.js +0 -200
- package/dist/tracking/file-hash-state.js +0 -114
- package/dist/tracking/git-attribution.js +0 -132
- package/dist/tracking/git-trailers.js +0 -171
- package/dist/tracking/intelligence-counter.js +0 -46
- package/dist/tracking/intent-correlator.js +0 -202
- package/dist/tracking/intent-encoder.js +0 -52
- package/dist/tracking/intent-token-tracker.js +0 -159
- package/dist/tracking/ledger-archiver.js +0 -94
- package/dist/tracking/ledger-chains.js +0 -245
- package/dist/tracking/metrics-store.js +0 -361
- package/dist/tracking/native-watcher.js +0 -131
- package/dist/tracking/offline-rewind.js +0 -295
- package/dist/tracking/pending-violations.js +0 -74
- package/dist/tracking/persistence-effectiveness.js +0 -167
- package/dist/tracking/prompt-durability.js +0 -202
- package/dist/tracking/quality-signals.js +0 -213
- package/dist/tracking/redactor.js +0 -73
- package/dist/tracking/rewind-engine.js +0 -161
- package/dist/tracking/session-history.js +0 -128
- package/dist/tracking/session-receipt.js +0 -88
- package/dist/tracking/session-summary-writer.js +0 -157
- package/dist/tracking/shadow-ledger.js +0 -321
- package/dist/tracking/stash-manager.js +0 -258
- package/dist/tracking/timeline-fork.js +0 -213
- package/dist/tracking/timeline.js +0 -69
- package/dist/tracking/token-flow.js +0 -276
- package/dist/tracking/turn-segmenter.js +0 -122
- package/dist/tracking/weekly-accumulator.js +0 -179
- package/dist/tracking/working-snapshots.js +0 -188
- package/dist/tracking/workspace-manifest.js +0 -176
- package/dist/transport/http.js +0 -102
- package/dist/utils/counterfactual.js +0 -65
- package/dist/utils/deep-link.js +0 -34
- package/dist/utils/detect.js +0 -193
- package/dist/utils/exec.js +0 -73
- package/dist/utils/file-logger.js +0 -87
- package/dist/utils/format-error.js +0 -29
- package/dist/utils/git.js +0 -181
- package/dist/utils/log.js +0 -57
- package/dist/utils/logger.js +0 -35
- package/dist/utils/mcp-content-json.js +0 -8
- package/dist/utils/session-logger.js +0 -154
- package/dist/utils/startup-log.js +0 -512
- package/dist/utils/ui.js +0 -56
|
@@ -1,691 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Multi-Level Cascaded Community Detection via Louvain Algorithm.
|
|
3
|
-
*
|
|
4
|
-
* Implements a hierarchically-consistent two-phase community detection:
|
|
5
|
-
* Phase 1 — File macro-communities from weighted file edges + directory proximity + class hierarchy
|
|
6
|
-
* Phase 2 — Entity sub-communities within each macro-community (scoped Louvain)
|
|
7
|
-
* Phase 3 — Consistency validation + isolate assignment
|
|
8
|
-
*
|
|
9
|
-
* Hierarchical IDs: entity.community = macro_id * 1000 + local_sub_id
|
|
10
|
-
* This ensures: floor(entity.community / 1000) === file's macro-community for ALL entities.
|
|
11
|
-
*
|
|
12
|
-
* Performance: Phase 1 <5ms (file nodes only), Phase 2 <150ms (scoped sub-runs).
|
|
13
|
-
* Replaces per-request computation with one-time indexing materialization.
|
|
14
|
-
*/
|
|
15
|
-
import { dirname } from "node:path";
|
|
16
|
-
// NodeNext CJS interop: graphology and graphology-communities-louvain ship as CJS
|
|
17
|
-
// with .d.ts using `export default`. TypeScript NodeNext sees the module namespace
|
|
18
|
-
// instead of the default export. We define the minimal interface we use and cast.
|
|
19
|
-
import GraphNs from "graphology";
|
|
20
|
-
import louvainNs from "graphology-communities-louvain";
|
|
21
|
-
import { isTestFile } from "./indexer/test-detector.js";
|
|
22
|
-
const Graph = GraphNs;
|
|
23
|
-
const louvain = louvainNs;
|
|
24
|
-
// ── Main Entry Point ────────────────────────────────────────────
|
|
25
|
-
/**
|
|
26
|
-
* Cascaded multi-level community detection.
|
|
27
|
-
*
|
|
28
|
-
* Phase 1: Build file graph from weighted file edges + synthetic signals → Louvain → file macro-communities
|
|
29
|
-
* Phase 2: For each macro-community, run scoped Louvain on entity subgraph → sub-communities
|
|
30
|
-
* Phase 3: Validate hierarchical consistency + assign isolates
|
|
31
|
-
*/
|
|
32
|
-
export function detectCascadedCommunities(fileEdges, entities, entityEdges) {
|
|
33
|
-
if (entities.length === 0) {
|
|
34
|
-
return {
|
|
35
|
-
entityAssignments: new Map(),
|
|
36
|
-
fileCommunities: [],
|
|
37
|
-
macroCommunities: [],
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
// Collect unique file paths
|
|
41
|
-
const allFiles = new Set();
|
|
42
|
-
for (const e of entities) {
|
|
43
|
-
if (e.file_path)
|
|
44
|
-
allFiles.add(e.file_path);
|
|
45
|
-
}
|
|
46
|
-
if (allFiles.size === 0) {
|
|
47
|
-
return {
|
|
48
|
-
entityAssignments: new Map(),
|
|
49
|
-
fileCommunities: [],
|
|
50
|
-
macroCommunities: [],
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
// ── Phase 1: File Macro-Communities ─────────────────────────────
|
|
54
|
-
const fileGraph = new Graph({ type: "undirected", allowSelfLoops: false });
|
|
55
|
-
for (const fp of allFiles) {
|
|
56
|
-
fileGraph.addNode(fp, { is_test: isTestFile(fp) });
|
|
57
|
-
}
|
|
58
|
-
// Pre-aggregate file edge weights across edge types (calls + imports + tests + implements)
|
|
59
|
-
const aggregatedWeights = new Map();
|
|
60
|
-
for (const fe of fileEdges) {
|
|
61
|
-
if (!allFiles.has(fe.from_file) || !allFiles.has(fe.to_file))
|
|
62
|
-
continue;
|
|
63
|
-
if (fe.from_file === fe.to_file)
|
|
64
|
-
continue;
|
|
65
|
-
let weight = fe.weight;
|
|
66
|
-
if (fe.edge_type === "tests" ||
|
|
67
|
-
(isTestFile(fe.from_file) && !isTestFile(fe.to_file))) {
|
|
68
|
-
weight *= 3;
|
|
69
|
-
}
|
|
70
|
-
// Normalize undirected pair key (smaller path first)
|
|
71
|
-
const [a, b] = fe.from_file < fe.to_file
|
|
72
|
-
? [fe.from_file, fe.to_file]
|
|
73
|
-
: [fe.to_file, fe.from_file];
|
|
74
|
-
const pairKey = `${a}\0${b}`;
|
|
75
|
-
aggregatedWeights.set(pairKey, (aggregatedWeights.get(pairKey) ?? 0) + weight);
|
|
76
|
-
}
|
|
77
|
-
// Add aggregated file edges to graph
|
|
78
|
-
for (const [pairKey, weight] of aggregatedWeights) {
|
|
79
|
-
const [a, b] = pairKey.split("\0");
|
|
80
|
-
try {
|
|
81
|
-
fileGraph.addEdge(a, b, { weight });
|
|
82
|
-
}
|
|
83
|
-
catch {
|
|
84
|
-
// parallel edge
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
// Directory proximity synthetic edges: files in same directory get weight 0.5
|
|
88
|
-
const dirToFiles = new Map();
|
|
89
|
-
for (const fp of allFiles) {
|
|
90
|
-
const dir = dirname(fp);
|
|
91
|
-
let files = dirToFiles.get(dir);
|
|
92
|
-
if (!files) {
|
|
93
|
-
files = [];
|
|
94
|
-
dirToFiles.set(dir, files);
|
|
95
|
-
}
|
|
96
|
-
files.push(fp);
|
|
97
|
-
}
|
|
98
|
-
for (const [, files] of dirToFiles) {
|
|
99
|
-
if (files.length < 2)
|
|
100
|
-
continue;
|
|
101
|
-
// Connect files in same directory with proximity signal (limited to avoid O(n²))
|
|
102
|
-
const limit = Math.min(files.length, 20);
|
|
103
|
-
for (let i = 0; i < limit; i++) {
|
|
104
|
-
for (let j = i + 1; j < limit; j++) {
|
|
105
|
-
const a = files[i];
|
|
106
|
-
const b = files[j];
|
|
107
|
-
if (!fileGraph.hasEdge(a, b)) {
|
|
108
|
-
try {
|
|
109
|
-
fileGraph.addEdge(a, b, { weight: 0.5 });
|
|
110
|
-
}
|
|
111
|
-
catch {
|
|
112
|
-
// parallel edge
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
// Class hierarchy bonus: files sharing implements/extends get weight 2.0
|
|
119
|
-
// (Detected from entity edges of type "implements" or "extends" across files)
|
|
120
|
-
const classHierarchyFiles = new Set();
|
|
121
|
-
for (const edge of entityEdges) {
|
|
122
|
-
if (edge.type !== "implements" && edge.type !== "extends")
|
|
123
|
-
continue;
|
|
124
|
-
const fromEntity = entities.find((e) => e.key === edge.from_key);
|
|
125
|
-
const toEntity = entities.find((e) => e.key === edge.to_key);
|
|
126
|
-
if (fromEntity &&
|
|
127
|
-
toEntity &&
|
|
128
|
-
fromEntity.file_path !== toEntity.file_path &&
|
|
129
|
-
allFiles.has(fromEntity.file_path) &&
|
|
130
|
-
allFiles.has(toEntity.file_path)) {
|
|
131
|
-
const pair = `${fromEntity.file_path}|${toEntity.file_path}`;
|
|
132
|
-
if (!classHierarchyFiles.has(pair)) {
|
|
133
|
-
classHierarchyFiles.add(pair);
|
|
134
|
-
if (!fileGraph.hasEdge(fromEntity.file_path, toEntity.file_path)) {
|
|
135
|
-
try {
|
|
136
|
-
fileGraph.addEdge(fromEntity.file_path, toEntity.file_path, {
|
|
137
|
-
weight: 2.0,
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
catch {
|
|
141
|
-
// parallel edge
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
// Run Louvain on file graph
|
|
148
|
-
let filePartition = {};
|
|
149
|
-
const fileAssignments = new Map();
|
|
150
|
-
if (fileGraph.size > 0) {
|
|
151
|
-
// At least one edge exists
|
|
152
|
-
filePartition = louvain(fileGraph, {
|
|
153
|
-
resolution: 1.2,
|
|
154
|
-
getEdgeWeight: "weight",
|
|
155
|
-
});
|
|
156
|
-
// Remap to contiguous IDs
|
|
157
|
-
const idRemap = new Map();
|
|
158
|
-
let nextId = 0;
|
|
159
|
-
for (const fp of allFiles) {
|
|
160
|
-
const rawId = filePartition[fp];
|
|
161
|
-
if (rawId === undefined)
|
|
162
|
-
continue;
|
|
163
|
-
if (!idRemap.has(rawId)) {
|
|
164
|
-
idRemap.set(rawId, nextId++);
|
|
165
|
-
}
|
|
166
|
-
fileAssignments.set(fp, idRemap.get(rawId));
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
// Handle files with no edges (isolates) — group by directory, then assign to
|
|
170
|
-
// the community that contains the most files from the same directory.
|
|
171
|
-
// This ensures isolated UI files end up in a UI community, not in indexer.
|
|
172
|
-
let nextMacroId = fileAssignments.size > 0 ? Math.max(...fileAssignments.values()) + 1 : 0;
|
|
173
|
-
const isolatedFiles = [];
|
|
174
|
-
for (const fp of allFiles) {
|
|
175
|
-
if (!fileAssignments.has(fp)) {
|
|
176
|
-
isolatedFiles.push(fp);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
if (isolatedFiles.length > 0) {
|
|
180
|
-
// For each isolated file, find the community most common among files in the same directory
|
|
181
|
-
for (const fp of isolatedFiles) {
|
|
182
|
-
const dir = dirname(fp);
|
|
183
|
-
let bestCommunity = -1;
|
|
184
|
-
let bestCount = 0;
|
|
185
|
-
const commCounts = new Map();
|
|
186
|
-
for (const [otherFp, cid] of fileAssignments) {
|
|
187
|
-
if (dirname(otherFp) === dir) {
|
|
188
|
-
const count = (commCounts.get(cid) ?? 0) + 1;
|
|
189
|
-
commCounts.set(cid, count);
|
|
190
|
-
if (count > bestCount) {
|
|
191
|
-
bestCount = count;
|
|
192
|
-
bestCommunity = cid;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
if (bestCommunity >= 0) {
|
|
197
|
-
fileAssignments.set(fp, bestCommunity);
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
// No directory peers — assign to new singleton
|
|
201
|
-
fileAssignments.set(fp, nextMacroId++);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
// Post-process: reassign test files to their strongest target community
|
|
206
|
-
reassignTestFiles(fileAssignments, fileEdges, allFiles);
|
|
207
|
-
// Post-process: merge small communities (≤3 files) into nearest larger community
|
|
208
|
-
mergeSmallCommunities(fileAssignments, fileEdges, allFiles, 3);
|
|
209
|
-
// Remap to contiguous IDs after merging
|
|
210
|
-
{
|
|
211
|
-
const usedIds = new Set(fileAssignments.values());
|
|
212
|
-
const sortedIds = [...usedIds].sort((a, b) => a - b);
|
|
213
|
-
const remap = new Map();
|
|
214
|
-
sortedIds.forEach((oldId, idx) => remap.set(oldId, idx));
|
|
215
|
-
for (const [fp, cid] of fileAssignments) {
|
|
216
|
-
fileAssignments.set(fp, remap.get(cid));
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
// Build macro-community metadata
|
|
220
|
-
const macroNodes = new Map();
|
|
221
|
-
for (const [fp, cid] of fileAssignments) {
|
|
222
|
-
let nodes = macroNodes.get(cid);
|
|
223
|
-
if (!nodes) {
|
|
224
|
-
nodes = [];
|
|
225
|
-
macroNodes.set(cid, nodes);
|
|
226
|
-
}
|
|
227
|
-
nodes.push(fp);
|
|
228
|
-
}
|
|
229
|
-
const macroCommunities = [];
|
|
230
|
-
const fileCommunities = [];
|
|
231
|
-
for (const [cid, files] of macroNodes) {
|
|
232
|
-
const label = generateCommunityLabel(files);
|
|
233
|
-
const cohesion = computeFileCohesion(fileGraph, files);
|
|
234
|
-
macroCommunities.push({ id: cid, label, size: files.length, cohesion });
|
|
235
|
-
for (const fp of files) {
|
|
236
|
-
fileCommunities.push({
|
|
237
|
-
file_path: fp,
|
|
238
|
-
community: cid,
|
|
239
|
-
label,
|
|
240
|
-
cohesion,
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
macroCommunities.sort((a, b) => b.size - a.size);
|
|
245
|
-
// ── Phase 2: Entity Sub-Communities ─────────────────────────────
|
|
246
|
-
// Build entity→file lookup
|
|
247
|
-
const entityFileMap = new Map();
|
|
248
|
-
for (const e of entities) {
|
|
249
|
-
entityFileMap.set(e.key, e.file_path);
|
|
250
|
-
}
|
|
251
|
-
// Group entities by macro-community
|
|
252
|
-
const macroEntityGroups = new Map();
|
|
253
|
-
for (const e of entities) {
|
|
254
|
-
const macroCid = fileAssignments.get(e.file_path);
|
|
255
|
-
if (macroCid === undefined)
|
|
256
|
-
continue;
|
|
257
|
-
let group = macroEntityGroups.get(macroCid);
|
|
258
|
-
if (!group) {
|
|
259
|
-
group = [];
|
|
260
|
-
macroEntityGroups.set(macroCid, group);
|
|
261
|
-
}
|
|
262
|
-
group.push(e.key);
|
|
263
|
-
}
|
|
264
|
-
const entityAssignments = new Map();
|
|
265
|
-
// Build entity edge lookup for efficient subgraph extraction
|
|
266
|
-
const entityEdgeSet = new Map();
|
|
267
|
-
for (const edge of entityEdges) {
|
|
268
|
-
let fromEdges = entityEdgeSet.get(edge.from_key);
|
|
269
|
-
if (!fromEdges) {
|
|
270
|
-
fromEdges = [];
|
|
271
|
-
entityEdgeSet.set(edge.from_key, fromEdges);
|
|
272
|
-
}
|
|
273
|
-
fromEdges.push({ to: edge.to_key, type: edge.type });
|
|
274
|
-
// Undirected — add reverse
|
|
275
|
-
let toEdges = entityEdgeSet.get(edge.to_key);
|
|
276
|
-
if (!toEdges) {
|
|
277
|
-
toEdges = [];
|
|
278
|
-
entityEdgeSet.set(edge.to_key, toEdges);
|
|
279
|
-
}
|
|
280
|
-
toEdges.push({ to: edge.from_key, type: edge.type });
|
|
281
|
-
}
|
|
282
|
-
for (const [macroCid, entityKeys] of macroEntityGroups) {
|
|
283
|
-
if (entityKeys.length <= 1) {
|
|
284
|
-
// Single entity — assign directly
|
|
285
|
-
for (const key of entityKeys) {
|
|
286
|
-
entityAssignments.set(key, macroCid * 1000);
|
|
287
|
-
}
|
|
288
|
-
continue;
|
|
289
|
-
}
|
|
290
|
-
// Build subgraph for this macro-community
|
|
291
|
-
const subgraph = new Graph({ type: "undirected", allowSelfLoops: false });
|
|
292
|
-
const entityKeySet = new Set(entityKeys);
|
|
293
|
-
for (const key of entityKeys) {
|
|
294
|
-
subgraph.addNode(key);
|
|
295
|
-
}
|
|
296
|
-
for (const key of entityKeys) {
|
|
297
|
-
const neighbors = entityEdgeSet.get(key);
|
|
298
|
-
if (!neighbors)
|
|
299
|
-
continue;
|
|
300
|
-
for (const { to, type } of neighbors) {
|
|
301
|
-
if (!entityKeySet.has(to) || key === to)
|
|
302
|
-
continue;
|
|
303
|
-
if (subgraph.hasEdge(key, to))
|
|
304
|
-
continue;
|
|
305
|
-
// Weight: contains=0.3, test→source=0.1, standard=1.0
|
|
306
|
-
let weight = 1.0;
|
|
307
|
-
if (type === "contains") {
|
|
308
|
-
weight = 0.3;
|
|
309
|
-
}
|
|
310
|
-
else if (type === "tests" || type === "calls") {
|
|
311
|
-
const fromFile = entityFileMap.get(key);
|
|
312
|
-
const toFile = entityFileMap.get(to);
|
|
313
|
-
if (fromFile &&
|
|
314
|
-
toFile &&
|
|
315
|
-
isTestFile(fromFile) &&
|
|
316
|
-
!isTestFile(toFile)) {
|
|
317
|
-
weight = 0.1;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
try {
|
|
321
|
-
subgraph.addEdge(key, to, { weight });
|
|
322
|
-
}
|
|
323
|
-
catch {
|
|
324
|
-
// parallel edge
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
// Run Louvain on subgraph
|
|
329
|
-
if (subgraph.size === 0) {
|
|
330
|
-
// No edges — each entity gets its own sub-community
|
|
331
|
-
let localId = 0;
|
|
332
|
-
for (const key of entityKeys) {
|
|
333
|
-
entityAssignments.set(key, macroCid * 1000 + localId++);
|
|
334
|
-
}
|
|
335
|
-
continue;
|
|
336
|
-
}
|
|
337
|
-
const subPartition = louvain(subgraph, {
|
|
338
|
-
resolution: 1.0,
|
|
339
|
-
getEdgeWeight: "weight",
|
|
340
|
-
});
|
|
341
|
-
// Remap sub-community IDs: macro_id * 1000 + local_sub_id
|
|
342
|
-
const subRemap = new Map();
|
|
343
|
-
let nextSubId = 0;
|
|
344
|
-
for (const key of entityKeys) {
|
|
345
|
-
const rawSubId = subPartition[key];
|
|
346
|
-
if (rawSubId === undefined) {
|
|
347
|
-
// Isolate within subgraph
|
|
348
|
-
entityAssignments.set(key, macroCid * 1000 + nextSubId++);
|
|
349
|
-
continue;
|
|
350
|
-
}
|
|
351
|
-
if (!subRemap.has(rawSubId)) {
|
|
352
|
-
subRemap.set(rawSubId, nextSubId++);
|
|
353
|
-
}
|
|
354
|
-
entityAssignments.set(key, macroCid * 1000 + subRemap.get(rawSubId));
|
|
355
|
-
}
|
|
356
|
-
// Split oversized sub-communities (>25% of macro-community)
|
|
357
|
-
const threshold = entityKeys.length * 0.25;
|
|
358
|
-
splitOversizedSubCommunities(subgraph, entityAssignments, entityKeys, macroCid, threshold, nextSubId);
|
|
359
|
-
}
|
|
360
|
-
// ── Phase 3: Consistency Validation ─────────────────────────────
|
|
361
|
-
// Ensure every entity has an assignment
|
|
362
|
-
for (const e of entities) {
|
|
363
|
-
if (!entityAssignments.has(e.key)) {
|
|
364
|
-
const macroCid = fileAssignments.get(e.file_path) ?? 0;
|
|
365
|
-
entityAssignments.set(e.key, macroCid * 1000);
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
return { entityAssignments, fileCommunities, macroCommunities };
|
|
369
|
-
}
|
|
370
|
-
/** @deprecated Use detectCascadedCommunities instead */
|
|
371
|
-
export function detectCommunities(entities, edges) {
|
|
372
|
-
// Delegate to cascaded detection with no file edges (flat fallback)
|
|
373
|
-
const result = detectCascadedCommunities([], entities.map((e) => ({ ...e, kind: "function" })), edges.map((e) => ({
|
|
374
|
-
from_key: e.from_key,
|
|
375
|
-
to_key: e.to_key,
|
|
376
|
-
type: e.type ?? "calls",
|
|
377
|
-
})));
|
|
378
|
-
return {
|
|
379
|
-
assignments: result.entityAssignments,
|
|
380
|
-
communities: result.macroCommunities,
|
|
381
|
-
};
|
|
382
|
-
}
|
|
383
|
-
// ── Helper Functions ────────────────────────────────────────────
|
|
384
|
-
/**
|
|
385
|
-
* Merge small communities (≤ minSize files) into their nearest larger community.
|
|
386
|
-
* Uses file edge connections first, then directory proximity as fallback.
|
|
387
|
-
*/
|
|
388
|
-
function mergeSmallCommunities(fileAssignments, fileEdges, allFiles, minSize) {
|
|
389
|
-
// Build community → files map
|
|
390
|
-
const communityFiles = new Map();
|
|
391
|
-
for (const [fp, cid] of fileAssignments) {
|
|
392
|
-
let files = communityFiles.get(cid);
|
|
393
|
-
if (!files) {
|
|
394
|
-
files = [];
|
|
395
|
-
communityFiles.set(cid, files);
|
|
396
|
-
}
|
|
397
|
-
files.push(fp);
|
|
398
|
-
}
|
|
399
|
-
// Identify large communities (merge targets)
|
|
400
|
-
const largeCommunities = new Set();
|
|
401
|
-
for (const [cid, files] of communityFiles) {
|
|
402
|
-
if (files.length > minSize)
|
|
403
|
-
largeCommunities.add(cid);
|
|
404
|
-
}
|
|
405
|
-
if (largeCommunities.size === 0)
|
|
406
|
-
return;
|
|
407
|
-
// Build file→community edge weight map for efficient lookup
|
|
408
|
-
const fileEdgeWeights = new Map();
|
|
409
|
-
for (const edge of fileEdges) {
|
|
410
|
-
const fromCid = fileAssignments.get(edge.from_file);
|
|
411
|
-
const toCid = fileAssignments.get(edge.to_file);
|
|
412
|
-
if (fromCid === undefined || toCid === undefined)
|
|
413
|
-
continue;
|
|
414
|
-
// Record edge from from_file to to_file's community
|
|
415
|
-
if (largeCommunities.has(toCid) && fromCid !== toCid) {
|
|
416
|
-
let weights = fileEdgeWeights.get(edge.from_file);
|
|
417
|
-
if (!weights) {
|
|
418
|
-
weights = new Map();
|
|
419
|
-
fileEdgeWeights.set(edge.from_file, weights);
|
|
420
|
-
}
|
|
421
|
-
weights.set(toCid, (weights.get(toCid) ?? 0) + edge.weight);
|
|
422
|
-
}
|
|
423
|
-
// Reverse direction
|
|
424
|
-
if (largeCommunities.has(fromCid) && toCid !== fromCid) {
|
|
425
|
-
let weights = fileEdgeWeights.get(edge.to_file);
|
|
426
|
-
if (!weights) {
|
|
427
|
-
weights = new Map();
|
|
428
|
-
fileEdgeWeights.set(edge.to_file, weights);
|
|
429
|
-
}
|
|
430
|
-
weights.set(fromCid, (weights.get(fromCid) ?? 0) + edge.weight);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
// Merge small communities
|
|
434
|
-
for (const [cid, files] of communityFiles) {
|
|
435
|
-
if (files.length > minSize || largeCommunities.has(cid))
|
|
436
|
-
continue;
|
|
437
|
-
// Strategy 1: find the large community with strongest file edge connections
|
|
438
|
-
const communityWeights = new Map();
|
|
439
|
-
for (const fp of files) {
|
|
440
|
-
const weights = fileEdgeWeights.get(fp);
|
|
441
|
-
if (weights) {
|
|
442
|
-
for (const [targetCid, w] of weights) {
|
|
443
|
-
if (largeCommunities.has(targetCid)) {
|
|
444
|
-
communityWeights.set(targetCid, (communityWeights.get(targetCid) ?? 0) + w);
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
let bestCommunity = null;
|
|
450
|
-
let bestWeight = 0;
|
|
451
|
-
for (const [targetCid, w] of communityWeights) {
|
|
452
|
-
if (w > bestWeight) {
|
|
453
|
-
bestWeight = w;
|
|
454
|
-
bestCommunity = targetCid;
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
// Strategy 2: fallback to directory proximity — find the large community
|
|
458
|
-
// that shares the most common directory prefix
|
|
459
|
-
if (bestCommunity === null) {
|
|
460
|
-
const dirs = files.map((fp) => dirname(fp));
|
|
461
|
-
const communityDirOverlap = new Map();
|
|
462
|
-
for (const dir of dirs) {
|
|
463
|
-
for (const [targetCid, targetFiles] of communityFiles) {
|
|
464
|
-
if (!largeCommunities.has(targetCid))
|
|
465
|
-
continue;
|
|
466
|
-
for (const tf of targetFiles) {
|
|
467
|
-
if (dirname(tf) === dir) {
|
|
468
|
-
communityDirOverlap.set(targetCid, (communityDirOverlap.get(targetCid) ?? 0) + 1);
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
let bestOverlap = 0;
|
|
474
|
-
for (const [targetCid, overlap] of communityDirOverlap) {
|
|
475
|
-
if (overlap > bestOverlap) {
|
|
476
|
-
bestOverlap = overlap;
|
|
477
|
-
bestCommunity = targetCid;
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
// Strategy 3: if still no match, merge into the largest community
|
|
482
|
-
if (bestCommunity === null) {
|
|
483
|
-
let maxSize = 0;
|
|
484
|
-
for (const [targetCid, targetFiles] of communityFiles) {
|
|
485
|
-
if (largeCommunities.has(targetCid) && targetFiles.length > maxSize) {
|
|
486
|
-
maxSize = targetFiles.length;
|
|
487
|
-
bestCommunity = targetCid;
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
if (bestCommunity !== null) {
|
|
492
|
-
for (const fp of files) {
|
|
493
|
-
fileAssignments.set(fp, bestCommunity);
|
|
494
|
-
}
|
|
495
|
-
// Update communityFiles for subsequent iterations
|
|
496
|
-
const targetFiles = communityFiles.get(bestCommunity);
|
|
497
|
-
targetFiles.push(...files);
|
|
498
|
-
communityFiles.delete(cid);
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
/**
|
|
503
|
-
* Reassign test files to the community of their strongest test target.
|
|
504
|
-
*/
|
|
505
|
-
function reassignTestFiles(fileAssignments, fileEdges, allFiles) {
|
|
506
|
-
for (const fp of allFiles) {
|
|
507
|
-
if (!isTestFile(fp))
|
|
508
|
-
continue;
|
|
509
|
-
// Find strongest non-test target
|
|
510
|
-
let bestTarget = null;
|
|
511
|
-
let bestWeight = 0;
|
|
512
|
-
for (const edge of fileEdges) {
|
|
513
|
-
const from = edge.from_file === fp ? edge.to_file : null;
|
|
514
|
-
const to = edge.to_file === fp ? edge.from_file : null;
|
|
515
|
-
const target = from ?? to;
|
|
516
|
-
if (!target || isTestFile(target) || !allFiles.has(target))
|
|
517
|
-
continue;
|
|
518
|
-
if (edge.weight > bestWeight) {
|
|
519
|
-
bestWeight = edge.weight;
|
|
520
|
-
bestTarget = target;
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
if (bestTarget) {
|
|
524
|
-
const targetCommunity = fileAssignments.get(bestTarget);
|
|
525
|
-
if (targetCommunity !== undefined) {
|
|
526
|
-
fileAssignments.set(fp, targetCommunity);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
/**
|
|
532
|
-
* Split oversized sub-communities within a macro-community.
|
|
533
|
-
*/
|
|
534
|
-
function splitOversizedSubCommunities(graph, assignments, entityKeys, macroCid, threshold, startSubId, depth = 0) {
|
|
535
|
-
if (depth > 3)
|
|
536
|
-
return;
|
|
537
|
-
// Group by current sub-community
|
|
538
|
-
const subGroups = new Map();
|
|
539
|
-
for (const key of entityKeys) {
|
|
540
|
-
const cid = assignments.get(key);
|
|
541
|
-
if (cid === undefined)
|
|
542
|
-
continue;
|
|
543
|
-
let group = subGroups.get(cid);
|
|
544
|
-
if (!group) {
|
|
545
|
-
group = [];
|
|
546
|
-
subGroups.set(cid, group);
|
|
547
|
-
}
|
|
548
|
-
group.push(key);
|
|
549
|
-
}
|
|
550
|
-
let nextSubId = startSubId;
|
|
551
|
-
for (const [cid, nodes] of subGroups) {
|
|
552
|
-
if (nodes.length <= threshold)
|
|
553
|
-
continue;
|
|
554
|
-
// Build subgraph
|
|
555
|
-
const subgraph = new Graph({ type: "undirected", allowSelfLoops: false });
|
|
556
|
-
const nodeSet = new Set(nodes);
|
|
557
|
-
for (const node of nodes) {
|
|
558
|
-
subgraph.addNode(node);
|
|
559
|
-
}
|
|
560
|
-
for (const node of nodes) {
|
|
561
|
-
graph.forEachEdge(node, (_edge, _attrs, source, target) => {
|
|
562
|
-
if (nodeSet.has(source) &&
|
|
563
|
-
nodeSet.has(target) &&
|
|
564
|
-
!subgraph.hasEdge(source, target)) {
|
|
565
|
-
try {
|
|
566
|
-
subgraph.addEdge(source, target);
|
|
567
|
-
}
|
|
568
|
-
catch {
|
|
569
|
-
// parallel edge
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
});
|
|
573
|
-
}
|
|
574
|
-
if (subgraph.size === 0)
|
|
575
|
-
continue;
|
|
576
|
-
const subPartition = louvain(subgraph, { resolution: 1.0 });
|
|
577
|
-
const subIds = new Set(Object.values(subPartition));
|
|
578
|
-
if (subIds.size <= 1)
|
|
579
|
-
continue;
|
|
580
|
-
// Remap
|
|
581
|
-
const subRemap = new Map();
|
|
582
|
-
let firstSub = true;
|
|
583
|
-
for (const node of nodes) {
|
|
584
|
-
const rawId = subPartition[node];
|
|
585
|
-
if (rawId === undefined)
|
|
586
|
-
continue;
|
|
587
|
-
if (!subRemap.has(rawId)) {
|
|
588
|
-
if (firstSub) {
|
|
589
|
-
subRemap.set(rawId, cid - macroCid * 1000); // Keep original local ID
|
|
590
|
-
firstSub = false;
|
|
591
|
-
}
|
|
592
|
-
else {
|
|
593
|
-
subRemap.set(rawId, nextSubId++);
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
assignments.set(node, macroCid * 1000 + subRemap.get(rawId));
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
/**
|
|
601
|
-
* Compute cohesion for file-level nodes in the file graph.
|
|
602
|
-
*/
|
|
603
|
-
function computeFileCohesion(graph, nodes) {
|
|
604
|
-
if (nodes.length < 2)
|
|
605
|
-
return 0.0;
|
|
606
|
-
const nodeSet = new Set(nodes);
|
|
607
|
-
const countedEdges = new Set();
|
|
608
|
-
let intraEdges = 0;
|
|
609
|
-
for (const node of nodes) {
|
|
610
|
-
if (!graph.hasNode(node))
|
|
611
|
-
continue;
|
|
612
|
-
graph.forEachEdge(node, (edge, _attrs, source, target) => {
|
|
613
|
-
if (nodeSet.has(source) &&
|
|
614
|
-
nodeSet.has(target) &&
|
|
615
|
-
!countedEdges.has(edge)) {
|
|
616
|
-
countedEdges.add(edge);
|
|
617
|
-
intraEdges++;
|
|
618
|
-
}
|
|
619
|
-
});
|
|
620
|
-
}
|
|
621
|
-
const maxPossible = (nodes.length * (nodes.length - 1)) / 2;
|
|
622
|
-
return maxPossible > 0
|
|
623
|
-
? Math.round((intraEdges / maxPossible) * 1000) / 1000
|
|
624
|
-
: 0.0;
|
|
625
|
-
}
|
|
626
|
-
/**
|
|
627
|
-
* Generate a community label from the most common file path prefix.
|
|
628
|
-
*/
|
|
629
|
-
function generateCommunityLabel(filePaths) {
|
|
630
|
-
if (filePaths.length === 0)
|
|
631
|
-
return "unknown";
|
|
632
|
-
const dirPaths = filePaths.map((fp) => {
|
|
633
|
-
const lastSlash = fp.lastIndexOf("/");
|
|
634
|
-
return lastSlash >= 0 ? fp.substring(0, lastSlash) : "";
|
|
635
|
-
});
|
|
636
|
-
const dirCounts = new Map();
|
|
637
|
-
for (const dir of dirPaths) {
|
|
638
|
-
const segments = dir.split("/").filter((s) => s.length > 0);
|
|
639
|
-
for (let i = 0; i < segments.length; i++) {
|
|
640
|
-
const prefix = segments.slice(0, i + 1).join("/");
|
|
641
|
-
dirCounts.set(prefix, (dirCounts.get(prefix) ?? 0) + 1);
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
const halfCount = filePaths.length / 2;
|
|
645
|
-
let bestDir = "";
|
|
646
|
-
let bestDepth = 0;
|
|
647
|
-
for (const [dir, count] of dirCounts) {
|
|
648
|
-
if (count >= halfCount) {
|
|
649
|
-
const depth = dir.split("/").length;
|
|
650
|
-
if (depth > bestDepth) {
|
|
651
|
-
bestDepth = depth;
|
|
652
|
-
bestDir = dir;
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
if (bestDir.length === 0) {
|
|
657
|
-
const singleDirs = new Map();
|
|
658
|
-
for (const dir of dirPaths) {
|
|
659
|
-
const parts = dir.split("/").filter((s) => s.length > 0);
|
|
660
|
-
const last = parts[parts.length - 1];
|
|
661
|
-
if (last) {
|
|
662
|
-
singleDirs.set(last, (singleDirs.get(last) ?? 0) + 1);
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
let maxCount = 0;
|
|
666
|
-
for (const [dir, count] of singleDirs) {
|
|
667
|
-
if (count > maxCount) {
|
|
668
|
-
maxCount = count;
|
|
669
|
-
bestDir = dir;
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
return bestDir || "unknown";
|
|
673
|
-
}
|
|
674
|
-
const segments = bestDir.split("/").filter((s) => s.length > 0);
|
|
675
|
-
const genericPrefixes = new Set([
|
|
676
|
-
"src",
|
|
677
|
-
"lib",
|
|
678
|
-
"app",
|
|
679
|
-
"packages",
|
|
680
|
-
"internal",
|
|
681
|
-
"pkg",
|
|
682
|
-
"cmd",
|
|
683
|
-
]);
|
|
684
|
-
for (let i = segments.length - 1; i >= 0; i--) {
|
|
685
|
-
const seg = segments[i];
|
|
686
|
-
if (seg && !genericPrefixes.has(seg)) {
|
|
687
|
-
return seg;
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
return segments[segments.length - 1] ?? "unknown";
|
|
691
|
-
}
|