homegraph 1.5.0 → 1.5.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.
Files changed (214) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +305 -305
  3. package/dist/bin/command-supervision.d.ts.map +1 -1
  4. package/dist/bin/command-supervision.js +7 -4
  5. package/dist/bin/command-supervision.js.map +1 -1
  6. package/dist/bin/homegraph.js +9 -9
  7. package/dist/db/index.js +36 -36
  8. package/dist/db/migrations.js +37 -37
  9. package/dist/db/queries.js +156 -156
  10. package/dist/db/schema.sql +203 -203
  11. package/dist/directory.js +5 -5
  12. package/dist/extraction/languages/arkts-viewtree.d.ts +2 -4
  13. package/dist/extraction/languages/arkts-viewtree.d.ts.map +1 -1
  14. package/dist/extraction/languages/arkts-viewtree.js +6 -21
  15. package/dist/extraction/languages/arkts-viewtree.js.map +1 -1
  16. package/dist/extraction/languages/arkts.d.ts +16 -6
  17. package/dist/extraction/languages/arkts.d.ts.map +1 -1
  18. package/dist/extraction/languages/arkts.js +174 -21
  19. package/dist/extraction/languages/arkts.js.map +1 -1
  20. package/dist/extraction/wasm/tree-sitter-c_sharp.wasm +0 -0
  21. package/dist/extraction/wasm/tree-sitter-cfml.wasm +0 -0
  22. package/dist/extraction/wasm/tree-sitter-cfquery.wasm +0 -0
  23. package/dist/extraction/wasm/tree-sitter-cfscript.wasm +0 -0
  24. package/dist/extraction/wasm/tree-sitter-cobol.wasm +0 -0
  25. package/dist/extraction/wasm/tree-sitter-erlang.wasm +0 -0
  26. package/dist/extraction/wasm/tree-sitter-nix.wasm +0 -0
  27. package/dist/extraction/wasm/tree-sitter-pascal.wasm +0 -0
  28. package/dist/extraction/wasm/tree-sitter-vbnet.wasm +0 -0
  29. package/dist/installer/instructions-template.js +9 -9
  30. package/dist/mcp/liveness-watchdog.d.ts +18 -0
  31. package/dist/mcp/liveness-watchdog.d.ts.map +1 -1
  32. package/dist/mcp/liveness-watchdog.js +185 -73
  33. package/dist/mcp/liveness-watchdog.js.map +1 -1
  34. package/dist/mcp/server-instructions.js +47 -47
  35. package/dist/reasoning/reasoner.js +32 -32
  36. package/dist/spec/db/commit-node.js +4 -4
  37. package/dist/spec/db/fragment-node.js +10 -10
  38. package/dist/spec/db/fts.js +8 -8
  39. package/dist/spec/db/relations.js +88 -88
  40. package/dist/spec/db/schema.js +6 -6
  41. package/dist/spec/db/schema.sql +121 -121
  42. package/dist/spec/db/spec-node.js +4 -4
  43. package/dist/spec/llm/prompts.js +53 -53
  44. package/dist/spec/utils.d.ts +16 -4
  45. package/dist/spec/utils.d.ts.map +1 -1
  46. package/dist/spec/utils.js +58 -6
  47. package/dist/spec/utils.js.map +1 -1
  48. package/package.json +62 -62
  49. package/scripts/_tmp-cfwk-resolve.log +0 -0
  50. package/scripts/_tmp-cfwk-sig.log +0 -0
  51. package/scripts/_tmp-cfwk-vt.log +0 -0
  52. package/scripts/add-lang/bench.sh +60 -60
  53. package/scripts/add-lang/check-grammar.mjs +75 -75
  54. package/scripts/add-lang/dump-ast.mjs +103 -103
  55. package/scripts/add-lang/verify-extraction.mjs +70 -70
  56. package/scripts/agent-eval/ab-adoption.sh +91 -91
  57. package/scripts/agent-eval/ab-hook.sh +86 -86
  58. package/scripts/agent-eval/ab-impl.sh +78 -78
  59. package/scripts/agent-eval/ab-new-vs-baseline.sh +102 -102
  60. package/scripts/agent-eval/ab-sufficiency.sh +78 -78
  61. package/scripts/agent-eval/arms-F.sh +21 -21
  62. package/scripts/agent-eval/arms-matrix.sh +37 -37
  63. package/scripts/agent-eval/audit.sh +68 -68
  64. package/scripts/agent-eval/bench-readme.sh +28 -28
  65. package/scripts/agent-eval/bench-why-repo.sh +22 -22
  66. package/scripts/agent-eval/block-read-hook.sh +19 -19
  67. package/scripts/agent-eval/hook-settings.json +15 -15
  68. package/scripts/agent-eval/itrun.sh +120 -120
  69. package/scripts/agent-eval/offload-eval-3arm.sh +72 -72
  70. package/scripts/agent-eval/offload-eval-cost.mjs +133 -133
  71. package/scripts/agent-eval/offload-eval-effort.mjs +108 -108
  72. package/scripts/agent-eval/offload-eval-frontload-matrix.sh +25 -25
  73. package/scripts/agent-eval/offload-eval-frontload.sh +47 -47
  74. package/scripts/agent-eval/offload-eval-ground-truth.json +18 -18
  75. package/scripts/agent-eval/offload-eval-hook.mjs +84 -84
  76. package/scripts/agent-eval/offload-eval-judge.mjs +103 -103
  77. package/scripts/agent-eval/offload-eval-matrix.sh +20 -20
  78. package/scripts/agent-eval/offload-eval-metrics.mjs +94 -94
  79. package/scripts/agent-eval/offload-eval-refs1.sh +50 -50
  80. package/scripts/agent-eval/offload-eval-setup.sh +24 -24
  81. package/scripts/agent-eval/offload-eval-styles.sh +71 -71
  82. package/scripts/agent-eval/offload-eval-summarize.mjs +68 -68
  83. package/scripts/agent-eval/offload-eval.md +76 -76
  84. package/scripts/agent-eval/parse-arms.mjs +116 -116
  85. package/scripts/agent-eval/parse-bench-readme.mjs +84 -84
  86. package/scripts/agent-eval/parse-run.mjs +45 -45
  87. package/scripts/agent-eval/parse-session.mjs +93 -93
  88. package/scripts/agent-eval/probe-context.mjs +21 -21
  89. package/scripts/agent-eval/probe-explore.mjs +40 -40
  90. package/scripts/agent-eval/probe-node.mjs +20 -20
  91. package/scripts/agent-eval/probe-sweep.mjs +119 -119
  92. package/scripts/agent-eval/probe-trace.mjs +20 -20
  93. package/scripts/agent-eval/redirect-read-hook.sh +38 -38
  94. package/scripts/agent-eval/repro-concurrent-explore.mjs +119 -119
  95. package/scripts/agent-eval/repro-daemon-clients.mjs +125 -125
  96. package/scripts/agent-eval/run-agent.sh +34 -34
  97. package/scripts/agent-eval/run-all.sh +75 -75
  98. package/scripts/agent-eval/run-arms.sh +56 -56
  99. package/scripts/agent-eval/seq-matrix.mjs +137 -137
  100. package/scripts/bench-arkts-init-rss.log +0 -0
  101. package/scripts/build-bundle.sh +123 -123
  102. package/scripts/exp_boundary_eval/README.md +247 -247
  103. package/scripts/exp_boundary_eval/__pycache__/_utils.cpython-310.pyc +0 -0
  104. package/scripts/exp_boundary_eval/__pycache__/_utils.cpython-38.pyc +0 -0
  105. package/scripts/exp_boundary_eval/__pycache__/analyze.cpython-310.pyc +0 -0
  106. package/scripts/exp_boundary_eval/__pycache__/analyze.cpython-38.pyc +0 -0
  107. package/scripts/exp_boundary_eval/__pycache__/deveco_arm.cpython-38.pyc +0 -0
  108. package/scripts/exp_boundary_eval/__pycache__/run_all.cpython-310.pyc +0 -0
  109. package/scripts/exp_boundary_eval/__pycache__/run_all.cpython-38.pyc +0 -0
  110. package/scripts/exp_boundary_eval/__pycache__/run_one.cpython-310.pyc +0 -0
  111. package/scripts/exp_boundary_eval/__pycache__/run_one.cpython-38.pyc +0 -0
  112. package/scripts/exp_boundary_eval/__pycache__/run_session.cpython-310.pyc +0 -0
  113. package/scripts/exp_boundary_eval/__pycache__/run_session.cpython-38.pyc +0 -0
  114. package/scripts/exp_boundary_eval/__pycache__/setup.cpython-310.pyc +0 -0
  115. package/scripts/exp_boundary_eval/__pycache__/setup.cpython-38.pyc +0 -0
  116. package/scripts/exp_boundary_eval/__pycache__/win_mcp_launcher.cpython-38.pyc +0 -0
  117. package/scripts/exp_boundary_eval/_test_mcp_chain.py +78 -78
  118. package/scripts/exp_boundary_eval/_test_stdin.py +8 -8
  119. package/scripts/exp_boundary_eval/_utils.py +1116 -1116
  120. package/scripts/exp_boundary_eval/analyze.py +1313 -1313
  121. package/scripts/exp_boundary_eval/deveco_arm.py +519 -519
  122. package/scripts/exp_boundary_eval/run_all.py +378 -378
  123. package/scripts/exp_boundary_eval/run_one.py +165 -165
  124. package/scripts/exp_boundary_eval/run_session.py +158 -158
  125. package/scripts/exp_boundary_eval/setup.py +120 -120
  126. package/scripts/exp_boundary_eval/win_mcp_launcher.py +73 -73
  127. package/scripts/exp_boundary_eval/win_mcp_stdio_wrap.js +36 -36
  128. package/scripts/exp_boundary_eval/win_node_launcher.py +24 -24
  129. package/scripts/extract-release-notes.mjs +130 -130
  130. package/scripts/local-install.sh +41 -41
  131. package/scripts/npm-sdk.js +75 -75
  132. package/scripts/npm-shim.js +275 -275
  133. package/scripts/ohos-sdk-publish.mjs +133 -133
  134. package/scripts/pack-npm.sh +119 -119
  135. package/scripts/prepare-release.mjs +270 -270
  136. package/scripts/probe-arkts-mem-why-run.log +0 -0
  137. package/scripts/probe-banner-livecard-ir.log +0 -0
  138. package/scripts/probe-cfwk-ast.stderr.log +0 -0
  139. package/scripts/probe-cfwk-ast.stdout.log +0 -0
  140. package/scripts/probe-cfwk-attr-shape.log +0 -0
  141. package/scripts/probe-cfwk-cfgdump.stderr.log +0 -0
  142. package/scripts/probe-cfwk-cfgdump.stdout.log +0 -0
  143. package/scripts/probe-cfwk-cvc.stderr.log +0 -0
  144. package/scripts/probe-cfwk-cvc.stdout.log +0 -0
  145. package/scripts/probe-cfwk-diag2.log +0 -0
  146. package/scripts/probe-cfwk-diag3.log +0 -0
  147. package/scripts/probe-cfwk-fedbg.stderr.log +0 -0
  148. package/scripts/probe-cfwk-fedbg.stdout.log +0 -0
  149. package/scripts/probe-cfwk-fileresult.log +0 -0
  150. package/scripts/probe-cfwk-fix.stderr.log +0 -0
  151. package/scripts/probe-cfwk-fix.stdout.log +0 -0
  152. package/scripts/probe-cfwk-fix2.stderr.log +0 -0
  153. package/scripts/probe-cfwk-fix2.stdout.log +0 -0
  154. package/scripts/probe-cfwk-fix3.stderr.log +0 -0
  155. package/scripts/probe-cfwk-fix3.stdout.log +0 -0
  156. package/scripts/probe-cfwk-foreach.log +0 -0
  157. package/scripts/probe-cfwk-getmethod-throw.log +0 -0
  158. package/scripts/probe-cfwk-hg-extract.log +0 -0
  159. package/scripts/probe-cfwk-pr1003-noprior.stderr.log +0 -0
  160. package/scripts/probe-cfwk-pr1003-noprior.stdout.log +0 -0
  161. package/scripts/probe-cfwk-pr1003.stderr.log +0 -0
  162. package/scripts/probe-cfwk-pr1003.stdout.log +0 -0
  163. package/scripts/probe-cfwk-preroot-noprior.stderr.log +0 -0
  164. package/scripts/probe-cfwk-preroot-noprior.stdout.log +0 -0
  165. package/scripts/probe-cfwk-preroot-prior.stderr.log +0 -0
  166. package/scripts/probe-cfwk-preroot-prior.stdout.log +0 -0
  167. package/scripts/probe-cfwk-preroot-skipstate.stderr.log +0 -0
  168. package/scripts/probe-cfwk-preroot-skipstate.stdout.log +0 -0
  169. package/scripts/probe-cfwk-resolve-sim.log +0 -0
  170. package/scripts/probe-cfwk-tree-shape.log +0 -0
  171. package/scripts/probe-cfwk-walk-abort.log +0 -0
  172. package/scripts/probe-cfwk.log +0 -0
  173. package/scripts/probe-force-index.log +0 -0
  174. package/scripts/probe-no-force-index.log +0 -0
  175. package/scripts/probe-samefile-ir.stderr.log +0 -0
  176. package/scripts/probe-samefile-ir.stdout.log +224 -0
  177. package/scripts/probe-sdk-vs-project.log +0 -0
  178. package/scripts/probe-viewtree-downgrade.log +0 -0
  179. package/dist/arkts/ohos-api-index.d.ts +0 -15
  180. package/dist/arkts/ohos-api-index.d.ts.map +0 -1
  181. package/dist/arkts/ohos-api-index.js +0 -190
  182. package/dist/arkts/ohos-api-index.js.map +0 -1
  183. package/dist/arkts/ohos-sdk-input.d.ts +0 -36
  184. package/dist/arkts/ohos-sdk-input.d.ts.map +0 -1
  185. package/dist/arkts/ohos-sdk-input.js +0 -214
  186. package/dist/arkts/ohos-sdk-input.js.map +0 -1
  187. package/dist/extraction/languages/arkts-state-decorators.d.ts +0 -13
  188. package/dist/extraction/languages/arkts-state-decorators.d.ts.map +0 -1
  189. package/dist/extraction/languages/arkts-state-decorators.js +0 -26
  190. package/dist/extraction/languages/arkts-state-decorators.js.map +0 -1
  191. package/dist/extraction/languages/ohos-api-consumer.d.ts +0 -34
  192. package/dist/extraction/languages/ohos-api-consumer.d.ts.map +0 -1
  193. package/dist/extraction/languages/ohos-api-consumer.js +0 -283
  194. package/dist/extraction/languages/ohos-api-consumer.js.map +0 -1
  195. package/dist/spec/build/git-scanner.d.ts +0 -93
  196. package/dist/spec/build/git-scanner.d.ts.map +0 -1
  197. package/dist/spec/build/git-scanner.js +0 -254
  198. package/dist/spec/build/git-scanner.js.map +0 -1
  199. package/dist/spec/git-utils.d.ts +0 -8
  200. package/dist/spec/git-utils.d.ts.map +0 -1
  201. package/dist/spec/git-utils.js +0 -14
  202. package/dist/spec/git-utils.js.map +0 -1
  203. package/dist/spec/mine/clusterer.d.ts +0 -63
  204. package/dist/spec/mine/clusterer.d.ts.map +0 -1
  205. package/dist/spec/mine/clusterer.js +0 -904
  206. package/dist/spec/mine/clusterer.js.map +0 -1
  207. package/dist/spec/mine/progress-handler.d.ts +0 -22
  208. package/dist/spec/mine/progress-handler.d.ts.map +0 -1
  209. package/dist/spec/mine/progress-handler.js +0 -108
  210. package/dist/spec/mine/progress-handler.js.map +0 -1
  211. package/dist/spec/mine/progress.d.ts +0 -23
  212. package/dist/spec/mine/progress.d.ts.map +0 -1
  213. package/dist/spec/mine/progress.js +0 -12
  214. package/dist/spec/mine/progress.js.map +0 -1
@@ -1,275 +1,275 @@
1
- #!/usr/bin/env node
2
- 'use strict';
3
- //
4
- // npm thin-installer launcher for HomeGraph.
5
- //
6
- // The heavy artifact (a vendored Node runtime + the app) ships as a per-platform
7
- // optionalDependency: homegraph-<platform>-<arch>. npm installs
8
- // only the one matching the host, via each package's `os`/`cpu` fields (the
9
- // esbuild pattern). This shim — run by the user's OWN Node — locates that bundle
10
- // and execs its launcher, so the real work always runs on the bundled Node 24
11
- // (with node:sqlite), regardless of the user's Node version. The user's Node is
12
- // only ever a launcher; even an ancient version can run this file.
13
- //
14
- // Self-heal (issue #303): some registries — notably the npmmirror/cnpm mirrors,
15
- // and some corporate proxies — don't reliably mirror the per-platform
16
- // optionalDependencies. npm treats an unfetchable optional dep as success and
17
- // silently skips it, so the bundle goes missing and every command fails. When
18
- // the installed bundle can't be resolved, this shim falls back to downloading
19
- // the matching bundle straight from GitHub Releases — the very archive
20
- // install.sh uses — into a cache dir, then runs that. Knobs:
21
- // HOMEGRAPH_NO_DOWNLOAD=1 disable the network fallback (print guidance)
22
- // HOMEGRAPH_INSTALL_DIR=DIR cache location (default: ~/.homegraph)
23
- // HOMEGRAPH_DOWNLOAD_BASE=URL release-download base (for mirrors/air-gapped)
24
- //
25
- // Wired up at release time as the main package's `bin`:
26
- // "bin": { "homegraph": "npm-shim.js" }
27
- // with the platform packages listed in `optionalDependencies`.
28
-
29
- var childProcess = require('child_process');
30
- var fs = require('fs');
31
- var os = require('os');
32
- var path = require('path');
33
-
34
- var target = process.platform + '-' + process.arch; // e.g. darwin-arm64, linux-x64
35
- var pkg = 'homegraph-' + target;
36
- var isWindows = process.platform === 'win32';
37
- var REPO = 'homegraph/homegraph';
38
-
39
- main().catch(function (e) {
40
- process.stderr.write('homegraph: ' + (e && e.message ? e.message : String(e)) + '\n');
41
- process.exit(1);
42
- });
43
-
44
- async function main() {
45
- // Happy path: the npm-installed optional dependency. Fall back to a download
46
- // when the registry didn't deliver it.
47
- var resolved = resolveInstalledBundle() || (await selfHealBundle());
48
- // Thread the MCP host's pid (our parent) down to the bundled server so its
49
- // orphan watchdog can poll the host directly. Without this, the server can
50
- // only watch THIS shim — and a shim killed during the server's first ~100ms
51
- // of startup used to leave the server orphaned forever (issue #1185). An
52
- // already-set value (an outer launcher) wins.
53
- var env = Object.assign({}, process.env);
54
- if (!env.HOMEGRAPH_HOST_PPID) env.HOMEGRAPH_HOST_PPID = String(process.ppid);
55
- var res = childProcess.spawnSync(resolved.command, resolved.args, { stdio: 'inherit', windowsHide: true, env: env });
56
- if (res.error) {
57
- process.stderr.write('homegraph: ' + res.error.message + '\n');
58
- process.exit(1);
59
- }
60
- process.exit(res.status === null ? 1 : res.status);
61
- }
62
-
63
- // Resolve the launcher from the installed per-platform optionalDependency.
64
- // Returns {command, args} or null if the package isn't installed.
65
- function resolveInstalledBundle() {
66
- try {
67
- if (isWindows) {
68
- // Modern Node refuses to spawn the bundle's .cmd directly (EINVAL, the
69
- // CVE-2024-27980 hardening on Node 24), so invoke the bundled node.exe
70
- // against the app entry point and pass --liftoff-only here.
71
- var nodeExe = require.resolve(pkg + '/node.exe');
72
- var entry = require.resolve(pkg + '/lib/dist/bin/homegraph.js');
73
- return { command: nodeExe, args: liftoff(entry) };
74
- }
75
- return { command: require.resolve(pkg + '/bin/homegraph'), args: process.argv.slice(2) };
76
- } catch (e) {
77
- return null;
78
- }
79
- }
80
-
81
- // Locate the launcher inside an extracted GitHub bundle directory (same
82
- // node/lib/bin layout as the npm platform package). Returns {command, args} or
83
- // null when the directory doesn't hold a usable bundle yet.
84
- function launcherIn(dir) {
85
- if (isWindows) {
86
- var nodeExe = path.join(dir, 'node.exe');
87
- var entry = path.join(dir, 'lib', 'dist', 'bin', 'homegraph.js');
88
- if (fs.existsSync(nodeExe) && fs.existsSync(entry)) {
89
- return { command: nodeExe, args: liftoff(entry) };
90
- }
91
- } else {
92
- var launcher = path.join(dir, 'bin', 'homegraph');
93
- if (fs.existsSync(launcher)) return { command: launcher, args: process.argv.slice(2) };
94
- }
95
- return null;
96
- }
97
-
98
- // --liftoff-only keeps tree-sitter's WASM grammars off V8's turboshaft tier to
99
- // avoid the Zone OOM on Node >= 22 (issues #293/#298). The unix bin/homegraph
100
- // launcher already passes it; on Windows we invoke node.exe directly so add it.
101
- function liftoff(entry) {
102
- return ['--liftoff-only', entry].concat(process.argv.slice(2));
103
- }
104
-
105
- // Download + cache the platform bundle from GitHub Releases. Returns
106
- // {command, args}; exits the process with guidance if it can't.
107
- async function selfHealBundle() {
108
- var version = readVersion();
109
- var bundlesDir = path.join(process.env.HOMEGRAPH_INSTALL_DIR || path.join(os.homedir(), '.homegraph'), 'bundles');
110
- var dest = path.join(bundlesDir, target + '-' + version);
111
-
112
- // Already downloaded by a previous run? Use it even when downloads are
113
- // disabled — HOMEGRAPH_NO_DOWNLOAD blocks fetching, not a cached bundle.
114
- var cached = launcherIn(dest);
115
- if (cached) { pruneOldBundles(bundlesDir, dest); return cached; }
116
-
117
- if (process.env.HOMEGRAPH_NO_DOWNLOAD) {
118
- fail('the network fallback is disabled (HOMEGRAPH_NO_DOWNLOAD is set).');
119
- }
120
-
121
- var asset = 'homegraph-' + target + (isWindows ? '.zip' : '.tar.gz');
122
- var base = process.env.HOMEGRAPH_DOWNLOAD_BASE || ('https://github.com/' + REPO + '/releases/download');
123
- var url = base + '/v' + version + '/' + asset;
124
-
125
- process.stderr.write(
126
- 'homegraph: platform bundle missing (registry did not provide ' + pkg + ').\n' +
127
- 'homegraph: downloading ' + asset + ' from GitHub Releases (' + version + ')...\n'
128
- );
129
-
130
- // Stage inside bundlesDir so the final rename is on the same filesystem (atomic,
131
- // no EXDEV across tmpfs). Strip the archive's top-level homegraph-<target>/ dir.
132
- fs.mkdirSync(bundlesDir, { recursive: true });
133
- var stage = fs.mkdtempSync(path.join(bundlesDir, '.dl-'));
134
- try {
135
- var archivePath = path.join(stage, asset);
136
- await download(url, archivePath, 6);
137
- await verifyChecksum(archivePath, asset, base, version);
138
- var extracted = path.join(stage, 'bundle');
139
- fs.mkdirSync(extracted);
140
- extract(archivePath, extracted);
141
-
142
- var raced = launcherIn(dest); // another process may have finished meanwhile
143
- if (raced) { rmrf(stage); return raced; }
144
- try {
145
- fs.renameSync(extracted, dest);
146
- } catch (e) {
147
- var other = launcherIn(dest); // lost the race but theirs is valid
148
- if (other) { rmrf(stage); return other; }
149
- throw e;
150
- }
151
- } catch (e) {
152
- rmrf(stage);
153
- fail('download failed (' + e.message + ').\n URL: ' + url);
154
- }
155
- rmrf(stage);
156
-
157
- var ready = launcherIn(dest);
158
- if (!ready) fail('downloaded bundle is missing its launcher under ' + dest + '.');
159
- pruneOldBundles(bundlesDir, dest);
160
- process.stderr.write('homegraph: bundle ready.\n');
161
- return ready;
162
- }
163
-
164
- function readVersion() {
165
- try {
166
- return require(path.join(__dirname, 'package.json')).version;
167
- } catch (e) {
168
- fail('could not read this package\'s version to locate a matching release.');
169
- }
170
- }
171
-
172
- // GET with manual redirect following (GitHub release URLs redirect to a CDN).
173
- function download(url, dest, redirectsLeft) {
174
- return new Promise(function (resolve, reject) {
175
- var https = require('https');
176
- // timeout is an idle/inactivity timeout — it won't kill a slow-but-progressing
177
- // download, only a stalled connection (so a blocked mirror fails fast with
178
- // guidance instead of hanging the user's command forever).
179
- var req = https.get(url, { headers: { 'User-Agent': 'homegraph-npm-shim' }, timeout: 30000 }, function (res) {
180
- var status = res.statusCode;
181
- if (status >= 300 && status < 400 && res.headers.location) {
182
- res.resume();
183
- if (redirectsLeft <= 0) { reject(new Error('too many redirects')); return; }
184
- download(new URL(res.headers.location, url).toString(), dest, redirectsLeft - 1).then(resolve, reject);
185
- return;
186
- }
187
- if (status !== 200) { res.resume(); reject(new Error('HTTP ' + status)); return; }
188
- var file = fs.createWriteStream(dest);
189
- res.on('error', reject);
190
- res.pipe(file);
191
- file.on('error', reject);
192
- file.on('finish', function () { file.close(function () { resolve(); }); });
193
- });
194
- req.on('timeout', function () { req.destroy(new Error('connection timed out')); });
195
- req.on('error', reject);
196
- });
197
- }
198
-
199
- // Best-effort integrity check. When the release publishes a SHA256SUMS file, the
200
- // downloaded archive MUST match its listed hash or we abort. When that file is
201
- // absent (older releases) or simply unreachable, we proceed — the archive still
202
- // arrived from GitHub over TLS. So tampering/corruption is caught, while a
203
- // missing checksum never breaks an install.
204
- async function verifyChecksum(archivePath, asset, base, version) {
205
- var sumsPath = archivePath + '.SHA256SUMS';
206
- try {
207
- await download(base + '/v' + version + '/SHA256SUMS', sumsPath, 6);
208
- } catch (e) {
209
- return; // not published / unreachable → skip
210
- }
211
- var expected = null;
212
- var lines = fs.readFileSync(sumsPath, 'utf8').split('\n');
213
- for (var i = 0; i < lines.length; i++) {
214
- var m = lines[i].trim().match(/^([0-9a-fA-F]{64})\s+\*?(.+)$/);
215
- if (m && path.basename(m[2].trim()) === asset) { expected = m[1].toLowerCase(); break; }
216
- }
217
- if (!expected) return; // asset not listed → nothing to check
218
- var actual = require('crypto').createHash('sha256').update(fs.readFileSync(archivePath)).digest('hex');
219
- if (actual !== expected) {
220
- throw new Error('checksum mismatch for ' + asset +
221
- ' (expected ' + expected.slice(0, 12) + '…, got ' + actual.slice(0, 12) + '…)');
222
- }
223
- process.stderr.write('homegraph: checksum verified.\n');
224
- }
225
-
226
- // Extract via the system tar — present on macOS, Linux, and Windows 10+
227
- // (bsdtar reads .zip too). No third-party dependency in the shim.
228
- function extract(archive, destDir) {
229
- var args = isWindows
230
- ? ['-xf', archive, '-C', destDir, '--strip-components=1']
231
- : ['-xzf', archive, '-C', destDir, '--strip-components=1'];
232
- var res = childProcess.spawnSync('tar', args, { stdio: 'ignore', windowsHide: true });
233
- if (res.error) throw new Error('tar unavailable: ' + res.error.message);
234
- if (res.status !== 0) throw new Error('tar exited ' + res.status);
235
- }
236
-
237
- function rmrf(p) {
238
- try { fs.rmSync(p, { recursive: true, force: true }); } catch (e) { /* best effort */ }
239
- }
240
-
241
- // Drop sibling bundles for OTHER versions of this same platform target, keeping
242
- // only keepDir. The self-heal cache otherwise accumulates a full ~50 MB bundle
243
- // per version forever (issue #1074). Best-effort: a locked/busy dir (a
244
- // concurrent run still mapping an older node.exe on Windows) just stays — rmrf
245
- // already swallows its own errors, and the readdir is guarded — so cleanup can
246
- // never break a working command. Only this target's "<target>-<version>" dirs
247
- // are touched; other platforms' bundles and the ".dl-*" staging dirs are left
248
- // alone.
249
- function pruneOldBundles(bundlesDir, keepDir) {
250
- var keep = path.basename(keepDir);
251
- try {
252
- var names = fs.readdirSync(bundlesDir);
253
- for (var i = 0; i < names.length; i++) {
254
- var name = names[i];
255
- if (name === keep) continue;
256
- if (name.indexOf(target + '-') !== 0) continue;
257
- rmrf(path.join(bundlesDir, name));
258
- }
259
- } catch (e) { /* best effort — never break a working run over cleanup */ }
260
- }
261
-
262
- function fail(reason) {
263
- process.stderr.write(
264
- 'homegraph: no prebuilt bundle for ' + target + '.\n' +
265
- (reason ? 'homegraph: ' + reason + '\n' : '') +
266
- 'Expected the optional package ' + pkg + ' to be installed.\n' +
267
- 'A registry mirror (e.g. npmmirror/cnpm) that did not mirror the per-platform\n' +
268
- 'package is the usual cause. Fixes:\n' +
269
- ' - install from the official registry:\n' +
270
- ' npm i -g homegraph --registry=https://registry.npmjs.org\n' +
271
- ' - or use the standalone installer (no Node required):\n' +
272
- ' curl -fsSL https://raw.githubusercontent.com/' + REPO + '/main/install.sh | sh\n'
273
- );
274
- process.exit(1);
275
- }
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+ //
4
+ // npm thin-installer launcher for HomeGraph.
5
+ //
6
+ // The heavy artifact (a vendored Node runtime + the app) ships as a per-platform
7
+ // optionalDependency: homegraph-<platform>-<arch>. npm installs
8
+ // only the one matching the host, via each package's `os`/`cpu` fields (the
9
+ // esbuild pattern). This shim — run by the user's OWN Node — locates that bundle
10
+ // and execs its launcher, so the real work always runs on the bundled Node 24
11
+ // (with node:sqlite), regardless of the user's Node version. The user's Node is
12
+ // only ever a launcher; even an ancient version can run this file.
13
+ //
14
+ // Self-heal (issue #303): some registries — notably the npmmirror/cnpm mirrors,
15
+ // and some corporate proxies — don't reliably mirror the per-platform
16
+ // optionalDependencies. npm treats an unfetchable optional dep as success and
17
+ // silently skips it, so the bundle goes missing and every command fails. When
18
+ // the installed bundle can't be resolved, this shim falls back to downloading
19
+ // the matching bundle straight from GitHub Releases — the very archive
20
+ // install.sh uses — into a cache dir, then runs that. Knobs:
21
+ // HOMEGRAPH_NO_DOWNLOAD=1 disable the network fallback (print guidance)
22
+ // HOMEGRAPH_INSTALL_DIR=DIR cache location (default: ~/.homegraph)
23
+ // HOMEGRAPH_DOWNLOAD_BASE=URL release-download base (for mirrors/air-gapped)
24
+ //
25
+ // Wired up at release time as the main package's `bin`:
26
+ // "bin": { "homegraph": "npm-shim.js" }
27
+ // with the platform packages listed in `optionalDependencies`.
28
+
29
+ var childProcess = require('child_process');
30
+ var fs = require('fs');
31
+ var os = require('os');
32
+ var path = require('path');
33
+
34
+ var target = process.platform + '-' + process.arch; // e.g. darwin-arm64, linux-x64
35
+ var pkg = 'homegraph-' + target;
36
+ var isWindows = process.platform === 'win32';
37
+ var REPO = 'homegraph/homegraph';
38
+
39
+ main().catch(function (e) {
40
+ process.stderr.write('homegraph: ' + (e && e.message ? e.message : String(e)) + '\n');
41
+ process.exit(1);
42
+ });
43
+
44
+ async function main() {
45
+ // Happy path: the npm-installed optional dependency. Fall back to a download
46
+ // when the registry didn't deliver it.
47
+ var resolved = resolveInstalledBundle() || (await selfHealBundle());
48
+ // Thread the MCP host's pid (our parent) down to the bundled server so its
49
+ // orphan watchdog can poll the host directly. Without this, the server can
50
+ // only watch THIS shim — and a shim killed during the server's first ~100ms
51
+ // of startup used to leave the server orphaned forever (issue #1185). An
52
+ // already-set value (an outer launcher) wins.
53
+ var env = Object.assign({}, process.env);
54
+ if (!env.HOMEGRAPH_HOST_PPID) env.HOMEGRAPH_HOST_PPID = String(process.ppid);
55
+ var res = childProcess.spawnSync(resolved.command, resolved.args, { stdio: 'inherit', windowsHide: true, env: env });
56
+ if (res.error) {
57
+ process.stderr.write('homegraph: ' + res.error.message + '\n');
58
+ process.exit(1);
59
+ }
60
+ process.exit(res.status === null ? 1 : res.status);
61
+ }
62
+
63
+ // Resolve the launcher from the installed per-platform optionalDependency.
64
+ // Returns {command, args} or null if the package isn't installed.
65
+ function resolveInstalledBundle() {
66
+ try {
67
+ if (isWindows) {
68
+ // Modern Node refuses to spawn the bundle's .cmd directly (EINVAL, the
69
+ // CVE-2024-27980 hardening on Node 24), so invoke the bundled node.exe
70
+ // against the app entry point and pass --liftoff-only here.
71
+ var nodeExe = require.resolve(pkg + '/node.exe');
72
+ var entry = require.resolve(pkg + '/lib/dist/bin/homegraph.js');
73
+ return { command: nodeExe, args: liftoff(entry) };
74
+ }
75
+ return { command: require.resolve(pkg + '/bin/homegraph'), args: process.argv.slice(2) };
76
+ } catch (e) {
77
+ return null;
78
+ }
79
+ }
80
+
81
+ // Locate the launcher inside an extracted GitHub bundle directory (same
82
+ // node/lib/bin layout as the npm platform package). Returns {command, args} or
83
+ // null when the directory doesn't hold a usable bundle yet.
84
+ function launcherIn(dir) {
85
+ if (isWindows) {
86
+ var nodeExe = path.join(dir, 'node.exe');
87
+ var entry = path.join(dir, 'lib', 'dist', 'bin', 'homegraph.js');
88
+ if (fs.existsSync(nodeExe) && fs.existsSync(entry)) {
89
+ return { command: nodeExe, args: liftoff(entry) };
90
+ }
91
+ } else {
92
+ var launcher = path.join(dir, 'bin', 'homegraph');
93
+ if (fs.existsSync(launcher)) return { command: launcher, args: process.argv.slice(2) };
94
+ }
95
+ return null;
96
+ }
97
+
98
+ // --liftoff-only keeps tree-sitter's WASM grammars off V8's turboshaft tier to
99
+ // avoid the Zone OOM on Node >= 22 (issues #293/#298). The unix bin/homegraph
100
+ // launcher already passes it; on Windows we invoke node.exe directly so add it.
101
+ function liftoff(entry) {
102
+ return ['--liftoff-only', entry].concat(process.argv.slice(2));
103
+ }
104
+
105
+ // Download + cache the platform bundle from GitHub Releases. Returns
106
+ // {command, args}; exits the process with guidance if it can't.
107
+ async function selfHealBundle() {
108
+ var version = readVersion();
109
+ var bundlesDir = path.join(process.env.HOMEGRAPH_INSTALL_DIR || path.join(os.homedir(), '.homegraph'), 'bundles');
110
+ var dest = path.join(bundlesDir, target + '-' + version);
111
+
112
+ // Already downloaded by a previous run? Use it even when downloads are
113
+ // disabled — HOMEGRAPH_NO_DOWNLOAD blocks fetching, not a cached bundle.
114
+ var cached = launcherIn(dest);
115
+ if (cached) { pruneOldBundles(bundlesDir, dest); return cached; }
116
+
117
+ if (process.env.HOMEGRAPH_NO_DOWNLOAD) {
118
+ fail('the network fallback is disabled (HOMEGRAPH_NO_DOWNLOAD is set).');
119
+ }
120
+
121
+ var asset = 'homegraph-' + target + (isWindows ? '.zip' : '.tar.gz');
122
+ var base = process.env.HOMEGRAPH_DOWNLOAD_BASE || ('https://github.com/' + REPO + '/releases/download');
123
+ var url = base + '/v' + version + '/' + asset;
124
+
125
+ process.stderr.write(
126
+ 'homegraph: platform bundle missing (registry did not provide ' + pkg + ').\n' +
127
+ 'homegraph: downloading ' + asset + ' from GitHub Releases (' + version + ')...\n'
128
+ );
129
+
130
+ // Stage inside bundlesDir so the final rename is on the same filesystem (atomic,
131
+ // no EXDEV across tmpfs). Strip the archive's top-level homegraph-<target>/ dir.
132
+ fs.mkdirSync(bundlesDir, { recursive: true });
133
+ var stage = fs.mkdtempSync(path.join(bundlesDir, '.dl-'));
134
+ try {
135
+ var archivePath = path.join(stage, asset);
136
+ await download(url, archivePath, 6);
137
+ await verifyChecksum(archivePath, asset, base, version);
138
+ var extracted = path.join(stage, 'bundle');
139
+ fs.mkdirSync(extracted);
140
+ extract(archivePath, extracted);
141
+
142
+ var raced = launcherIn(dest); // another process may have finished meanwhile
143
+ if (raced) { rmrf(stage); return raced; }
144
+ try {
145
+ fs.renameSync(extracted, dest);
146
+ } catch (e) {
147
+ var other = launcherIn(dest); // lost the race but theirs is valid
148
+ if (other) { rmrf(stage); return other; }
149
+ throw e;
150
+ }
151
+ } catch (e) {
152
+ rmrf(stage);
153
+ fail('download failed (' + e.message + ').\n URL: ' + url);
154
+ }
155
+ rmrf(stage);
156
+
157
+ var ready = launcherIn(dest);
158
+ if (!ready) fail('downloaded bundle is missing its launcher under ' + dest + '.');
159
+ pruneOldBundles(bundlesDir, dest);
160
+ process.stderr.write('homegraph: bundle ready.\n');
161
+ return ready;
162
+ }
163
+
164
+ function readVersion() {
165
+ try {
166
+ return require(path.join(__dirname, 'package.json')).version;
167
+ } catch (e) {
168
+ fail('could not read this package\'s version to locate a matching release.');
169
+ }
170
+ }
171
+
172
+ // GET with manual redirect following (GitHub release URLs redirect to a CDN).
173
+ function download(url, dest, redirectsLeft) {
174
+ return new Promise(function (resolve, reject) {
175
+ var https = require('https');
176
+ // timeout is an idle/inactivity timeout — it won't kill a slow-but-progressing
177
+ // download, only a stalled connection (so a blocked mirror fails fast with
178
+ // guidance instead of hanging the user's command forever).
179
+ var req = https.get(url, { headers: { 'User-Agent': 'homegraph-npm-shim' }, timeout: 30000 }, function (res) {
180
+ var status = res.statusCode;
181
+ if (status >= 300 && status < 400 && res.headers.location) {
182
+ res.resume();
183
+ if (redirectsLeft <= 0) { reject(new Error('too many redirects')); return; }
184
+ download(new URL(res.headers.location, url).toString(), dest, redirectsLeft - 1).then(resolve, reject);
185
+ return;
186
+ }
187
+ if (status !== 200) { res.resume(); reject(new Error('HTTP ' + status)); return; }
188
+ var file = fs.createWriteStream(dest);
189
+ res.on('error', reject);
190
+ res.pipe(file);
191
+ file.on('error', reject);
192
+ file.on('finish', function () { file.close(function () { resolve(); }); });
193
+ });
194
+ req.on('timeout', function () { req.destroy(new Error('connection timed out')); });
195
+ req.on('error', reject);
196
+ });
197
+ }
198
+
199
+ // Best-effort integrity check. When the release publishes a SHA256SUMS file, the
200
+ // downloaded archive MUST match its listed hash or we abort. When that file is
201
+ // absent (older releases) or simply unreachable, we proceed — the archive still
202
+ // arrived from GitHub over TLS. So tampering/corruption is caught, while a
203
+ // missing checksum never breaks an install.
204
+ async function verifyChecksum(archivePath, asset, base, version) {
205
+ var sumsPath = archivePath + '.SHA256SUMS';
206
+ try {
207
+ await download(base + '/v' + version + '/SHA256SUMS', sumsPath, 6);
208
+ } catch (e) {
209
+ return; // not published / unreachable → skip
210
+ }
211
+ var expected = null;
212
+ var lines = fs.readFileSync(sumsPath, 'utf8').split('\n');
213
+ for (var i = 0; i < lines.length; i++) {
214
+ var m = lines[i].trim().match(/^([0-9a-fA-F]{64})\s+\*?(.+)$/);
215
+ if (m && path.basename(m[2].trim()) === asset) { expected = m[1].toLowerCase(); break; }
216
+ }
217
+ if (!expected) return; // asset not listed → nothing to check
218
+ var actual = require('crypto').createHash('sha256').update(fs.readFileSync(archivePath)).digest('hex');
219
+ if (actual !== expected) {
220
+ throw new Error('checksum mismatch for ' + asset +
221
+ ' (expected ' + expected.slice(0, 12) + '…, got ' + actual.slice(0, 12) + '…)');
222
+ }
223
+ process.stderr.write('homegraph: checksum verified.\n');
224
+ }
225
+
226
+ // Extract via the system tar — present on macOS, Linux, and Windows 10+
227
+ // (bsdtar reads .zip too). No third-party dependency in the shim.
228
+ function extract(archive, destDir) {
229
+ var args = isWindows
230
+ ? ['-xf', archive, '-C', destDir, '--strip-components=1']
231
+ : ['-xzf', archive, '-C', destDir, '--strip-components=1'];
232
+ var res = childProcess.spawnSync('tar', args, { stdio: 'ignore', windowsHide: true });
233
+ if (res.error) throw new Error('tar unavailable: ' + res.error.message);
234
+ if (res.status !== 0) throw new Error('tar exited ' + res.status);
235
+ }
236
+
237
+ function rmrf(p) {
238
+ try { fs.rmSync(p, { recursive: true, force: true }); } catch (e) { /* best effort */ }
239
+ }
240
+
241
+ // Drop sibling bundles for OTHER versions of this same platform target, keeping
242
+ // only keepDir. The self-heal cache otherwise accumulates a full ~50 MB bundle
243
+ // per version forever (issue #1074). Best-effort: a locked/busy dir (a
244
+ // concurrent run still mapping an older node.exe on Windows) just stays — rmrf
245
+ // already swallows its own errors, and the readdir is guarded — so cleanup can
246
+ // never break a working command. Only this target's "<target>-<version>" dirs
247
+ // are touched; other platforms' bundles and the ".dl-*" staging dirs are left
248
+ // alone.
249
+ function pruneOldBundles(bundlesDir, keepDir) {
250
+ var keep = path.basename(keepDir);
251
+ try {
252
+ var names = fs.readdirSync(bundlesDir);
253
+ for (var i = 0; i < names.length; i++) {
254
+ var name = names[i];
255
+ if (name === keep) continue;
256
+ if (name.indexOf(target + '-') !== 0) continue;
257
+ rmrf(path.join(bundlesDir, name));
258
+ }
259
+ } catch (e) { /* best effort — never break a working run over cleanup */ }
260
+ }
261
+
262
+ function fail(reason) {
263
+ process.stderr.write(
264
+ 'homegraph: no prebuilt bundle for ' + target + '.\n' +
265
+ (reason ? 'homegraph: ' + reason + '\n' : '') +
266
+ 'Expected the optional package ' + pkg + ' to be installed.\n' +
267
+ 'A registry mirror (e.g. npmmirror/cnpm) that did not mirror the per-platform\n' +
268
+ 'package is the usual cause. Fixes:\n' +
269
+ ' - install from the official registry:\n' +
270
+ ' npm i -g homegraph --registry=https://registry.npmjs.org\n' +
271
+ ' - or use the standalone installer (no Node required):\n' +
272
+ ' curl -fsSL https://raw.githubusercontent.com/' + REPO + '/main/install.sh | sh\n'
273
+ );
274
+ process.exit(1);
275
+ }