toolnet-memory 0.2.0
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/.env.example +28 -0
- package/README.md +147 -0
- package/bin/toolnet-memory +415 -0
- package/dist/packages/cli/commands.js +1 -0
- package/dist/packages/cli/index.js +1 -0
- package/dist/packages/sdk/client.js +78 -0
- package/dist/packages/sdk/index.js +1 -0
- package/dist/packages/sdk/types.js +1 -0
- package/dist/src/api/index.js +1 -0
- package/dist/src/api/routes/health.js +1 -0
- package/dist/src/api/routes/index.js +1 -0
- package/dist/src/api/routes/memory.js +1 -0
- package/dist/src/api/routes/projects.js +1 -0
- package/dist/src/api/routes/search.js +1 -0
- package/dist/src/api/server.js +1 -0
- package/dist/src/capture/activity-capture.js +21 -0
- package/dist/src/capture/command-capture.js +12 -0
- package/dist/src/capture/error-capture.js +14 -0
- package/dist/src/capture/event-deduplicator.js +28 -0
- package/dist/src/capture/event-queue.js +20 -0
- package/dist/src/capture/file-capture.js +23 -0
- package/dist/src/capture/index.js +8 -0
- package/dist/src/capture/session-capture.js +18 -0
- package/dist/src/capture/tool-capture.js +13 -0
- package/dist/src/code-intelligence/analysis/analysis-engine.js +26 -0
- package/dist/src/code-intelligence/analysis/dead-code-analyzer.js +127 -0
- package/dist/src/code-intelligence/analysis/dependency-analyzer.js +66 -0
- package/dist/src/code-intelligence/analysis/index.js +4 -0
- package/dist/src/code-intelligence/analysis/types.js +1 -0
- package/dist/src/code-intelligence/architecture/architecture-engine.js +48 -0
- package/dist/src/code-intelligence/architecture/clusters.js +322 -0
- package/dist/src/code-intelligence/architecture/entry-points.js +150 -0
- package/dist/src/code-intelligence/architecture/hotspots.js +136 -0
- package/dist/src/code-intelligence/architecture/index.js +6 -0
- package/dist/src/code-intelligence/architecture/layers.js +197 -0
- package/dist/src/code-intelligence/architecture/types.js +1 -0
- package/dist/src/code-intelligence/chunks/index.js +2 -0
- package/dist/src/code-intelligence/chunks/smart-chunker.js +125 -0
- package/dist/src/code-intelligence/chunks/types.js +1 -0
- package/dist/src/code-intelligence/git/git-diff.js +109 -0
- package/dist/src/code-intelligence/git/index.js +1 -0
- package/dist/src/code-intelligence/graph/architecture.js +26 -0
- package/dist/src/code-intelligence/graph/graph-builder.js +213 -0
- package/dist/src/code-intelligence/graph/graph-store.js +57 -0
- package/dist/src/code-intelligence/graph/index.js +1 -0
- package/dist/src/code-intelligence/graph/relationships.js +1 -0
- package/dist/src/code-intelligence/graph/trace.js +65 -0
- package/dist/src/code-intelligence/impact/blast-radius.js +160 -0
- package/dist/src/code-intelligence/impact/change-mapper.js +69 -0
- package/dist/src/code-intelligence/impact/edit-tool-detector.js +92 -0
- package/dist/src/code-intelligence/impact/impact-analyzer.js +60 -0
- package/dist/src/code-intelligence/impact/impact-guard.js +33 -0
- package/dist/src/code-intelligence/impact/index.js +5 -0
- package/dist/src/code-intelligence/incremental/file-hash.js +8 -0
- package/dist/src/code-intelligence/incremental/incremental-indexer.js +106 -0
- package/dist/src/code-intelligence/incremental/manifest-builder.js +19 -0
- package/dist/src/code-intelligence/incremental/manifest.js +31 -0
- package/dist/src/code-intelligence/index.js +23 -0
- package/dist/src/code-intelligence/indexer/file-watcher.js +1 -0
- package/dist/src/code-intelligence/indexer/incremental-indexer.js +1 -0
- package/dist/src/code-intelligence/indexer/index.js +1 -0
- package/dist/src/code-intelligence/indexer/repository-indexer.js +31 -0
- package/dist/src/code-intelligence/indexer/repository-scanner.js +50 -0
- package/dist/src/code-intelligence/parsers/index.js +1 -0
- package/dist/src/code-intelligence/parsers/tree-sitter/index.js +1 -0
- package/dist/src/code-intelligence/parsers/tree-sitter/languages.js +1 -0
- package/dist/src/code-intelligence/parsers/tree-sitter/parser.js +1 -0
- package/dist/src/code-intelligence/parsers/typescript-parser.js +190 -0
- package/dist/src/code-intelligence/query/graph-query-engine.js +275 -0
- package/dist/src/code-intelligence/query/index.js +2 -0
- package/dist/src/code-intelligence/query/types.js +1 -0
- package/dist/src/code-intelligence/resolution/index.js +2 -0
- package/dist/src/code-intelligence/resolution/types.js +1 -0
- package/dist/src/code-intelligence/resolution/typescript-resolver.js +316 -0
- package/dist/src/code-intelligence/rich/index.js +1 -0
- package/dist/src/code-intelligence/rich/rich-graph-enricher.js +463 -0
- package/dist/src/code-intelligence/semantic/index.js +2 -0
- package/dist/src/code-intelligence/semantic/semantic-code-engine.js +213 -0
- package/dist/src/code-intelligence/semantic/source-priority.js +22 -0
- package/dist/src/code-intelligence/symbols/index.js +1 -0
- package/dist/src/code-intelligence/symbols/reference-resolver.js +21 -0
- package/dist/src/code-intelligence/symbols/symbol-extractor.js +1 -0
- package/dist/src/code-intelligence/test-analysis.js +72 -0
- package/dist/src/code-intelligence/test-architecture.js +67 -0
- package/dist/src/code-intelligence/test-impact.js +47 -0
- package/dist/src/code-intelligence/test-incremental.js +24 -0
- package/dist/src/code-intelligence/test-index.js +39 -0
- package/dist/src/code-intelligence/test-resolution.js +54 -0
- package/dist/src/code-intelligence/test-rich-graph.js +48 -0
- package/dist/src/code-intelligence/test-semantic.js +57 -0
- package/dist/src/code-intelligence/test-visualization.js +36 -0
- package/dist/src/code-intelligence/types.js +1 -0
- package/dist/src/code-intelligence/visualization/index.js +2 -0
- package/dist/src/code-intelligence/visualization/types.js +1 -0
- package/dist/src/code-intelligence/visualization/visualization-builder.js +104 -0
- package/dist/src/core/config.js +66 -0
- package/dist/src/core/global-env.js +70 -0
- package/dist/src/core/importance.js +30 -0
- package/dist/src/core/index.js +5 -0
- package/dist/src/core/memory-engine.js +183 -0
- package/dist/src/core/project-manager.js +243 -0
- package/dist/src/core/types.js +1 -0
- package/dist/src/embeddings/batch-embedder.js +1 -0
- package/dist/src/embeddings/cache.js +1 -0
- package/dist/src/embeddings/embedder.js +51 -0
- package/dist/src/embeddings/index.js +4 -0
- package/dist/src/embeddings/local.js +37 -0
- package/dist/src/embeddings/provider.js +1 -0
- package/dist/src/embeddings/remote.js +76 -0
- package/dist/src/embeddings/test-connection.js +16 -0
- package/dist/src/hooks/after-edit.js +3 -0
- package/dist/src/hooks/after-tool.js +3 -0
- package/dist/src/hooks/auto-context.js +68 -0
- package/dist/src/hooks/auto-impact-guard.js +94 -0
- package/dist/src/hooks/auto-retrieval.js +27 -0
- package/dist/src/hooks/before-agent.js +18 -0
- package/dist/src/hooks/before-compact.js +3 -0
- package/dist/src/hooks/before-edit.js +20 -0
- package/dist/src/hooks/index.js +11 -0
- package/dist/src/hooks/runtime.js +110 -0
- package/dist/src/hooks/session-end.js +3 -0
- package/dist/src/hooks/session-start.js +3 -0
- package/dist/src/index.js +22 -0
- package/dist/src/mcp/bootstrap.js +68 -0
- package/dist/src/mcp/context.js +1 -0
- package/dist/src/mcp/index.js +3 -0
- package/dist/src/mcp/server.js +45 -0
- package/dist/src/mcp/tools/analyze-impact.js +41 -0
- package/dist/src/mcp/tools/dead-code.js +55 -0
- package/dist/src/mcp/tools/find-callers.js +24 -0
- package/dist/src/mcp/tools/find-dependencies.js +57 -0
- package/dist/src/mcp/tools/find-symbol.js +7 -0
- package/dist/src/mcp/tools/get-architecture.js +11 -0
- package/dist/src/mcp/tools/graph-dependents.js +24 -0
- package/dist/src/mcp/tools/graph-neighborhood.js +58 -0
- package/dist/src/mcp/tools/graph-path.js +43 -0
- package/dist/src/mcp/tools/graph-query-utils.js +32 -0
- package/dist/src/mcp/tools/impact-guard.js +34 -0
- package/dist/src/mcp/tools/index-repository.js +1 -0
- package/dist/src/mcp/tools/index.js +20 -0
- package/dist/src/mcp/tools/memory-decisions.js +1 -0
- package/dist/src/mcp/tools/memory-forget.js +14 -0
- package/dist/src/mcp/tools/memory-recent.js +1 -0
- package/dist/src/mcp/tools/memory-remember.js +33 -0
- package/dist/src/mcp/tools/memory-rules.js +1 -0
- package/dist/src/mcp/tools/memory-search.js +18 -0
- package/dist/src/mcp/tools/memory-todos.js +1 -0
- package/dist/src/mcp/tools/project-context.js +25 -0
- package/dist/src/mcp/tools/search-code.js +21 -0
- package/dist/src/mcp/tools/semantic-code-search.js +33 -0
- package/dist/src/mcp/tools/snapshot-create.js +13 -0
- package/dist/src/mcp/tools/snapshot-list.js +7 -0
- package/dist/src/mcp/tools/snapshot-restore.js +13 -0
- package/dist/src/mcp/tools/trace-calls.js +37 -0
- package/dist/src/memory/activity.js +5 -0
- package/dist/src/memory/conflict-detector.js +105 -0
- package/dist/src/memory/consolidator.js +104 -0
- package/dist/src/memory/decay.js +59 -0
- package/dist/src/memory/decisions.js +3 -0
- package/dist/src/memory/deduplicate.js +25 -0
- package/dist/src/memory/index.js +11 -0
- package/dist/src/memory/maintenance.js +16 -0
- package/dist/src/memory/remember.js +3 -0
- package/dist/src/memory/rules.js +3 -0
- package/dist/src/memory/search.js +3 -0
- package/dist/src/memory/summaries.js +1 -0
- package/dist/src/memory/todos.js +3 -0
- package/dist/src/processor/decision-extractor.js +13 -0
- package/dist/src/processor/importance-scorer.js +22 -0
- package/dist/src/processor/index.js +7 -0
- package/dist/src/processor/memory-extractor.js +59 -0
- package/dist/src/processor/memory-processor.js +105 -0
- package/dist/src/processor/rule-extractor.js +13 -0
- package/dist/src/processor/summarizer.js +71 -0
- package/dist/src/processor/todo-extractor.js +12 -0
- package/dist/src/production/config-check.js +34 -0
- package/dist/src/production/doctor.js +58 -0
- package/dist/src/production/full-index.js +114 -0
- package/dist/src/production/health.js +58 -0
- package/dist/src/production/index-pipeline.js +151 -0
- package/dist/src/production/index.js +4 -0
- package/dist/src/production/project-lock.js +95 -0
- package/dist/src/production/project-status.js +16 -0
- package/dist/src/production/recovery.js +15 -0
- package/dist/src/production/snapshot-cli.js +58 -0
- package/dist/src/project-manual/cli.js +77 -0
- package/dist/src/project-manual/index.js +2 -0
- package/dist/src/project-manual/manager.js +147 -0
- package/dist/src/project-manual/types.js +1 -0
- package/dist/src/retrieval/bm25/index.js +1 -0
- package/dist/src/retrieval/bm25/search.js +1 -0
- package/dist/src/retrieval/bm25.js +30 -0
- package/dist/src/retrieval/context-budget.js +1 -0
- package/dist/src/retrieval/context-builder.js +22 -0
- package/dist/src/retrieval/graph/index.js +1 -0
- package/dist/src/retrieval/graph/search.js +1 -0
- package/dist/src/retrieval/hybrid/hybrid-search.js +1 -0
- package/dist/src/retrieval/hybrid/index.js +1 -0
- package/dist/src/retrieval/hybrid-search.js +68 -0
- package/dist/src/retrieval/importance.js +16 -0
- package/dist/src/retrieval/index.js +12 -0
- package/dist/src/retrieval/query-analyzer.js +32 -0
- package/dist/src/retrieval/query-classifier.js +1 -0
- package/dist/src/retrieval/query-expansion.js +1 -0
- package/dist/src/retrieval/recency.js +12 -0
- package/dist/src/retrieval/reranker/index.js +1 -0
- package/dist/src/retrieval/reranker/reranker.js +1 -0
- package/dist/src/retrieval/retrieval-engine.js +17 -0
- package/dist/src/retrieval/tokenizer.js +9 -0
- package/dist/src/retrieval/type-priority.js +10 -0
- package/dist/src/retrieval/types.js +1 -0
- package/dist/src/retrieval/vector/index.js +4 -0
- package/dist/src/retrieval/vector/metadata-filter.js +1 -0
- package/dist/src/retrieval/vector/persistence.js +67 -0
- package/dist/src/retrieval/vector/search.js +12 -0
- package/dist/src/retrieval/vector/test-persistence.js +36 -0
- package/dist/src/retrieval/vector/upsert.js +40 -0
- package/dist/src/retrieval/vector/vector-store.js +89 -0
- package/dist/src/retrieval/vector-hybrid-engine.js +91 -0
- package/dist/src/runtime/bootstrap.js +28 -0
- package/dist/src/runtime/graceful.js +17 -0
- package/dist/src/runtime/index.js +3 -0
- package/dist/src/runtime/toolnet-memory-runtime.js +156 -0
- package/dist/src/security/encryption.js +1 -0
- package/dist/src/security/file-filter.js +30 -0
- package/dist/src/security/index.js +3 -0
- package/dist/src/security/sanitizer.js +49 -0
- package/dist/src/security/secret-scanner.js +56 -0
- package/dist/src/session/agy/adapter.js +159 -0
- package/dist/src/session/agy/cli.js +87 -0
- package/dist/src/session/agy/hook-installer.js +62 -0
- package/dist/src/session/agy/hook.js +167 -0
- package/dist/src/session/agy/index.js +5 -0
- package/dist/src/session/agy/project-resolver.js +27 -0
- package/dist/src/session/agy/recovery.js +112 -0
- package/dist/src/session/agy/transcript.js +191 -0
- package/dist/src/session/codex/adapter.js +138 -0
- package/dist/src/session/codex/cli.js +236 -0
- package/dist/src/session/codex/context-hook-installer.js +70 -0
- package/dist/src/session/codex/context-hook.js +69 -0
- package/dist/src/session/codex/discovery.js +138 -0
- package/dist/src/session/codex/index.js +6 -0
- package/dist/src/session/codex/notify-installer.js +135 -0
- package/dist/src/session/codex/project-resolver.js +25 -0
- package/dist/src/session/codex/recovery.js +27 -0
- package/dist/src/session/codex/rollout.js +186 -0
- package/dist/src/session/core.js +239 -0
- package/dist/src/session/identity.js +57 -0
- package/dist/src/session/index.js +9 -0
- package/dist/src/session/learner/cli.js +62 -0
- package/dist/src/session/learner/extractor.js +461 -0
- package/dist/src/session/learner/index.js +4 -0
- package/dist/src/session/learner/journal.js +159 -0
- package/dist/src/session/learner/learner.js +124 -0
- package/dist/src/session/learner/types.js +1 -0
- package/dist/src/session/opencode/adapter.js +553 -0
- package/dist/src/session/opencode/cli.js +121 -0
- package/dist/src/session/opencode/index.js +2 -0
- package/dist/src/session/opencode/plugin-installer.js +230 -0
- package/dist/src/session/store.js +242 -0
- package/dist/src/session/types.js +1 -0
- package/dist/src/session/utils.js +47 -0
- package/dist/src/session/wal.js +303 -0
- package/dist/src/snapshot/index.js +2 -0
- package/dist/src/snapshot/manager.js +130 -0
- package/dist/src/snapshot/test-snapshot.js +28 -0
- package/dist/src/snapshot/types.js +1 -0
- package/dist/src/storage/architecture-store.js +37 -0
- package/dist/src/storage/cleanup-health.js +20 -0
- package/dist/src/storage/code-analysis-store.js +35 -0
- package/dist/src/storage/code-chunk-store.js +25 -0
- package/dist/src/storage/code-graph-store.js +24 -0
- package/dist/src/storage/code-manifest-store.js +24 -0
- package/dist/src/storage/code-vector-store.js +25 -0
- package/dist/src/storage/github/client.js +1 -0
- package/dist/src/storage/github/index.js +1 -0
- package/dist/src/storage/google-drive/client.js +1 -0
- package/dist/src/storage/google-drive/index.js +1 -0
- package/dist/src/storage/huggingface/client.js +116 -0
- package/dist/src/storage/huggingface/index.js +1 -0
- package/dist/src/storage/index.js +16 -0
- package/dist/src/storage/list-bucket-raw.js +14 -0
- package/dist/src/storage/list-bucket-tree.js +38 -0
- package/dist/src/storage/local/client.js +99 -0
- package/dist/src/storage/local/index.js +1 -0
- package/dist/src/storage/memory-store.js +30 -0
- package/dist/src/storage/project/cleanup-layout-v1.js +72 -0
- package/dist/src/storage/project/folder.js +15 -0
- package/dist/src/storage/project/index.js +3 -0
- package/dist/src/storage/project/layout.js +74 -0
- package/dist/src/storage/project/migrate-layout-v2.js +101 -0
- package/dist/src/storage/project/migrate.js +73 -0
- package/dist/src/storage/project/scoped-provider.js +165 -0
- package/dist/src/storage/provider.js +26 -0
- package/dist/src/storage/retrying-provider.js +23 -0
- package/dist/src/storage/test-connection.js +32 -0
- package/dist/src/storage/type-resolution-store.js +26 -0
- package/dist/src/storage/types.js +1 -0
- package/dist/src/storage/vector-store.js +30 -0
- package/dist/src/storage/visualization-store.js +25 -0
- package/dist/src/sync/compressor.js +1 -0
- package/dist/src/sync/conflict-resolver.js +1 -0
- package/dist/src/sync/downloader.js +1 -0
- package/dist/src/sync/index.js +1 -0
- package/dist/src/sync/sync-engine.js +1 -0
- package/dist/src/sync/upload-queue.js +1 -0
- package/dist/src/utils/retry.js +29 -0
- package/dist/src/visualization/server.js +270 -0
- package/dist/src/work-continuity/brief-cache.js +107 -0
- package/dist/src/work-continuity/brief.js +349 -0
- package/dist/src/work-continuity/cli.js +96 -0
- package/dist/src/work-continuity/context-cli.js +106 -0
- package/dist/src/work-continuity/context-runtime-cli.js +88 -0
- package/dist/src/work-continuity/extractor.js +339 -0
- package/dist/src/work-continuity/handoff.js +185 -0
- package/dist/src/work-continuity/index.js +14 -0
- package/dist/src/work-continuity/injection.js +84 -0
- package/dist/src/work-continuity/journal.js +49 -0
- package/dist/src/work-continuity/learner.js +127 -0
- package/dist/src/work-continuity/reducer.js +316 -0
- package/dist/src/work-continuity/semantic-extractor.js +360 -0
- package/dist/src/work-continuity/semantic-journal.js +55 -0
- package/dist/src/work-continuity/semantic-learner.js +125 -0
- package/dist/src/work-continuity/semantic-reducer.js +268 -0
- package/dist/src/work-continuity/semantic-types.js +1 -0
- package/dist/src/work-continuity/types.js +1 -0
- package/dist/visualization/public/index.html +2896 -0
- package/dist/visualization/public/vendor/3d-force-graph.min.js +5 -0
- package/package.json +69 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { createHash, } from "node:crypto";
|
|
2
|
+
const EDGE_WEIGHT = {
|
|
3
|
+
CALL_REFERENCE: 7,
|
|
4
|
+
CALLS: 3,
|
|
5
|
+
IMPORTS: 4,
|
|
6
|
+
USES_TYPE: 4,
|
|
7
|
+
WRITES: 5,
|
|
8
|
+
INHERITS: 7,
|
|
9
|
+
IMPLEMENTS: 7,
|
|
10
|
+
ROUTE: 4,
|
|
11
|
+
TESTS: 1,
|
|
12
|
+
};
|
|
13
|
+
function hash(value) {
|
|
14
|
+
return createHash("sha256")
|
|
15
|
+
.update(value)
|
|
16
|
+
.digest("hex")
|
|
17
|
+
.slice(0, 12);
|
|
18
|
+
}
|
|
19
|
+
function normalize(value) {
|
|
20
|
+
return value.replaceAll("\\", "/");
|
|
21
|
+
}
|
|
22
|
+
function subsystemOf(filePath) {
|
|
23
|
+
const path = normalize(filePath);
|
|
24
|
+
const parts = path.split("/")
|
|
25
|
+
.filter(Boolean);
|
|
26
|
+
if (parts[0] === "src" &&
|
|
27
|
+
parts[1]) {
|
|
28
|
+
return `src/${parts[1]}`;
|
|
29
|
+
}
|
|
30
|
+
if (parts[0] === "packages" &&
|
|
31
|
+
parts[1]) {
|
|
32
|
+
return `packages/${parts[1]}`;
|
|
33
|
+
}
|
|
34
|
+
if (parts[0] === "tests") {
|
|
35
|
+
return "tests";
|
|
36
|
+
}
|
|
37
|
+
if (parts[0] === "bin") {
|
|
38
|
+
return "bin";
|
|
39
|
+
}
|
|
40
|
+
return parts[0] ?? "root";
|
|
41
|
+
}
|
|
42
|
+
function clusterLabel(subsystem) {
|
|
43
|
+
const value = subsystem
|
|
44
|
+
.replace(/^src\//, "")
|
|
45
|
+
.replace(/^packages\//, "");
|
|
46
|
+
const map = {
|
|
47
|
+
"code-intelligence": "Code Intelligence",
|
|
48
|
+
memory: "Memory",
|
|
49
|
+
storage: "Storage",
|
|
50
|
+
retrieval: "Retrieval",
|
|
51
|
+
runtime: "Runtime",
|
|
52
|
+
mcp: "MCP",
|
|
53
|
+
hooks: "Hooks",
|
|
54
|
+
security: "Security",
|
|
55
|
+
snapshot: "Snapshots",
|
|
56
|
+
production: "Production",
|
|
57
|
+
capture: "Capture",
|
|
58
|
+
processor: "Processor",
|
|
59
|
+
core: "Core",
|
|
60
|
+
tests: "Tests",
|
|
61
|
+
bin: "CLI",
|
|
62
|
+
sdk: "SDK",
|
|
63
|
+
cli: "CLI",
|
|
64
|
+
};
|
|
65
|
+
return (map[value] ??
|
|
66
|
+
value);
|
|
67
|
+
}
|
|
68
|
+
export class ClusterDetector {
|
|
69
|
+
graph;
|
|
70
|
+
constructor(graph) {
|
|
71
|
+
this.graph = graph;
|
|
72
|
+
}
|
|
73
|
+
detect(projectId) {
|
|
74
|
+
const symbols = this.graph.allSymbols(projectId);
|
|
75
|
+
const edges = this.graph.allEdges(projectId);
|
|
76
|
+
const symbolById = new Map(symbols.map((symbol) => [
|
|
77
|
+
symbol.id,
|
|
78
|
+
symbol,
|
|
79
|
+
]));
|
|
80
|
+
const files = [
|
|
81
|
+
...new Set(symbols.map((symbol) => normalize(symbol.filePath))),
|
|
82
|
+
].sort();
|
|
83
|
+
const pairMap = new Map();
|
|
84
|
+
const makeKey = (a, b) => {
|
|
85
|
+
const sorted = [a, b].sort();
|
|
86
|
+
return `${sorted[0]}\u0000${sorted[1]}`;
|
|
87
|
+
};
|
|
88
|
+
for (const edge of edges) {
|
|
89
|
+
const from = symbolById.get(edge.from);
|
|
90
|
+
const to = symbolById.get(edge.to);
|
|
91
|
+
if (!from ||
|
|
92
|
+
!to) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const a = normalize(from.filePath);
|
|
96
|
+
const b = normalize(to.filePath);
|
|
97
|
+
if (a === b) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const weight = EDGE_WEIGHT[edge.type] ?? 0;
|
|
101
|
+
if (weight <= 0) {
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const key = makeKey(a, b);
|
|
105
|
+
const existing = pairMap.get(key);
|
|
106
|
+
if (existing) {
|
|
107
|
+
existing.weight +=
|
|
108
|
+
weight;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
pairMap.set(key, {
|
|
112
|
+
a: [a, b].sort()[0],
|
|
113
|
+
b: [a, b].sort()[1],
|
|
114
|
+
weight,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/*
|
|
119
|
+
* 1. Base grouping = architectural subsystem.
|
|
120
|
+
*/
|
|
121
|
+
const subsystemFiles = new Map();
|
|
122
|
+
for (const file of files) {
|
|
123
|
+
const subsystem = subsystemOf(file);
|
|
124
|
+
const group = subsystemFiles.get(subsystem) ?? [];
|
|
125
|
+
group.push(file);
|
|
126
|
+
subsystemFiles.set(subsystem, group);
|
|
127
|
+
}
|
|
128
|
+
/*
|
|
129
|
+
* 2. Split very large subsystems by strong internal connectivity.
|
|
130
|
+
*
|
|
131
|
+
* Không để code-intelligence 70+ files trở thành một khối mù.
|
|
132
|
+
*/
|
|
133
|
+
const groups = [];
|
|
134
|
+
for (const [subsystem, subsystemGroup,] of subsystemFiles) {
|
|
135
|
+
subsystemGroup.sort();
|
|
136
|
+
if (subsystemGroup.length <=
|
|
137
|
+
30) {
|
|
138
|
+
groups.push({
|
|
139
|
+
subsystem,
|
|
140
|
+
files: subsystemGroup,
|
|
141
|
+
});
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const fileSet = new Set(subsystemGroup);
|
|
145
|
+
const adjacency = new Map();
|
|
146
|
+
for (const file of subsystemGroup) {
|
|
147
|
+
adjacency.set(file, new Set());
|
|
148
|
+
}
|
|
149
|
+
for (const pair of pairMap.values()) {
|
|
150
|
+
if (!fileSet.has(pair.a) ||
|
|
151
|
+
!fileSet.has(pair.b)) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
/*
|
|
155
|
+
* Strong internal relationship only.
|
|
156
|
+
*/
|
|
157
|
+
if (pair.weight >=
|
|
158
|
+
8) {
|
|
159
|
+
adjacency.get(pair.a)?.add(pair.b);
|
|
160
|
+
adjacency.get(pair.b)?.add(pair.a);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const visited = new Set();
|
|
164
|
+
const components = [];
|
|
165
|
+
for (const start of subsystemGroup) {
|
|
166
|
+
if (visited.has(start)) {
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const queue = [start];
|
|
170
|
+
const component = [];
|
|
171
|
+
visited.add(start);
|
|
172
|
+
while (queue.length >
|
|
173
|
+
0) {
|
|
174
|
+
const current = queue.shift();
|
|
175
|
+
component.push(current);
|
|
176
|
+
for (const neighbor of adjacency.get(current) ?? []) {
|
|
177
|
+
if (visited.has(neighbor)) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
visited.add(neighbor);
|
|
181
|
+
queue.push(neighbor);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
components.push(component.sort());
|
|
185
|
+
}
|
|
186
|
+
/*
|
|
187
|
+
* Strong components >=2 become own clusters.
|
|
188
|
+
* Singleton files remain one residual subsystem cluster.
|
|
189
|
+
*/
|
|
190
|
+
const residual = [];
|
|
191
|
+
for (const component of components) {
|
|
192
|
+
if (component.length >=
|
|
193
|
+
2) {
|
|
194
|
+
groups.push({
|
|
195
|
+
subsystem,
|
|
196
|
+
files: component,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
residual.push(...component);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (residual.length >
|
|
204
|
+
0) {
|
|
205
|
+
groups.push({
|
|
206
|
+
subsystem: `${subsystem}/residual`,
|
|
207
|
+
files: residual.sort(),
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/*
|
|
212
|
+
* 3. Merge tiny cross-subsystem groups only
|
|
213
|
+
* when coupling is extremely strong.
|
|
214
|
+
*/
|
|
215
|
+
let working = groups.map((group) => ({
|
|
216
|
+
subsystem: group.subsystem,
|
|
217
|
+
files: [...group.files],
|
|
218
|
+
}));
|
|
219
|
+
for (let pass = 0; pass < 3; pass++) {
|
|
220
|
+
let merged = false;
|
|
221
|
+
outer: for (let i = 0; i <
|
|
222
|
+
working.length; i++) {
|
|
223
|
+
const a = working[i];
|
|
224
|
+
if (!a ||
|
|
225
|
+
a.files.length >
|
|
226
|
+
3) {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
for (let j = 0; j <
|
|
230
|
+
working.length; j++) {
|
|
231
|
+
if (i === j) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
const b = working[j];
|
|
235
|
+
if (!b) {
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
let connection = 0;
|
|
239
|
+
const setA = new Set(a.files);
|
|
240
|
+
const setB = new Set(b.files);
|
|
241
|
+
for (const pair of pairMap.values()) {
|
|
242
|
+
const crosses = (setA.has(pair.a) &&
|
|
243
|
+
setB.has(pair.b)) ||
|
|
244
|
+
(setA.has(pair.b) &&
|
|
245
|
+
setB.has(pair.a));
|
|
246
|
+
if (crosses) {
|
|
247
|
+
connection +=
|
|
248
|
+
pair.weight;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (connection >=
|
|
252
|
+
20) {
|
|
253
|
+
b.files =
|
|
254
|
+
[
|
|
255
|
+
...new Set([
|
|
256
|
+
...b.files,
|
|
257
|
+
...a.files,
|
|
258
|
+
]),
|
|
259
|
+
].sort();
|
|
260
|
+
working.splice(i, 1);
|
|
261
|
+
merged =
|
|
262
|
+
true;
|
|
263
|
+
break outer;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (!merged) {
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/*
|
|
272
|
+
* 4. Compute metrics.
|
|
273
|
+
*/
|
|
274
|
+
const result = [];
|
|
275
|
+
for (const group of working) {
|
|
276
|
+
const set = new Set(group.files);
|
|
277
|
+
let internalWeight = 0;
|
|
278
|
+
let externalWeight = 0;
|
|
279
|
+
for (const pair of pairMap.values()) {
|
|
280
|
+
const inA = set.has(pair.a);
|
|
281
|
+
const inB = set.has(pair.b);
|
|
282
|
+
if (inA &&
|
|
283
|
+
inB) {
|
|
284
|
+
internalWeight +=
|
|
285
|
+
pair.weight;
|
|
286
|
+
}
|
|
287
|
+
else if (inA ||
|
|
288
|
+
inB) {
|
|
289
|
+
externalWeight +=
|
|
290
|
+
pair.weight;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const total = internalWeight +
|
|
294
|
+
externalWeight;
|
|
295
|
+
const baseSubsystem = group.subsystem
|
|
296
|
+
.replace(/\/residual$/, "");
|
|
297
|
+
const labelBase = clusterLabel(baseSubsystem);
|
|
298
|
+
const label = group.subsystem.endsWith("/residual")
|
|
299
|
+
? `${labelBase} Misc`
|
|
300
|
+
: labelBase;
|
|
301
|
+
result.push({
|
|
302
|
+
id: `cluster-${hash(`${group.subsystem}:${group.files.join("|")}`)}`,
|
|
303
|
+
label,
|
|
304
|
+
subsystem: baseSubsystem,
|
|
305
|
+
files: group.files,
|
|
306
|
+
size: group.files.length,
|
|
307
|
+
internalWeight,
|
|
308
|
+
externalWeight,
|
|
309
|
+
cohesion: total > 0
|
|
310
|
+
? Number((internalWeight /
|
|
311
|
+
total).toFixed(4))
|
|
312
|
+
: 0,
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
return result
|
|
316
|
+
.sort((a, b) => b.size -
|
|
317
|
+
a.size ||
|
|
318
|
+
b.cohesion -
|
|
319
|
+
a.cohesion ||
|
|
320
|
+
a.label.localeCompare(b.label));
|
|
321
|
+
}
|
|
322
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
function normalize(value) {
|
|
2
|
+
return value
|
|
3
|
+
.replaceAll("\\", "/")
|
|
4
|
+
.toLowerCase();
|
|
5
|
+
}
|
|
6
|
+
export class EntryPointDetector {
|
|
7
|
+
graph;
|
|
8
|
+
constructor(graph) {
|
|
9
|
+
this.graph = graph;
|
|
10
|
+
}
|
|
11
|
+
detect(projectId) {
|
|
12
|
+
const symbols = this.graph.allSymbols(projectId);
|
|
13
|
+
const edges = this.graph.allEdges(projectId);
|
|
14
|
+
const results = [];
|
|
15
|
+
for (const symbol of symbols) {
|
|
16
|
+
/*
|
|
17
|
+
* Route là entry semantic.
|
|
18
|
+
* Các loại còn lại chỉ xét FILE node.
|
|
19
|
+
*/
|
|
20
|
+
if (symbol.type !== "route" &&
|
|
21
|
+
symbol.type !== "file") {
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const result = this.classify(symbol, edges);
|
|
25
|
+
if (result) {
|
|
26
|
+
results.push({
|
|
27
|
+
symbolId: symbol.id,
|
|
28
|
+
filePath: symbol.filePath,
|
|
29
|
+
name: symbol.name,
|
|
30
|
+
kind: result.kind,
|
|
31
|
+
score: result.score,
|
|
32
|
+
reasons: result.reasons,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/*
|
|
37
|
+
* Một file chỉ giữ entry tốt nhất.
|
|
38
|
+
* Route vẫn giữ riêng theo symbol.
|
|
39
|
+
*/
|
|
40
|
+
const bestByKey = new Map();
|
|
41
|
+
for (const item of results) {
|
|
42
|
+
const key = item.kind === "route"
|
|
43
|
+
? item.symbolId
|
|
44
|
+
: item.filePath;
|
|
45
|
+
const existing = bestByKey.get(key);
|
|
46
|
+
if (!existing ||
|
|
47
|
+
item.score >
|
|
48
|
+
existing.score) {
|
|
49
|
+
bestByKey.set(key, item);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return [
|
|
53
|
+
...bestByKey.values(),
|
|
54
|
+
]
|
|
55
|
+
.sort((a, b) => b.score -
|
|
56
|
+
a.score ||
|
|
57
|
+
a.filePath.localeCompare(b.filePath));
|
|
58
|
+
}
|
|
59
|
+
classify(symbol, edges) {
|
|
60
|
+
const path = normalize(symbol.filePath);
|
|
61
|
+
const reasons = [];
|
|
62
|
+
if (symbol.type ===
|
|
63
|
+
"route") {
|
|
64
|
+
return {
|
|
65
|
+
kind: "route",
|
|
66
|
+
score: 100,
|
|
67
|
+
reasons: [
|
|
68
|
+
"HTTP route",
|
|
69
|
+
],
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
let kind = null;
|
|
73
|
+
let score = 0;
|
|
74
|
+
if (path.startsWith("bin/")) {
|
|
75
|
+
kind =
|
|
76
|
+
"cli";
|
|
77
|
+
score =
|
|
78
|
+
100;
|
|
79
|
+
reasons.push("bin executable");
|
|
80
|
+
}
|
|
81
|
+
else if (/(^|\/)(cli)\.[^.]+$/
|
|
82
|
+
.test(path)) {
|
|
83
|
+
kind =
|
|
84
|
+
"cli";
|
|
85
|
+
score =
|
|
86
|
+
90;
|
|
87
|
+
reasons.push("CLI entry file");
|
|
88
|
+
}
|
|
89
|
+
else if (/(^|\/)(main|server)\.[^.]+$/
|
|
90
|
+
.test(path)) {
|
|
91
|
+
kind =
|
|
92
|
+
"main";
|
|
93
|
+
score =
|
|
94
|
+
90;
|
|
95
|
+
reasons.push("main/server file");
|
|
96
|
+
}
|
|
97
|
+
else if (path.includes("/runtime/") &&
|
|
98
|
+
/(^|\/)(bootstrap|index)\.[^.]+$/
|
|
99
|
+
.test(path)) {
|
|
100
|
+
kind =
|
|
101
|
+
"runtime";
|
|
102
|
+
score =
|
|
103
|
+
75;
|
|
104
|
+
reasons.push("runtime bootstrap");
|
|
105
|
+
}
|
|
106
|
+
else if (/(^|\/)index\.[^.]+$/
|
|
107
|
+
.test(path)) {
|
|
108
|
+
/*
|
|
109
|
+
* Chỉ package/public index có tín hiệu
|
|
110
|
+
* dependency thực sự.
|
|
111
|
+
*/
|
|
112
|
+
const incoming = edges.filter((edge) => edge.to ===
|
|
113
|
+
symbol.id &&
|
|
114
|
+
edge.type !==
|
|
115
|
+
"DEFINES").length;
|
|
116
|
+
const outgoing = edges.filter((edge) => edge.from ===
|
|
117
|
+
symbol.id &&
|
|
118
|
+
edge.type !==
|
|
119
|
+
"DEFINES").length;
|
|
120
|
+
if (path.startsWith("packages/") ||
|
|
121
|
+
incoming === 0 ||
|
|
122
|
+
outgoing > 0) {
|
|
123
|
+
kind =
|
|
124
|
+
"package";
|
|
125
|
+
score =
|
|
126
|
+
path.startsWith("packages/")
|
|
127
|
+
? 70
|
|
128
|
+
: 55;
|
|
129
|
+
reasons.push("package/public index");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (!kind) {
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
const incoming = edges.filter((edge) => edge.to ===
|
|
136
|
+
symbol.id &&
|
|
137
|
+
edge.type !==
|
|
138
|
+
"DEFINES").length;
|
|
139
|
+
if (incoming === 0) {
|
|
140
|
+
score +=
|
|
141
|
+
10;
|
|
142
|
+
reasons.push("no incoming dependency");
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
kind,
|
|
146
|
+
score,
|
|
147
|
+
reasons,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
const EDGE_WEIGHT = {
|
|
2
|
+
CALL_REFERENCE: 6,
|
|
3
|
+
CALLS: 4,
|
|
4
|
+
IMPORTS: 3,
|
|
5
|
+
USES_TYPE: 4,
|
|
6
|
+
WRITES: 5,
|
|
7
|
+
ROUTE: 4,
|
|
8
|
+
TESTS: 1,
|
|
9
|
+
INHERITS: 5,
|
|
10
|
+
IMPLEMENTS: 5,
|
|
11
|
+
DEFINES: 0.25,
|
|
12
|
+
};
|
|
13
|
+
export class HotspotAnalyzer {
|
|
14
|
+
graph;
|
|
15
|
+
constructor(graph) {
|
|
16
|
+
this.graph = graph;
|
|
17
|
+
}
|
|
18
|
+
analyze(projectId) {
|
|
19
|
+
const symbols = this.graph.allSymbols(projectId);
|
|
20
|
+
const edges = this.graph.allEdges(projectId);
|
|
21
|
+
const symbolById = new Map(symbols.map((symbol) => [
|
|
22
|
+
symbol.id,
|
|
23
|
+
symbol,
|
|
24
|
+
]));
|
|
25
|
+
const files = new Map();
|
|
26
|
+
const get = (filePath) => {
|
|
27
|
+
let value = files.get(filePath);
|
|
28
|
+
if (!value) {
|
|
29
|
+
value = {
|
|
30
|
+
symbols: 0,
|
|
31
|
+
incoming: 0,
|
|
32
|
+
outgoing: 0,
|
|
33
|
+
weightedIncoming: 0,
|
|
34
|
+
weightedOutgoing: 0,
|
|
35
|
+
writes: 0,
|
|
36
|
+
callReferences: 0,
|
|
37
|
+
crossFile: 0,
|
|
38
|
+
};
|
|
39
|
+
files.set(filePath, value);
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
};
|
|
43
|
+
for (const symbol of symbols) {
|
|
44
|
+
get(symbol.filePath).symbols++;
|
|
45
|
+
}
|
|
46
|
+
for (const edge of edges) {
|
|
47
|
+
const from = symbolById.get(edge.from);
|
|
48
|
+
const to = symbolById.get(edge.to);
|
|
49
|
+
if (!from ||
|
|
50
|
+
!to) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const weight = EDGE_WEIGHT[edge.type] ?? 1;
|
|
54
|
+
const source = get(from.filePath);
|
|
55
|
+
const target = get(to.filePath);
|
|
56
|
+
source.outgoing++;
|
|
57
|
+
target.incoming++;
|
|
58
|
+
source.weightedOutgoing +=
|
|
59
|
+
weight;
|
|
60
|
+
target.weightedIncoming +=
|
|
61
|
+
weight;
|
|
62
|
+
if (edge.type ===
|
|
63
|
+
"WRITES") {
|
|
64
|
+
source.writes++;
|
|
65
|
+
}
|
|
66
|
+
if (edge.type ===
|
|
67
|
+
"CALL_REFERENCE") {
|
|
68
|
+
source.callReferences++;
|
|
69
|
+
}
|
|
70
|
+
if (from.filePath !==
|
|
71
|
+
to.filePath) {
|
|
72
|
+
source.crossFile++;
|
|
73
|
+
target.crossFile++;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const scored = [
|
|
77
|
+
...files.entries(),
|
|
78
|
+
]
|
|
79
|
+
.map(([filePath, value,]) => {
|
|
80
|
+
const score = Number((value.symbols *
|
|
81
|
+
0.4 +
|
|
82
|
+
value.weightedIncoming *
|
|
83
|
+
1.75 +
|
|
84
|
+
value.weightedOutgoing *
|
|
85
|
+
0.65 +
|
|
86
|
+
value.crossFile *
|
|
87
|
+
1.5 +
|
|
88
|
+
value.writes *
|
|
89
|
+
3 +
|
|
90
|
+
value.callReferences *
|
|
91
|
+
2).toFixed(2));
|
|
92
|
+
const reasons = [];
|
|
93
|
+
if (value.incoming >=
|
|
94
|
+
10) {
|
|
95
|
+
reasons.push("high incoming dependency");
|
|
96
|
+
}
|
|
97
|
+
if (value.crossFile >=
|
|
98
|
+
10) {
|
|
99
|
+
reasons.push("high cross-file coupling");
|
|
100
|
+
}
|
|
101
|
+
if (value.callReferences >=
|
|
102
|
+
5) {
|
|
103
|
+
reasons.push("many exact call references");
|
|
104
|
+
}
|
|
105
|
+
if (value.writes > 0) {
|
|
106
|
+
reasons.push("contains write relationships");
|
|
107
|
+
}
|
|
108
|
+
if (value.symbols >=
|
|
109
|
+
10) {
|
|
110
|
+
reasons.push("high symbol density");
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
filePath,
|
|
114
|
+
score,
|
|
115
|
+
symbols: value.symbols,
|
|
116
|
+
incoming: value.incoming,
|
|
117
|
+
outgoing: value.outgoing,
|
|
118
|
+
reasons,
|
|
119
|
+
};
|
|
120
|
+
})
|
|
121
|
+
.filter((item) => item.score > 0)
|
|
122
|
+
.sort((a, b) => b.score -
|
|
123
|
+
a.score ||
|
|
124
|
+
a.filePath.localeCompare(b.filePath));
|
|
125
|
+
if (scored.length === 0) {
|
|
126
|
+
return [];
|
|
127
|
+
}
|
|
128
|
+
/*
|
|
129
|
+
* Top 15%, tối đa 30.
|
|
130
|
+
* Repo nhỏ vẫn giữ tối thiểu 1 hotspot.
|
|
131
|
+
*/
|
|
132
|
+
const limit = Math.max(1, Math.min(30, Math.ceil(scored.length *
|
|
133
|
+
0.15)));
|
|
134
|
+
return scored.slice(0, limit);
|
|
135
|
+
}
|
|
136
|
+
}
|