@backbencher/brain 0.0.0-stage → 0.1.1
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/CHANGELOG.md +298 -0
- package/EMBEDDINGS.md +355 -0
- package/LICENSE +21 -0
- package/README.md +415 -2
- package/bin/bb +13 -0
- package/dist/analysis-exclusions-7ZJDXOVZ.js +35 -0
- package/dist/brain-index-health-Q5APTJQL.js +17 -0
- package/dist/brain-relationship-enrichment-MYR2AM4F.js +558 -0
- package/dist/brain.sql-EIM4WS4D.js +29 -0
- package/dist/chunk-22V37YTK.js +1758 -0
- package/dist/chunk-2BSKD7O3.js +40 -0
- package/dist/chunk-2DE27DVC.js +1314 -0
- package/dist/chunk-2RRBMNBQ.js +322 -0
- package/dist/chunk-2WXYNLBA.js +29 -0
- package/dist/chunk-3CTFIJJB.js +17 -0
- package/dist/chunk-3ZTOHHSW.js +2104 -0
- package/dist/chunk-4H22TLOT.js +543 -0
- package/dist/chunk-5BVAH26D.js +340 -0
- package/dist/chunk-62DHLYW6.js +3427 -0
- package/dist/chunk-7AN7U5IZ.js +371 -0
- package/dist/chunk-7DRPNVIN.js +782 -0
- package/dist/chunk-7MBYT777.js +174 -0
- package/dist/chunk-AHDWGM7Q.js +236 -0
- package/dist/chunk-B5GEAAYF.js +818 -0
- package/dist/chunk-BEIZZ73B.js +1479 -0
- package/dist/chunk-BQ3OVQ3P.js +125 -0
- package/dist/chunk-D2Q4Y47A.js +53 -0
- package/dist/chunk-DIYAGQJL.js +34 -0
- package/dist/chunk-DWKXZ6IF.js +336 -0
- package/dist/chunk-DYM4FQEE.js +90 -0
- package/dist/chunk-ENW4BTBA.js +930 -0
- package/dist/chunk-EWMWK7WN.js +75 -0
- package/dist/chunk-EY5ZZUUA.js +626 -0
- package/dist/chunk-GBI7V7KY.js +69 -0
- package/dist/chunk-GEFFO6O2.js +152 -0
- package/dist/chunk-HJEWO5EV.js +247 -0
- package/dist/chunk-HQSPJDHI.js +613 -0
- package/dist/chunk-KAET47ZL.js +125 -0
- package/dist/chunk-KCDECRJY.js +440 -0
- package/dist/chunk-L5TDYKYK.js +76 -0
- package/dist/chunk-MLKGABMK.js +9 -0
- package/dist/chunk-NO3U3FJQ.js +201 -0
- package/dist/chunk-OWISBAOF.js +61 -0
- package/dist/chunk-QKTRECYS.js +1119 -0
- package/dist/chunk-QLK6ED4Q.js +235 -0
- package/dist/chunk-R2GW4F6R.js +19 -0
- package/dist/chunk-RCMTFSEF.js +87 -0
- package/dist/chunk-RM3PY6TG.js +376 -0
- package/dist/chunk-ROMM6KMC.js +433 -0
- package/dist/chunk-SGGCSHAX.js +12050 -0
- package/dist/chunk-SK6YVHFZ.js +211 -0
- package/dist/chunk-SM7RFQRO.js +214 -0
- package/dist/chunk-UGR4QIGS.js +67 -0
- package/dist/chunk-UQNQZXHY.js +711 -0
- package/dist/chunk-UUNHHX4M.js +13 -0
- package/dist/chunk-VL52PL2T.js +71 -0
- package/dist/chunk-WIG36E67.js +196 -0
- package/dist/chunk-XHK3WWGB.js +7436 -0
- package/dist/chunk-XTQM2IP3.js +2863 -0
- package/dist/chunk-ZE2LG76D.js +1296 -0
- package/dist/chunk-ZETTGPAW.js +6950 -0
- package/dist/cli/index.js +37 -0
- package/dist/config-HZAAYDP3.js +10 -0
- package/dist/content-search-7XIP555Q.js +593 -0
- package/dist/database-76RP44JG.js +9 -0
- package/dist/doctor-ANGETYMN.js +395 -0
- package/dist/edit-engine-CG72LDZD.js +51 -0
- package/dist/engine-BKEOGN7N.js +11 -0
- package/dist/engine-VIW6NSKO.js +18 -0
- package/dist/fingerprint-OBLIMZLP.js +33 -0
- package/dist/framework-intelligence-3LORTPOX.js +860 -0
- package/dist/freshness-3K6VU5OT.js +174 -0
- package/dist/graph-engine-HBSQNPTB.js +21 -0
- package/dist/graph_request-Q6NKB6N2.js +350 -0
- package/dist/impact_request-LYP672WE.js +68 -0
- package/dist/index-space-IUUN5WYV.js +15 -0
- package/dist/index-status-7OVGYSGW.js +34 -0
- package/dist/index.js +159 -0
- package/dist/instance-FO6QZRLH.js +19 -0
- package/dist/language-detection-DFCAU5DZ.js +11 -0
- package/dist/lsp/server.js +4301 -0
- package/dist/mcp/server.js +132 -0
- package/dist/onboard-DIQZV2NW.js +161 -0
- package/dist/override-companion-pass-XR7MHTP7.js +117 -0
- package/dist/parallel-indexer-FK2XKZXY.js +88 -0
- package/dist/pool-worker.js +1153 -0
- package/dist/project-health-6XPENVAA.js +94 -0
- package/dist/project-index-5SDPHITE.js +57 -0
- package/dist/project-registry-ZJ3DVUGO.js +355 -0
- package/dist/real-bash-analyzer-EJAU5LW5.js +2201 -0
- package/dist/real-c-analyzer-HBD3FNNH.js +5942 -0
- package/dist/real-clean-analyzer-4EEZLPV5.js +3470 -0
- package/dist/real-clojure-analyzer-Q55EBOO2.js +2818 -0
- package/dist/real-cpp-analyzer-IVVOD25R.js +4101 -0
- package/dist/real-csharp-analyzer-ZBNLNJZJ.js +3298 -0
- package/dist/real-css-analyzer-6XRQLUN7.js +15 -0
- package/dist/real-dart-analyzer-OLR72EFW.js +3944 -0
- package/dist/real-elixir-analyzer-MQWGAH2I.js +2192 -0
- package/dist/real-erlang-analyzer-7BVPQEHO.js +1731 -0
- package/dist/real-fsharp-analyzer-C2UPFNOA.js +2390 -0
- package/dist/real-go-analyzer-UKGI43YS.js +4126 -0
- package/dist/real-graphql-analyzer-VE2EEFII.js +1442 -0
- package/dist/real-groovy-analyzer-W26VACFW.js +2285 -0
- package/dist/real-haskell-analyzer-5CVVAJRX.js +2533 -0
- package/dist/real-hcl-analyzer-5ZT4U43N.js +725 -0
- package/dist/real-html-analyzer-JWNX3C6I.js +1759 -0
- package/dist/real-java-analyzer-GU42VBGQ.js +4677 -0
- package/dist/real-javascript-analyzer-QA4QOJ6W.js +14 -0
- package/dist/real-json-analyzer-MPA6PRXL.js +1640 -0
- package/dist/real-julia-analyzer-22CWYQMN.js +2085 -0
- package/dist/real-kotlin-analyzer-QDZJLXXU.js +3956 -0
- package/dist/real-lua-analyzer-G7NHLYKS.js +1957 -0
- package/dist/real-makefile-analyzer-OCY3GT5N.js +920 -0
- package/dist/real-markdown-analyzer-TEJK2JXJ.js +1215 -0
- package/dist/real-matlab-analyzer-Q5SWLN7Z.js +480 -0
- package/dist/real-nim-analyzer-YLXSK563.js +3603 -0
- package/dist/real-objective-c-analyzer-D23ACC2R.js +1518 -0
- package/dist/real-perl-analyzer-WWTRHYDT.js +1865 -0
- package/dist/real-php-analyzer-JNKJHTCL.js +9368 -0
- package/dist/real-powershell-analyzer-XRSNQERV.js +2915 -0
- package/dist/real-protobuf-analyzer-3OKUCZ7T.js +867 -0
- package/dist/real-python-analyzer-VXAFWI72.js +4880 -0
- package/dist/real-r-analyzer-ZGAIDVI4.js +2596 -0
- package/dist/real-ruby-analyzer-X2KTLVBH.js +2429 -0
- package/dist/real-rust-analyzer-OFFHPL2A.js +3359 -0
- package/dist/real-scala-analyzer-TSTW27J6.js +3293 -0
- package/dist/real-scss-analyzer-AVWYPSOQ.js +1515 -0
- package/dist/real-solidity-analyzer-UCI2KWUB.js +2608 -0
- package/dist/real-sql-analyzer-F77ZMQXR.js +3840 -0
- package/dist/real-svelte-analyzer-SF7NRUO5.js +2532 -0
- package/dist/real-swift-analyzer-MN5QT3QN.js +2311 -0
- package/dist/real-toml-analyzer-66L7BDCQ.js +929 -0
- package/dist/real-typescript-analyzer-6KEMVDWQ.js +14 -0
- package/dist/real-vb-analyzer-VICVPOP7.js +3145 -0
- package/dist/real-vba-analyzer-HXLTTHKM.js +466 -0
- package/dist/real-vue-analyzer-QXR56CFV.js +2680 -0
- package/dist/real-xml-analyzer-YL465ZJW.js +2405 -0
- package/dist/real-yaml-analyzer-SUBCBNSI.js +3528 -0
- package/dist/real-zig-analyzer-HIQLZMGY.js +2430 -0
- package/dist/regex-search-NJKHMZNO.js +239 -0
- package/dist/rules-ACHPB7QU.js +152 -0
- package/dist/scoped-path-MJYWJZQX.js +11 -0
- package/dist/setup-show-NT5ZHYNE.js +15 -0
- package/dist/setup-wizard-G5YYR5T6.js +24 -0
- package/dist/symbol-search-GZJIM2J2.js +16 -0
- package/dist/taint-KXUHHPIE.js +202 -0
- package/dist/taint-analyzer-N7YLZU3N.js +163 -0
- package/dist/tool-handlers-OJDC35DI.js +72 -0
- package/dist/tool-registry-BPEJNSZG.js +17 -0
- package/dist/tool-validation-Q57YMMBX.js +32 -0
- package/dist/type-inference-QMT2RMLV.js +13 -0
- package/dist/vector-binary-LMY4QGAN.js +19 -0
- package/dist/version-ABP2TUL2.js +9 -0
- package/dist/wasm-NMWO3WXY.js +16 -0
- package/docs/EMBEDDINGS.md +132 -0
- package/docs/README.md +178 -0
- package/docs/data/m10-baseline.json +602 -0
- package/docs/data/m10-benchmark-baseline.json +57 -0
- package/docs/data/m10-benchmark-last-run.json +127 -0
- package/docs/data/m10-final-audit.json +215 -0
- package/docs/data/m12-final-audit.json +138 -0
- package/docs/data/m12-surface-matrix.json +50 -0
- package/docs/data/public-tools.json +1902 -0
- package/docs/indexing/DEAD-ASSETS.md +88 -0
- package/docs/indexing/INDEXER-REVIEW.md +634 -0
- package/docs/indexing/PERFORMANCE-FINDINGS.md +106 -0
- package/docs/indexing/README.md +427 -0
- package/docs/indexing/discovery-matrix.md +41 -0
- package/docs/indexing/embeddings.md +325 -0
- package/docs/indexing/environment-variables.md +77 -0
- package/docs/indexing/performance.md +323 -0
- package/docs/indexing/resume.md +345 -0
- package/docs/indexing/troubleshooting.md +391 -0
- package/docs/man/bb.1 +190 -0
- package/docs/tools/bb_analyze.md +147 -0
- package/docs/tools/bb_annotate.md +110 -0
- package/docs/tools/bb_annotations.md +106 -0
- package/docs/tools/bb_ask.md +101 -0
- package/docs/tools/bb_cross_language.md +103 -0
- package/docs/tools/bb_doc.md +169 -0
- package/docs/tools/bb_edit.md +185 -0
- package/docs/tools/bb_graph.md +169 -0
- package/docs/tools/bb_health.md +156 -0
- package/docs/tools/bb_impact.md +117 -0
- package/docs/tools/bb_index.md +108 -0
- package/docs/tools/bb_migrate.md +135 -0
- package/docs/tools/bb_patterns.md +140 -0
- package/docs/tools/bb_search.md +150 -0
- package/docs/tools/bb_security.md +100 -0
- package/docs/tools/bb_visualize.md +184 -0
- package/grammars/README.md +57 -0
- package/grammars/THIRD-PARTY-NOTICES.md +50 -0
- package/grammars/tree-sitter-c/LICENSE +21 -0
- package/grammars/tree-sitter-clojure/LICENSE.md +21 -0
- package/grammars/tree-sitter-cpp/LICENSE +21 -0
- package/grammars/tree-sitter-csharp/LICENSE +21 -0
- package/grammars/tree-sitter-css/LICENSE +21 -0
- package/grammars/tree-sitter-css/README.md +21 -0
- package/grammars/tree-sitter-dart/LICENSE +20 -0
- package/grammars/tree-sitter-dart-upstream/LICENSE +20 -0
- package/grammars/tree-sitter-dart-upstream/README.md +18 -0
- package/grammars/tree-sitter-elixir/LICENSE +176 -0
- package/grammars/tree-sitter-elixir/README.md +11 -0
- package/grammars/tree-sitter-fsharp/LICENSE +21 -0
- package/grammars/tree-sitter-fsharp/README.md +45 -0
- package/grammars/tree-sitter-graphql/LICENSE +21 -0
- package/grammars/tree-sitter-graphql/README.md +106 -0
- package/grammars/tree-sitter-groovy/LICENSE +21 -0
- package/grammars/tree-sitter-groovy/README.md +4 -0
- package/grammars/tree-sitter-haskell/LICENSE +21 -0
- package/grammars/tree-sitter-haskell/README.md +394 -0
- package/grammars/tree-sitter-hcl/CHANGELOG.md +102 -0
- package/grammars/tree-sitter-hcl/LICENSE +201 -0
- package/grammars/tree-sitter-hcl/README.md +39 -0
- package/grammars/tree-sitter-html/LICENSE +21 -0
- package/grammars/tree-sitter-html/README.md +21 -0
- package/grammars/tree-sitter-julia/LICENSE +21 -0
- package/grammars/tree-sitter-julia/README.md +24 -0
- package/grammars/tree-sitter-kotlin/LICENSE +9 -0
- package/grammars/tree-sitter-lua/LICENSE.md +21 -0
- package/grammars/tree-sitter-lua/README.md +29 -0
- package/grammars/tree-sitter-make/LICENSE +21 -0
- package/grammars/tree-sitter-matlab/LICENSE +19 -0
- package/grammars/tree-sitter-matlab/README.md +99 -0
- package/grammars/tree-sitter-nim/LICENSE +373 -0
- package/grammars/tree-sitter-nim/README.md +46 -0
- package/grammars/tree-sitter-objc/LICENSE +21 -0
- package/grammars/tree-sitter-objc/README.md +20 -0
- package/grammars/tree-sitter-perl/LICENSE +7 -0
- package/grammars/tree-sitter-perl/README.md +100 -0
- package/grammars/tree-sitter-powershell/LICENSE +21 -0
- package/grammars/tree-sitter-powershell/README.md +7 -0
- package/grammars/tree-sitter-proto/LICENSE +21 -0
- package/grammars/tree-sitter-proto/README.md +105 -0
- package/grammars/tree-sitter-r/LICENSE +9 -0
- package/grammars/tree-sitter-r/README.md +66 -0
- package/grammars/tree-sitter-ruby/LICENSE +22 -0
- package/grammars/tree-sitter-scss/LICENSE +21 -0
- package/grammars/tree-sitter-scss/README.md +17 -0
- package/grammars/tree-sitter-solidity/LICENSE +7 -0
- package/grammars/tree-sitter-sql/LICENSE +21 -0
- package/grammars/tree-sitter-typescript/LICENSE +21 -0
- package/grammars/tree-sitter-vba/LICENSE +21 -0
- package/grammars/tree-sitter-vba/README.md +738 -0
- package/grammars/tree-sitter-vbnet/LICENSE +21 -0
- package/grammars/tree-sitter-vbnet/README.md +322 -0
- package/grammars/tree-sitter-xml/LICENSE +19 -0
- package/grammars/tree-sitter-yaml/LICENSE +20 -0
- package/grammars/tree-sitter-yaml/README.md +21 -0
- package/package.json +184 -4
- package/wasm/dart.wasm +0 -0
- package/wasm/tree-sitter-bash.wasm +0 -0
- package/wasm/tree-sitter-c.wasm +0 -0
- package/wasm/tree-sitter-c_sharp.wasm +0 -0
- package/wasm/tree-sitter-clean.wasm +0 -0
- package/wasm/tree-sitter-clojure.wasm +0 -0
- package/wasm/tree-sitter-core_schema.wasm +0 -0
- package/wasm/tree-sitter-cpp.wasm +0 -0
- package/wasm/tree-sitter-css.wasm +0 -0
- package/wasm/tree-sitter-dart.wasm +0 -0
- package/wasm/tree-sitter-elisp.wasm +0 -0
- package/wasm/tree-sitter-elixir.wasm +0 -0
- package/wasm/tree-sitter-elm.wasm +0 -0
- package/wasm/tree-sitter-embedded_template.wasm +0 -0
- package/wasm/tree-sitter-erlang.wasm +0 -0
- package/wasm/tree-sitter-fsharp.wasm +0 -0
- package/wasm/tree-sitter-fsharp_signature.wasm +0 -0
- package/wasm/tree-sitter-go.wasm +0 -0
- package/wasm/tree-sitter-graphql.wasm +0 -0
- package/wasm/tree-sitter-groovy.wasm +0 -0
- package/wasm/tree-sitter-haskell.wasm +0 -0
- package/wasm/tree-sitter-hcl.wasm +0 -0
- package/wasm/tree-sitter-html.wasm +0 -0
- package/wasm/tree-sitter-java.wasm +0 -0
- package/wasm/tree-sitter-javascript.wasm +0 -0
- package/wasm/tree-sitter-json.wasm +0 -0
- package/wasm/tree-sitter-json_schema.wasm +0 -0
- package/wasm/tree-sitter-julia.wasm +0 -0
- package/wasm/tree-sitter-kotlin.wasm +0 -0
- package/wasm/tree-sitter-lua.wasm +0 -0
- package/wasm/tree-sitter-make.wasm +0 -0
- package/wasm/tree-sitter-markdown.wasm +0 -0
- package/wasm/tree-sitter-markdown_inline.wasm +0 -0
- package/wasm/tree-sitter-matlab.wasm +0 -0
- package/wasm/tree-sitter-nim.wasm +0 -0
- package/wasm/tree-sitter-objc.wasm +0 -0
- package/wasm/tree-sitter-ocaml.wasm +0 -0
- package/wasm/tree-sitter-perl.wasm +0 -0
- package/wasm/tree-sitter-php.wasm +0 -0
- package/wasm/tree-sitter-php_only.wasm +0 -0
- package/wasm/tree-sitter-powershell.wasm +0 -0
- package/wasm/tree-sitter-protobuf.wasm +0 -0
- package/wasm/tree-sitter-python.wasm +0 -0
- package/wasm/tree-sitter-ql.wasm +0 -0
- package/wasm/tree-sitter-r.wasm +0 -0
- package/wasm/tree-sitter-rescript.wasm +0 -0
- package/wasm/tree-sitter-ruby.wasm +0 -0
- package/wasm/tree-sitter-rust.wasm +0 -0
- package/wasm/tree-sitter-scala.wasm +0 -0
- package/wasm/tree-sitter-scss.wasm +0 -0
- package/wasm/tree-sitter-solidity.wasm +0 -0
- package/wasm/tree-sitter-sql.wasm +0 -0
- package/wasm/tree-sitter-svelte.wasm +0 -0
- package/wasm/tree-sitter-swift.wasm +0 -0
- package/wasm/tree-sitter-systemrdl.wasm +0 -0
- package/wasm/tree-sitter-terraform.wasm +0 -0
- package/wasm/tree-sitter-tlaplus.wasm +0 -0
- package/wasm/tree-sitter-toml.wasm +0 -0
- package/wasm/tree-sitter-tsx.wasm +0 -0
- package/wasm/tree-sitter-typescript.wasm +0 -0
- package/wasm/tree-sitter-vba.wasm +0 -0
- package/wasm/tree-sitter-vbnet.wasm +0 -0
- package/wasm/tree-sitter-vue.wasm +0 -0
- package/wasm/tree-sitter-xml.wasm +0 -0
- package/wasm/tree-sitter-yaml.wasm +0 -0
- package/wasm/tree-sitter-zig.wasm +0 -0
- package/wasm/tree-sitter.wasm +0 -0
- package/wasm/tsx.wasm +0 -0
- package/wasm/typescript.wasm +0 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BrainDatabase
|
|
3
|
+
} from "./chunk-QKTRECYS.js";
|
|
4
|
+
import {
|
|
5
|
+
create
|
|
6
|
+
} from "./chunk-BQ3OVQ3P.js";
|
|
7
|
+
import "./chunk-2BSKD7O3.js";
|
|
8
|
+
import {
|
|
9
|
+
BrainFileTable,
|
|
10
|
+
BrainProjectHealthTable,
|
|
11
|
+
BrainSymbolTable
|
|
12
|
+
} from "./chunk-ROMM6KMC.js";
|
|
13
|
+
import "./chunk-MLKGABMK.js";
|
|
14
|
+
|
|
15
|
+
// src/ast/project-health.ts
|
|
16
|
+
import { eq, sql, and, desc } from "drizzle-orm";
|
|
17
|
+
var log = create({ service: "project-health" });
|
|
18
|
+
async function computeAndStoreProjectHealth(projectId) {
|
|
19
|
+
const db = await BrainDatabase.getInstance(projectId);
|
|
20
|
+
const symbolCounts = await db.select({
|
|
21
|
+
type: BrainSymbolTable.symbol_type,
|
|
22
|
+
count: sql`count(*)`,
|
|
23
|
+
avgComplexity: sql`avg(${BrainSymbolTable.complexity})`
|
|
24
|
+
}).from(BrainSymbolTable).where(eq(BrainSymbolTable.project_id, projectId)).groupBy(BrainSymbolTable.symbol_type);
|
|
25
|
+
const langCounts = await db.select({
|
|
26
|
+
language: BrainFileTable.language,
|
|
27
|
+
count: sql`count(*)`,
|
|
28
|
+
lines: sql`sum(${BrainFileTable.line_count})`
|
|
29
|
+
}).from(BrainFileTable).where(eq(BrainFileTable.project_id, projectId)).groupBy(BrainFileTable.language);
|
|
30
|
+
const complexFunctions = await db.select({
|
|
31
|
+
name: BrainSymbolTable.name,
|
|
32
|
+
complexity: BrainSymbolTable.complexity,
|
|
33
|
+
file: BrainFileTable.relative_path
|
|
34
|
+
}).from(BrainSymbolTable).innerJoin(BrainFileTable, eq(BrainSymbolTable.file_id, BrainFileTable.id)).where(
|
|
35
|
+
and(
|
|
36
|
+
eq(BrainSymbolTable.project_id, projectId),
|
|
37
|
+
eq(BrainSymbolTable.symbol_type, "function"),
|
|
38
|
+
sql`${BrainSymbolTable.complexity} > 10`
|
|
39
|
+
)
|
|
40
|
+
).orderBy(desc(BrainSymbolTable.complexity)).limit(20);
|
|
41
|
+
const totalSymbols = symbolCounts.reduce((acc, s) => acc + (s.count || 0), 0);
|
|
42
|
+
const totalLines = langCounts.reduce((acc, l) => acc + (l.lines || 0), 0);
|
|
43
|
+
const avgComplexity = symbolCounts.find((s) => s.type === "function")?.avgComplexity || 1;
|
|
44
|
+
const complexityScore = Math.max(0, Math.min(100, 100 - avgComplexity * 5));
|
|
45
|
+
const functionCount = symbolCounts.find((s) => s.type === "function")?.count || 0;
|
|
46
|
+
const classCount = symbolCounts.find((s) => s.type === "class")?.count || 0;
|
|
47
|
+
const ratio = classCount > 0 ? functionCount / classCount : functionCount / 10;
|
|
48
|
+
const maintainabilityScore = Math.max(0, Math.min(100, 100 - Math.abs(ratio - 5) * 5));
|
|
49
|
+
const highComplexityCount = complexFunctions.length;
|
|
50
|
+
const techDebtScore = Math.max(0, 100 - highComplexityCount * 5);
|
|
51
|
+
const codeQualityScore = Math.round((complexityScore + maintainabilityScore + techDebtScore) / 3);
|
|
52
|
+
const langDist = langCounts.filter((l) => l.language).map((l) => ({
|
|
53
|
+
language: l.language,
|
|
54
|
+
files: l.count,
|
|
55
|
+
lines: l.lines,
|
|
56
|
+
percentage: totalLines > 0 ? Math.round(l.lines / totalLines * 100) : 0
|
|
57
|
+
}));
|
|
58
|
+
const bottlenecks = complexFunctions.map((f) => `${f.name} (complexity: ${f.complexity}) in ${f.file}`);
|
|
59
|
+
const existing = await db.select().from(BrainProjectHealthTable).where(eq(BrainProjectHealthTable.project_id, projectId)).limit(1);
|
|
60
|
+
const healthData = {
|
|
61
|
+
project_id: projectId,
|
|
62
|
+
complexity_score: Math.round(complexityScore),
|
|
63
|
+
maintainability_score: Math.round(maintainabilityScore),
|
|
64
|
+
technical_debt_score: Math.round(techDebtScore),
|
|
65
|
+
code_quality_score: codeQualityScore,
|
|
66
|
+
language_distribution: JSON.stringify(langDist),
|
|
67
|
+
service_metrics: JSON.stringify([]),
|
|
68
|
+
dependency_metrics: JSON.stringify({ total: totalSymbols, external: 0 }),
|
|
69
|
+
circular_dependencies: JSON.stringify([]),
|
|
70
|
+
contract_drift: JSON.stringify([]),
|
|
71
|
+
performance_bottlenecks: JSON.stringify(bottlenecks),
|
|
72
|
+
time_updated: Date.now()
|
|
73
|
+
};
|
|
74
|
+
if (existing.length > 0) {
|
|
75
|
+
await db.update(BrainProjectHealthTable).set(healthData).where(eq(BrainProjectHealthTable.project_id, projectId));
|
|
76
|
+
} else {
|
|
77
|
+
await db.insert(BrainProjectHealthTable).values({
|
|
78
|
+
...healthData,
|
|
79
|
+
time_created: Date.now()
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
log.info("project health computed", {
|
|
83
|
+
projectId,
|
|
84
|
+
complexityScore: Math.round(complexityScore),
|
|
85
|
+
maintainabilityScore: Math.round(maintainabilityScore),
|
|
86
|
+
techDebtScore: Math.round(techDebtScore),
|
|
87
|
+
codeQualityScore,
|
|
88
|
+
languages: langDist.length,
|
|
89
|
+
bottlenecks: bottlenecks.length
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
export {
|
|
93
|
+
computeAndStoreProjectHealth
|
|
94
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CLOJURE_DECLARATION_FORMS,
|
|
3
|
+
CLOJURE_DECLARATION_SPEC,
|
|
4
|
+
CPP_DECLARATION_SPEC,
|
|
5
|
+
DART_DECLARATION_SPEC,
|
|
6
|
+
FSHARP_DECLARATION_SPEC,
|
|
7
|
+
JAVASCRIPT_DECLARATION_SPEC,
|
|
8
|
+
JAVA_DECLARATION_SPEC,
|
|
9
|
+
PYTHON_DECLARATION_SPEC,
|
|
10
|
+
RUBY_DECLARATION_SPEC,
|
|
11
|
+
SCSS_DECLARATION_SPEC,
|
|
12
|
+
SOLIDITY_DECLARATION_SPEC,
|
|
13
|
+
TYPESCRIPT_DECLARATION_SPEC,
|
|
14
|
+
buildProjectIndex,
|
|
15
|
+
buildProjectIndexFromDisk,
|
|
16
|
+
buildProjectIndexStreaming,
|
|
17
|
+
buildResolutionTables,
|
|
18
|
+
clojureDeclarationName,
|
|
19
|
+
clojureExecutionHead,
|
|
20
|
+
clojureFileNamespace,
|
|
21
|
+
clojureSymName,
|
|
22
|
+
deserializeProjectIndex,
|
|
23
|
+
emptyProjectIndex,
|
|
24
|
+
enrichWithProjectIndex,
|
|
25
|
+
indexTree,
|
|
26
|
+
roundTripProjectIndex,
|
|
27
|
+
serializeProjectIndex
|
|
28
|
+
} from "./chunk-2DE27DVC.js";
|
|
29
|
+
import "./chunk-MLKGABMK.js";
|
|
30
|
+
export {
|
|
31
|
+
CLOJURE_DECLARATION_FORMS,
|
|
32
|
+
CLOJURE_DECLARATION_SPEC,
|
|
33
|
+
CPP_DECLARATION_SPEC,
|
|
34
|
+
DART_DECLARATION_SPEC,
|
|
35
|
+
FSHARP_DECLARATION_SPEC,
|
|
36
|
+
JAVASCRIPT_DECLARATION_SPEC,
|
|
37
|
+
JAVA_DECLARATION_SPEC,
|
|
38
|
+
PYTHON_DECLARATION_SPEC,
|
|
39
|
+
RUBY_DECLARATION_SPEC,
|
|
40
|
+
SCSS_DECLARATION_SPEC,
|
|
41
|
+
SOLIDITY_DECLARATION_SPEC,
|
|
42
|
+
TYPESCRIPT_DECLARATION_SPEC,
|
|
43
|
+
buildProjectIndex,
|
|
44
|
+
buildProjectIndexFromDisk,
|
|
45
|
+
buildProjectIndexStreaming,
|
|
46
|
+
buildResolutionTables,
|
|
47
|
+
clojureDeclarationName,
|
|
48
|
+
clojureExecutionHead,
|
|
49
|
+
clojureFileNamespace,
|
|
50
|
+
clojureSymName,
|
|
51
|
+
deserializeProjectIndex,
|
|
52
|
+
emptyProjectIndex,
|
|
53
|
+
enrichWithProjectIndex,
|
|
54
|
+
indexTree,
|
|
55
|
+
roundTripProjectIndex,
|
|
56
|
+
serializeProjectIndex
|
|
57
|
+
};
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import {
|
|
2
|
+
computeDbFingerprint,
|
|
3
|
+
readStoredFingerprint
|
|
4
|
+
} from "./chunk-7MBYT777.js";
|
|
5
|
+
import {
|
|
6
|
+
liveIndexLockHolder
|
|
7
|
+
} from "./chunk-ZETTGPAW.js";
|
|
8
|
+
import "./chunk-4H22TLOT.js";
|
|
9
|
+
import "./chunk-GEFFO6O2.js";
|
|
10
|
+
import {
|
|
11
|
+
clearStatus,
|
|
12
|
+
readLiveStatus,
|
|
13
|
+
writeLiveStatus
|
|
14
|
+
} from "./chunk-NO3U3FJQ.js";
|
|
15
|
+
import "./chunk-DYM4FQEE.js";
|
|
16
|
+
import {
|
|
17
|
+
BrainDatabase
|
|
18
|
+
} from "./chunk-QKTRECYS.js";
|
|
19
|
+
import "./chunk-3ZTOHHSW.js";
|
|
20
|
+
import "./chunk-BQ3OVQ3P.js";
|
|
21
|
+
import "./chunk-SK6YVHFZ.js";
|
|
22
|
+
import {
|
|
23
|
+
bbProjectsDir
|
|
24
|
+
} from "./chunk-2BSKD7O3.js";
|
|
25
|
+
import "./chunk-KAET47ZL.js";
|
|
26
|
+
import "./chunk-KCDECRJY.js";
|
|
27
|
+
import "./chunk-D2Q4Y47A.js";
|
|
28
|
+
import "./chunk-SM7RFQRO.js";
|
|
29
|
+
import "./chunk-L5TDYKYK.js";
|
|
30
|
+
import "./chunk-ROMM6KMC.js";
|
|
31
|
+
import "./chunk-MLKGABMK.js";
|
|
32
|
+
|
|
33
|
+
// src/ast/project-registry.ts
|
|
34
|
+
import { readdirSync, statSync, existsSync, rmSync, copyFileSync, mkdirSync } from "fs";
|
|
35
|
+
import { createRequire } from "module";
|
|
36
|
+
import path from "path";
|
|
37
|
+
var require2 = createRequire(import.meta.url);
|
|
38
|
+
function commonRoot(paths) {
|
|
39
|
+
if (paths.length === 0) return null;
|
|
40
|
+
let prefix = paths[0];
|
|
41
|
+
for (const p of paths.slice(1)) {
|
|
42
|
+
while (!p.startsWith(prefix)) {
|
|
43
|
+
const idx = prefix.lastIndexOf("/");
|
|
44
|
+
if (idx <= 0) return "/";
|
|
45
|
+
prefix = prefix.slice(0, idx);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return prefix;
|
|
49
|
+
}
|
|
50
|
+
function dbIndexedRoot(raw) {
|
|
51
|
+
try {
|
|
52
|
+
const rows = raw.prepare("SELECT path FROM brain_file").all();
|
|
53
|
+
const abs = rows.map((r) => String(r.path)).filter((p) => p.startsWith("/"));
|
|
54
|
+
if (abs.length === 0) return null;
|
|
55
|
+
const root = commonRoot(abs);
|
|
56
|
+
return root && root !== "/" ? root : null;
|
|
57
|
+
} catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function listProjects() {
|
|
62
|
+
const dir = bbProjectsDir();
|
|
63
|
+
if (!existsSync(dir)) return [];
|
|
64
|
+
const entries = [];
|
|
65
|
+
for (const id of readdirSync(dir)) {
|
|
66
|
+
if (!/^[0-9a-f]{40}$/.test(id)) continue;
|
|
67
|
+
const dbPath = path.join(dir, id, "brain.db");
|
|
68
|
+
if (!existsSync(dbPath)) continue;
|
|
69
|
+
let sizeBytes = 0;
|
|
70
|
+
try {
|
|
71
|
+
sizeBytes = statSync(dbPath).size;
|
|
72
|
+
} catch {
|
|
73
|
+
}
|
|
74
|
+
let files = 0, symbols = 0, lastIndexed = null, status = null, humanPath = null;
|
|
75
|
+
let unreadable = null;
|
|
76
|
+
try {
|
|
77
|
+
const row = BrainDatabase.openByPath(dbPath, { readonly: true });
|
|
78
|
+
const meta = row.prepare("SELECT total_files, total_symbols, time_updated, status FROM brain_index_status WHERE project_id = ?").get(id);
|
|
79
|
+
if (meta) {
|
|
80
|
+
files = Number(meta.total_files ?? 0);
|
|
81
|
+
symbols = Number(meta.total_symbols ?? 0);
|
|
82
|
+
lastIndexed = meta.time_updated ? Number(meta.time_updated) : null;
|
|
83
|
+
status = meta.status ?? null;
|
|
84
|
+
if (files === 0 && symbols === 0) {
|
|
85
|
+
files = Number(row.prepare("SELECT COUNT(*) c FROM brain_file").get()?.c ?? 0);
|
|
86
|
+
symbols = Number(row.prepare("SELECT COUNT(*) c FROM brain_symbol").get()?.c ?? 0);
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
files = Number(row.prepare("SELECT COUNT(*) c FROM brain_file").get()?.c ?? 0);
|
|
90
|
+
symbols = Number(row.prepare("SELECT COUNT(*) c FROM brain_symbol").get()?.c ?? 0);
|
|
91
|
+
}
|
|
92
|
+
const sample = row.prepare("SELECT path FROM brain_file LIMIT 300").all().map((r) => String(r.path));
|
|
93
|
+
humanPath = commonRoot(sample);
|
|
94
|
+
} catch (error) {
|
|
95
|
+
unreadable = String(error?.message ?? error).slice(0, 200);
|
|
96
|
+
}
|
|
97
|
+
entries.push({ id, dbPath, dbSizeBytes: sizeBytes, files, symbols, lastIndexed, status, humanPath, unreadable });
|
|
98
|
+
}
|
|
99
|
+
return entries;
|
|
100
|
+
}
|
|
101
|
+
function resolveProjectId(prefix) {
|
|
102
|
+
const p = prefix.trim().toLowerCase();
|
|
103
|
+
if (!/^[0-9a-f]{1,40}$/.test(p)) return null;
|
|
104
|
+
if (p.length === 40) return existsSync(path.join(bbProjectsDir(), p)) ? p : null;
|
|
105
|
+
let match = null;
|
|
106
|
+
let ambiguous = false;
|
|
107
|
+
try {
|
|
108
|
+
for (const entry of readdirSync(bbProjectsDir(), { withFileTypes: true })) {
|
|
109
|
+
if (!entry.isDirectory()) continue;
|
|
110
|
+
if (!entry.name.startsWith(p)) continue;
|
|
111
|
+
if (match) {
|
|
112
|
+
ambiguous = true;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
match = entry.name;
|
|
116
|
+
}
|
|
117
|
+
} catch {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
return ambiguous ? null : match;
|
|
121
|
+
}
|
|
122
|
+
function removeProject(id, force) {
|
|
123
|
+
const resolved = resolveProjectId(id);
|
|
124
|
+
if (!resolved) {
|
|
125
|
+
const p = id.trim().toLowerCase();
|
|
126
|
+
if (/^[0-9a-f]{1,40}$/.test(p)) {
|
|
127
|
+
let hits = 0;
|
|
128
|
+
try {
|
|
129
|
+
for (const entry of readdirSync(bbProjectsDir(), { withFileTypes: true })) {
|
|
130
|
+
if (entry.isDirectory() && entry.name.startsWith(p)) hits++;
|
|
131
|
+
}
|
|
132
|
+
} catch {
|
|
133
|
+
}
|
|
134
|
+
if (hits > 1) {
|
|
135
|
+
return { id, removed: false, reason: `project id ${p} is ambiguous (${hits} matches) \u2014 use more characters` };
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return { id, removed: false, reason: `no project with id ${id.slice(0, 8)}` };
|
|
139
|
+
}
|
|
140
|
+
const id2 = resolved;
|
|
141
|
+
const dir = path.join(bbProjectsDir(), id2);
|
|
142
|
+
const live = readLiveStatus(id2);
|
|
143
|
+
const inFlight = !!live && live.phase === "indexing" && Date.now() - live.updatedAt < 36e5;
|
|
144
|
+
const lockHolder = liveIndexLockHolder(id2);
|
|
145
|
+
if ((inFlight || lockHolder) && !force) {
|
|
146
|
+
const reason = lockHolder ? `project is being indexed by pid ${lockHolder.pid} (index lock held); use --force to remove anyway` : `project is being indexed (in-flight); use --force to remove anyway`;
|
|
147
|
+
return { id: id2, removed: false, reason };
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
try {
|
|
151
|
+
const raw = BrainDatabase.openByPath(path.join(dir, "brain.db"));
|
|
152
|
+
const { wipeProjectData } = require2("./parallel-indexer");
|
|
153
|
+
wipeProjectData(raw);
|
|
154
|
+
raw.close();
|
|
155
|
+
} catch {
|
|
156
|
+
}
|
|
157
|
+
rmSync(dir, { recursive: true, force: true });
|
|
158
|
+
clearStatus(id2);
|
|
159
|
+
return { id: id2, removed: !existsSync(dir) };
|
|
160
|
+
} catch (e) {
|
|
161
|
+
return { id: id2, removed: false, reason: e?.message ?? String(e) };
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
function totalProjectsBytes(entries) {
|
|
165
|
+
return entries.reduce((a, e) => a + e.dbSizeBytes, 0);
|
|
166
|
+
}
|
|
167
|
+
function findAdoptableDb(checkoutFingerprint) {
|
|
168
|
+
const dir = bbProjectsDir();
|
|
169
|
+
if (!existsSync(dir)) return null;
|
|
170
|
+
for (const id of readdirSync(dir)) {
|
|
171
|
+
if (!/^[0-9a-f]{40}$/.test(id)) continue;
|
|
172
|
+
const dbPath = path.join(dir, id, "brain.db");
|
|
173
|
+
if (!existsSync(dbPath)) continue;
|
|
174
|
+
try {
|
|
175
|
+
const raw = BrainDatabase.openByPath(dbPath, { readonly: true });
|
|
176
|
+
const stored = readStoredFingerprint(raw, id);
|
|
177
|
+
let fp = stored;
|
|
178
|
+
let git = null;
|
|
179
|
+
try {
|
|
180
|
+
const row = raw.prepare("SELECT codebase_git FROM brain_index_status WHERE project_id = ?").get(id);
|
|
181
|
+
git = row?.codebase_git ?? null;
|
|
182
|
+
} catch {
|
|
183
|
+
}
|
|
184
|
+
if (!fp) {
|
|
185
|
+
try {
|
|
186
|
+
fp = computeDbFingerprint(raw, git);
|
|
187
|
+
} catch {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (fp && fp === checkoutFingerprint) {
|
|
192
|
+
const live = dbIndexedRoot(raw);
|
|
193
|
+
if (live && existsSync(live)) continue;
|
|
194
|
+
const files = Number(raw.prepare("SELECT COUNT(*) c FROM brain_file").get()?.c ?? 0);
|
|
195
|
+
return { id, dbPath, fingerprint: fp, git, files };
|
|
196
|
+
}
|
|
197
|
+
} catch {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
function adoptProject(fromId, toId, newPath) {
|
|
204
|
+
if (!/^[0-9a-f]{40}$/.test(fromId) || !/^[0-9a-f]{40}$/.test(toId)) {
|
|
205
|
+
return { adopted: false, toId, reason: "invalid project id" };
|
|
206
|
+
}
|
|
207
|
+
if (fromId === toId) {
|
|
208
|
+
return { adopted: false, toId, reason: "source and target ids are identical" };
|
|
209
|
+
}
|
|
210
|
+
const srcDb = path.join(bbProjectsDir(), fromId, "brain.db");
|
|
211
|
+
return importDbFile(srcDb, toId, newPath, { fromId, dropSource: true });
|
|
212
|
+
}
|
|
213
|
+
function importDbFile(dbFilePath, toId, newPath, opts = {}) {
|
|
214
|
+
if (!/^[0-9a-f]{40}$/.test(toId)) {
|
|
215
|
+
return { adopted: false, toId, reason: "invalid project id" };
|
|
216
|
+
}
|
|
217
|
+
const src = path.resolve(dbFilePath);
|
|
218
|
+
if (!existsSync(src)) {
|
|
219
|
+
return { adopted: false, toId, reason: `no such DB file: ${src}` };
|
|
220
|
+
}
|
|
221
|
+
const dstDir = path.join(bbProjectsDir(), toId);
|
|
222
|
+
const dstDb = path.join(dstDir, "brain.db");
|
|
223
|
+
if (existsSync(dstDir)) {
|
|
224
|
+
return { adopted: false, toId, reason: `target project ${toId.slice(0, 8)} already has an index` };
|
|
225
|
+
}
|
|
226
|
+
const fromId = opts.fromId ?? projectIdFromDbFile(src);
|
|
227
|
+
try {
|
|
228
|
+
let isBrainDb = false;
|
|
229
|
+
try {
|
|
230
|
+
const probe = BrainDatabase.openByPath(src, { readonly: true });
|
|
231
|
+
try {
|
|
232
|
+
const t = probe.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='brain_file'").get();
|
|
233
|
+
isBrainDb = !!t;
|
|
234
|
+
} finally {
|
|
235
|
+
BrainDatabase.closeByPath(src);
|
|
236
|
+
}
|
|
237
|
+
} catch {
|
|
238
|
+
BrainDatabase.closeByPath(src);
|
|
239
|
+
}
|
|
240
|
+
if (!isBrainDb) return { adopted: false, toId, reason: `not a brain DB (no brain_file table): ${src}` };
|
|
241
|
+
mkdirSync(dstDir, { recursive: true });
|
|
242
|
+
try {
|
|
243
|
+
BrainDatabase.openByPath(src).exec("PRAGMA wal_checkpoint(TRUNCATE)");
|
|
244
|
+
BrainDatabase.closeByPath(src);
|
|
245
|
+
} catch {
|
|
246
|
+
BrainDatabase.closeByPath(src);
|
|
247
|
+
for (const suffix of ["-wal", "-shm"]) {
|
|
248
|
+
if (existsSync(src + suffix)) copyFileSync(src + suffix, dstDb + suffix);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
copyFileSync(src, dstDb);
|
|
252
|
+
const raw = BrainDatabase.openByPath(dstDb);
|
|
253
|
+
if (fromId && fromId !== toId) {
|
|
254
|
+
try {
|
|
255
|
+
raw.prepare("PRAGMA foreign_keys = OFF").run();
|
|
256
|
+
} catch {
|
|
257
|
+
}
|
|
258
|
+
const projectScoped = [
|
|
259
|
+
"brain_file",
|
|
260
|
+
"brain_symbol",
|
|
261
|
+
"brain_relationship",
|
|
262
|
+
"brain_import",
|
|
263
|
+
"brain_query_cache",
|
|
264
|
+
"brain_project_health",
|
|
265
|
+
"brain_taint_flow",
|
|
266
|
+
"brain_semantic_embedding",
|
|
267
|
+
"brain_analysis_run",
|
|
268
|
+
"brain_analysis_finding",
|
|
269
|
+
"brain_analysis_occurrence",
|
|
270
|
+
"brain_index_status",
|
|
271
|
+
"brain_annotation"
|
|
272
|
+
];
|
|
273
|
+
for (const table of projectScoped) {
|
|
274
|
+
try {
|
|
275
|
+
raw.prepare(`UPDATE ${table} SET project_id = ? WHERE project_id = ?`).run(toId, fromId);
|
|
276
|
+
} catch {
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
rekeyEmbeddedIds(raw, fromId, toId, projectScoped);
|
|
280
|
+
}
|
|
281
|
+
rewriteBrainFilePaths(raw, newPath);
|
|
282
|
+
raw.close();
|
|
283
|
+
if (fromId) {
|
|
284
|
+
const live = readLiveStatus(fromId);
|
|
285
|
+
if (live) {
|
|
286
|
+
writeLiveStatus(toId, { ...live, projectId: toId });
|
|
287
|
+
clearStatus(fromId);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (opts.dropSource) {
|
|
291
|
+
rmSync(path.dirname(src), { recursive: true, force: true });
|
|
292
|
+
}
|
|
293
|
+
return { adopted: true, fromId: fromId ?? void 0, toId };
|
|
294
|
+
} catch (e) {
|
|
295
|
+
try {
|
|
296
|
+
rmSync(dstDir, { recursive: true, force: true });
|
|
297
|
+
} catch {
|
|
298
|
+
}
|
|
299
|
+
return { adopted: false, toId, reason: e?.message ?? String(e) };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function rekeyEmbeddedIds(raw, fromId, toId, tables) {
|
|
303
|
+
const isIdColumn = (name) => name !== "project_id" && (name === "id" || name === "entity_id" || name.endsWith("_id"));
|
|
304
|
+
for (const table of tables) {
|
|
305
|
+
try {
|
|
306
|
+
const cols = raw.prepare(`PRAGMA table_info(${table})`).all().map((c) => c.name).filter(isIdColumn);
|
|
307
|
+
for (const col of cols) {
|
|
308
|
+
try {
|
|
309
|
+
raw.prepare(
|
|
310
|
+
`UPDATE ${table} SET ${col} = ? || substr(${col}, length(?) + 1) WHERE ${col} LIKE ? ESCAPE '\\'`
|
|
311
|
+
).run(toId, fromId, `${fromId}:%`);
|
|
312
|
+
} catch {
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
} catch {
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function projectIdFromDbFile(dbFilePath) {
|
|
320
|
+
const parent = path.basename(path.dirname(path.resolve(dbFilePath)));
|
|
321
|
+
if (/^[0-9a-f]{40}$/.test(parent)) return parent;
|
|
322
|
+
try {
|
|
323
|
+
const probe = BrainDatabase.openByPath(dbFilePath, { readonly: true });
|
|
324
|
+
try {
|
|
325
|
+
const row = probe.prepare("SELECT project_id FROM brain_file LIMIT 1").get();
|
|
326
|
+
if (row?.project_id && /^[0-9a-f]{40}$/.test(row.project_id)) return row.project_id;
|
|
327
|
+
} finally {
|
|
328
|
+
BrainDatabase.closeByPath(dbFilePath);
|
|
329
|
+
}
|
|
330
|
+
} catch {
|
|
331
|
+
}
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
function rewriteBrainFilePaths(raw, newPath) {
|
|
335
|
+
try {
|
|
336
|
+
const sample = raw.prepare("SELECT path FROM brain_file").all().map((r) => String(r.path));
|
|
337
|
+
const oldRoot = commonRoot(sample);
|
|
338
|
+
if (oldRoot && oldRoot !== "/" && newPath) {
|
|
339
|
+
const escaped = oldRoot.replace(/[\\%_]/g, (c) => `\\${c}`) + "%";
|
|
340
|
+
raw.prepare(
|
|
341
|
+
"UPDATE brain_file SET path = ? || SUBSTR(path, LENGTH(?) + 1) WHERE path LIKE ? ESCAPE '\\'"
|
|
342
|
+
).run(newPath, oldRoot, escaped);
|
|
343
|
+
}
|
|
344
|
+
} catch {
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
export {
|
|
348
|
+
adoptProject,
|
|
349
|
+
findAdoptableDb,
|
|
350
|
+
importDbFile,
|
|
351
|
+
listProjects,
|
|
352
|
+
removeProject,
|
|
353
|
+
resolveProjectId,
|
|
354
|
+
totalProjectsBytes
|
|
355
|
+
};
|