@esankhan3/code-search-mcp 1.1.8 → 1.1.10

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.
@@ -0,0 +1,94 @@
1
+ import { describe, it, before, after } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { mkdirSync, rmSync, writeFileSync } from "node:fs";
4
+ import { join } from "node:path";
5
+ import {
6
+ BIN,
7
+ tmp,
8
+ makeRepo,
9
+ baseEnv,
10
+ spawnProc,
11
+ getJson,
12
+ waitFor,
13
+ exitWithin,
14
+ startFakeEmbedder
15
+ } from "./_harness.js";
16
+ let root;
17
+ let ws;
18
+ let embedder;
19
+ before(async () => {
20
+ root = tmp("cs-cal-");
21
+ ws = join(root, "ws");
22
+ makeRepo(ws, "svc", Object.fromEntries(Array.from({ length: 300 }, (_, i) => [
23
+ `src/m${i}.ts`,
24
+ `export function handler${i}(a: number): number {
25
+ return a * ${i} + ${i * 7};
26
+ }
27
+ `
28
+ ])));
29
+ embedder = await startFakeEmbedder(47404);
30
+ mkdirSync(join(root, "home", ".code-search"), { recursive: true });
31
+ writeFileSync(join(root, "home", ".code-search", "config.yaml"), [
32
+ "storage:",
33
+ " backends:",
34
+ " vector:",
35
+ " lancedb:",
36
+ " index: { type: ivf_pq, minRows: 256, numPartitions: 4, numSubVectors: 2 }",
37
+ ""
38
+ ].join("\n"));
39
+ });
40
+ after(async () => {
41
+ await embedder.close();
42
+ rmSync(root, { recursive: true, force: true });
43
+ });
44
+ describe("nprobes calibration and server starts", () => {
45
+ it("the first index run after each server start recalibrates; later runs only when due", async () => {
46
+ const env = baseEnv({
47
+ home: join(root, "home"),
48
+ dataDir: join(root, "data"),
49
+ embedderUrl: embedder.url,
50
+ extra: { CODE_SEARCH_WORKERS: "2" }
51
+ });
52
+ const base = "http://127.0.0.1:47405";
53
+ const serve = async () => {
54
+ const p2 = spawnProc(BIN.mcp, ["--serve", "--port", "47405"], env);
55
+ await waitFor(async () => (await getJson(`${base}/health`)).status === 200, 2e4, `health
56
+ ${p2.stderr()}`);
57
+ await waitFor(() => (p2.stderr().match(/Server running for/g) ?? []).length >= 2, 2e4, "two workers");
58
+ return p2;
59
+ };
60
+ const index = async (p2) => {
61
+ const r = await getJson(`${base}/index`, {
62
+ method: "POST",
63
+ headers: { "Content-Type": "application/json" },
64
+ body: JSON.stringify({ path: ws })
65
+ });
66
+ assert.equal(r.status, 200, `${JSON.stringify(r.body)}
67
+ ${p2.stderr()}`);
68
+ };
69
+ const calibrations = (p2) => p2.stderr().match(/nprobes=\d+ \([^;]*/g) ?? [];
70
+ const stop = async (p2) => {
71
+ p2.child.kill("SIGTERM");
72
+ await exitWithin(p2, 5e3);
73
+ };
74
+ let p = await serve();
75
+ try {
76
+ await index(p);
77
+ await index(p);
78
+ assert.equal(calibrations(p).length, 1, p.stderr());
79
+ assert.doesNotMatch(calibrations(p)[0], /recalibrated/);
80
+ } finally {
81
+ await stop(p);
82
+ }
83
+ p = await serve();
84
+ try {
85
+ await index(p);
86
+ await index(p);
87
+ const runs = calibrations(p);
88
+ assert.equal(runs.length, 1, p.stderr());
89
+ assert.match(runs[0], /recalibrated at server start/);
90
+ } finally {
91
+ await stop(p);
92
+ }
93
+ });
94
+ });
@@ -1,7 +1,7 @@
1
1
  import { describe, it, before, after } from "node:test";
2
2
  import assert from "node:assert/strict";
3
3
  import { execFileSync, spawnSync } from "node:child_process";
4
- import { rmSync } from "node:fs";
4
+ import { readFileSync, rmSync } from "node:fs";
5
5
  import { join } from "node:path";
6
6
  import {
7
7
  BIN,
@@ -121,6 +121,10 @@ ${p.stderr()}`);
121
121
  let indexer = 0;
122
122
  await waitFor(() => (indexer = childPid(p.child.pid, "index-child")) > 0, 1e4, `an indexer child process
123
123
  ${p.stderr()}`);
124
+ if (process.platform === "linux") {
125
+ const score = () => readFileSync(`/proc/${indexer}/oom_score_adj`, "utf8").trim();
126
+ await waitFor(() => score() === "1000", 1e4, "the indexer to put itself first for the out-of-memory killer");
127
+ }
124
128
  process.kill(indexer, "SIGKILL");
125
129
  const res = await killed;
126
130
  assert.equal(res.status, 500, JSON.stringify(res.body));
@@ -1,9 +1,15 @@
1
+ import { writeFileSync } from "node:fs";
1
2
  import { indexFromPath } from "@esankhan3/anvil-knowledge-core";
2
3
  const send = (msg, then) => process.send(msg, then);
4
+ try {
5
+ writeFileSync("/proc/self/oom_score_adj", "1000");
6
+ } catch {
7
+ }
3
8
  process.once("message", (run) => {
4
9
  indexFromPath(run.project, run.dirPath, {
5
10
  force: run.force,
6
11
  config: run.config,
12
+ freshCalibration: run.freshCalibration,
7
13
  onProgress: (message) => send({ type: "progress", message }),
8
14
  onDetailedProgress: (progress) => send({ type: "detail", progress })
9
15
  }).then(
package/dist/server.js CHANGED
@@ -14,8 +14,8 @@ import {
14
14
  getBlobStore,
15
15
  readVectorIndexHealth,
16
16
  indexFromPath,
17
- getRetriever,
18
17
  invalidateRetriever,
18
+ refreshRetriever,
19
19
  KnowledgeIndexer
20
20
  } from "@esankhan3/anvil-knowledge-core";
21
21
  import { loadServerConfig, resolvedKnowledgeConfig } from "./core/env-config.js";
@@ -340,7 +340,6 @@ function runClusterPrimary(ctx, workerCount) {
340
340
  let ipcSeq = 0;
341
341
  const pendingIndexRequests = /* @__PURE__ */ new Map();
342
342
  const pendingRateRequests = /* @__PURE__ */ new Map();
343
- let invalidation = Promise.resolve();
344
343
  function forwardIndexToPrimary(body) {
345
344
  return new Promise((resolvePromise, rejectPromise) => {
346
345
  if (!process.send) {
@@ -383,12 +382,10 @@ function registerWorkerIpc(ctx) {
383
382
  pendingRateRequests.delete(msg.id);
384
383
  pending?.({ ok: msg.ok, retryAfterSec: msg.retryAfterSec });
385
384
  } else if (msg?.type === "csm:invalidate") {
386
- invalidation = invalidateRetriever(msg.project).catch(() => {
387
- });
388
385
  if (msg.project === ctx.projectName) ctx.indexReady = true;
389
386
  } else if (msg?.type === "csm:warm") {
390
387
  const warmed = { type: "csm:warmed", id: msg.id };
391
- void invalidation.then(() => getRetriever(msg.project, resolvedKnowledgeConfig())).then(() => log.info("Search warmed after reindex")).catch((err) => log.warn(`Search warm-up failed (the next query rebuilds): ${err instanceof Error ? err.message : String(err)}`)).finally(() => {
388
+ void refreshRetriever(msg.project, resolvedKnowledgeConfig()).then(() => log.info("Search warmed after reindex")).catch((err) => log.warn(`Search warm-up failed (the next query rebuilds): ${err instanceof Error ? err.message : String(err)}`)).finally(() => {
392
389
  if (process.connected) process.send?.(warmed);
393
390
  });
394
391
  } else if (msg?.type === "csm:state") {
@@ -401,6 +398,7 @@ function registerWorkerIpc(ctx) {
401
398
  }
402
399
  let indexInChild = false;
403
400
  let activeIndexChild = null;
401
+ let freshCalibrationPending = true;
404
402
  function indexInChildProcess(project, dirPath, opts) {
405
403
  return new Promise((resolveRun, rejectRun) => {
406
404
  const child = fork(fileURLToPath(new URL("./index-child.js", import.meta.url)), [], {
@@ -417,7 +415,14 @@ function indexInChildProcess(project, dirPath, opts) {
417
415
  activeIndexChild = null;
418
416
  finish();
419
417
  };
420
- const run = { type: "run", project, dirPath, force: opts.force, config: opts.config };
418
+ const run = {
419
+ type: "run",
420
+ project,
421
+ dirPath,
422
+ force: opts.force,
423
+ config: opts.config,
424
+ freshCalibration: opts.freshCalibration
425
+ };
421
426
  child.on("message", (msg) => {
422
427
  if (msg.type === "ready") child.send(run);
423
428
  else if (msg.type === "progress") opts.onProgress?.(msg.message);
@@ -459,6 +464,7 @@ async function trackedIndex(ctx, project, dirPath, opts) {
459
464
  const stats = await (indexInChild ? indexInChildProcess : indexFromPath)(project, dirPath, {
460
465
  force: opts?.force,
461
466
  config: resolvedKnowledgeConfig(),
467
+ freshCalibration: freshCalibrationPending,
462
468
  onProgress: (m) => {
463
469
  ctx.indexing.message = m;
464
470
  progressLog.info(m);
@@ -469,6 +475,7 @@ async function trackedIndex(ctx, project, dirPath, opts) {
469
475
  ctx.indexing.message = p.message;
470
476
  }
471
477
  });
478
+ freshCalibrationPending = false;
472
479
  try {
473
480
  await invalidateRetriever(project);
474
481
  } catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@esankhan3/code-search-mcp",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "description": "MCP server for multi-repo code search \u2014 GitHub org or local path, zero config",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@esankhan3/anvil-agent-core": "^0.5.4",
27
- "@esankhan3/anvil-knowledge-core": "^0.4.13",
27
+ "@esankhan3/anvil-knowledge-core": "^0.4.17",
28
28
  "@modelcontextprotocol/node": "^2.0.0",
29
29
  "@modelcontextprotocol/server": "^2.0.0"
30
30
  },