memindex 1.1.1 → 1.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.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  for (const p of ['AGENT_MEM_']) for (const k of Object.keys(process.env)) { const n = 'MEMINDEX_' + k.slice(p.length); if (k.startsWith(p) && process.env[n] === undefined) process.env[n] = process.env[k]; }
3
3
  import { spawnSync } from 'child_process';
4
- import { existsSync, readFileSync, realpathSync, writeFileSync } from 'fs';
4
+ import { existsSync, readdirSync, readFileSync, realpathSync, writeFileSync } from 'fs';
5
5
  import { createRequire } from 'module';
6
6
  import { homedir } from 'os';
7
7
  import { join, dirname, relative, isAbsolute } from 'path';
@@ -184,6 +184,53 @@ function formatMissing(missing) {
184
184
  return missing.slice(0, MISSING_DEPS_LOG_LIMIT).join(', ') + ', +' + (missing.length - MISSING_DEPS_LOG_LIMIT) + ' more';
185
185
  }
186
186
 
187
+ // onnxruntime-node ships its native engine for every platform and CPU; only the
188
+ // folder for this machine is ever loaded. Delete the rest after an install.
189
+ // Mirrors trimOnnxruntimeBinaries in src/npx-cli/install/trim-onnxruntime.ts,
190
+ // which has the full explanation; keep the two in step. Deletion goes through
191
+ // safe-remove.cjs, the build's copy of scripts/safe-remove.cjs, the same guard
192
+ // the product uses: nothing outside the plugin folder, nothing that holds the
193
+ // home or data folders, links unlinked rather than followed.
194
+ // keepArches: every CPU architecture a program that loads the engine runs as
195
+ // (this script's own, and Bun's, which runs the worker); null keeps them all.
196
+ function trimOnnxruntimeBinaries(pluginRoot, keepArches = [process.arch], platform = process.platform) {
197
+ try {
198
+ const { removeTree } = createRequire(import.meta.url)('./safe-remove.cjs');
199
+ const binRoot = join(pluginRoot, NODE_MODULES_DIRNAME, 'onnxruntime-node', 'bin');
200
+ if (!existsSync(binRoot)) return;
201
+ const keep = (arch) => keepArches === null || keepArches.includes(arch);
202
+ for (const napi of readdirSync(binRoot)) {
203
+ const napiDir = join(binRoot, napi);
204
+ const ownDir = join(napiDir, platform);
205
+ if (!existsSync(ownDir) || !readdirSync(ownDir).some(keep)) continue;
206
+ for (const plat of readdirSync(napiDir)) {
207
+ const platDir = join(napiDir, plat);
208
+ const doomed = plat === platform
209
+ ? readdirSync(platDir).filter((arch) => !keep(arch)).map((arch) => join(platDir, arch))
210
+ : [platDir];
211
+ for (const target of doomed) removeTree(target, pluginRoot);
212
+ }
213
+ }
214
+ } catch {
215
+ // best effort: an untrimmed tree is only bigger
216
+ }
217
+ }
218
+
219
+ function bunArch(bunPath) {
220
+ try {
221
+ const result = spawnSync(bunPath, ['-p', 'process.arch'], {
222
+ encoding: 'utf-8',
223
+ stdio: ['pipe', 'pipe', 'pipe'],
224
+ timeout: 10_000,
225
+ windowsHide: true,
226
+ });
227
+ const arch = result.status === 0 ? String(result.stdout || '').trim() : '';
228
+ return arch || null;
229
+ } catch {
230
+ return null;
231
+ }
232
+ }
233
+
187
234
  // Setup-phase auto-install of plugin runtime dependencies.
188
235
  //
189
236
  // The plugin marketplace extracts files into ~/.claude/plugins/cache/...
@@ -283,6 +330,8 @@ function ensurePluginDependencies(pluginRoot) {
283
330
  // lookups process-wide, so running this file under bun would make the
284
331
  // re-check cry wolf on every successful fresh install. Keep the hook on node.
285
332
  const missingAfter = findMissingDependencies(pluginRoot);
333
+ const workerArch = bunArch(bunPath);
334
+ trimOnnxruntimeBinaries(pluginRoot, workerArch ? [...new Set([process.arch, workerArch])] : null);
286
335
  if (missingAfter.length === 0) {
287
336
  // Close the diagnostic loop: a Setup hook that can block for up to
288
337
  // 120s needs an explicit completion line so users can distinguish a