@pcircle/memesh 4.2.5 → 4.2.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pcircle/memesh",
3
- "version": "4.2.5",
3
+ "version": "4.2.7",
4
4
  "description": "MeMesh — Local memory for Claude Code and MCP coding agents. One SQLite file, zero cloud required.",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -387,23 +387,42 @@ export function tryRequireBetterSqlite() {
387
387
  function _attemptBetterSqliteRebuild() {
388
388
  try {
389
389
  // Package root = parent of the scripts/hooks/ directory that contains
390
- // this file. That's where `package.json` + `node_modules/` live.
390
+ // this file. That's where memesh's own `package.json` lives.
391
391
  const here = dirname(fileURLToPath(import.meta.url));
392
392
  const pkgRoot = dirname(dirname(here));
393
393
  if (!existsSync(join(pkgRoot, 'package.json'))) return;
394
- if (!existsSync(join(pkgRoot, 'node_modules', 'better-sqlite3'))) {
395
- // No better-sqlite3 directory at all (npm install never ran).
396
- // `npm rebuild` would no-op; the right fix is a full install,
397
- // which we don't trigger here (could pull arbitrary packages).
398
- // Surface a clear actionable line instead.
394
+ // Resolve better-sqlite3's actual install location via Node's normal
395
+ // resolution algorithm, which follows hoisting (the consumer's
396
+ // top-level node_modules holds the package when memesh is installed
397
+ // as a dependency). Looking at `<pkgRoot>/node_modules/better-sqlite3`
398
+ // directly would false-negative on every hoisted install.
399
+ let bsqliteDir;
400
+ try {
401
+ // `require.resolve` returns the path to `lib/index.js` inside the
402
+ // package. Walk up to the package directory.
403
+ const entry = require.resolve('better-sqlite3', { paths: [pkgRoot] });
404
+ // `<bsqliteDir>/lib/index.js` → walk back twice to the package root.
405
+ bsqliteDir = dirname(dirname(entry));
406
+ } catch {
407
+ // Genuinely not installed anywhere on the resolution path. `npm
408
+ // rebuild` cannot help; the user needs a full install.
399
409
  try {
400
410
  process.stderr.write(
401
- `[memesh hook] No better-sqlite3 in ${join(pkgRoot, 'node_modules')}. `
402
- + `Run: cd "${pkgRoot}" && npm install --omit=dev\n`,
411
+ `[memesh hook] better-sqlite3 is not installed (Node could not resolve from ${pkgRoot}). `
412
+ + `Run: cd to the project that depends on @pcircle/memesh and run \`npm install\`.\n`,
403
413
  );
404
414
  } catch {}
405
415
  return;
406
416
  }
417
+ // The hoisted install location's package root — npm rebuild needs to
418
+ // be run from a project that owns this node_modules tree. Walking
419
+ // up to the nearest directory that has its own package.json gives
420
+ // us the right cwd.
421
+ let rebuildCwd = dirname(bsqliteDir);
422
+ while (rebuildCwd !== dirname(rebuildCwd)) {
423
+ if (existsSync(join(rebuildCwd, 'package.json')) && !rebuildCwd.endsWith('node_modules')) break;
424
+ rebuildCwd = dirname(rebuildCwd);
425
+ }
407
426
  const memesh = join(homedir(), '.memesh');
408
427
  try { mkdirSync(memesh, { recursive: true, mode: 0o700 }); } catch {}
409
428
  const markerPath = join(memesh, 'last-rebuild-attempt.lock');
@@ -429,13 +448,13 @@ function _attemptBetterSqliteRebuild() {
429
448
  }
430
449
  process.stderr.write(
431
450
  `[memesh hook] Attempting to rebuild better-sqlite3 in background — `
432
- + `next session should capture normally. (pkgRoot: ${pkgRoot})\n`
451
+ + `next session should capture normally. (rebuildCwd: ${rebuildCwd})\n`
433
452
  + `[memesh hook] To retry later, manually: rm "${markerPath}" && `
434
- + `cd "${pkgRoot}" && npm rebuild better-sqlite3\n`,
453
+ + `cd "${rebuildCwd}" && npm rebuild better-sqlite3\n`,
435
454
  );
436
455
  const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
437
456
  const child = spawn(npm, ['rebuild', 'better-sqlite3'], {
438
- cwd: pkgRoot,
457
+ cwd: rebuildCwd,
439
458
  detached: true,
440
459
  stdio: 'ignore',
441
460
  windowsHide: true,