@esankhan3/code-search-mcp 1.0.5 → 1.1.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.
- package/README.md +114 -54
- package/dist/__tests__/_harness.js +194 -0
- package/dist/__tests__/config-cli.test.js +110 -0
- package/dist/__tests__/config-resolver.test.js +1 -1
- package/dist/__tests__/daemon-rpc-protocol.test.js +95 -0
- package/dist/__tests__/daemon-runtime.test.js +145 -0
- package/dist/__tests__/graph-tools-store.test.js +314 -0
- package/dist/__tests__/graph-tools.test.js +11 -4
- package/dist/__tests__/http-auth.test.js +180 -0
- package/dist/__tests__/http-index.test.js +125 -0
- package/dist/__tests__/metrics-collect.test.js +48 -0
- package/dist/__tests__/observability.test.js +3 -4
- package/dist/__tests__/remote-proxy.test.js +105 -0
- package/dist/__tests__/server-cli-args.test.js +146 -0
- package/dist/__tests__/server-lifecycle.test.js +108 -0
- package/dist/__tests__/tool-inputs.test.js +178 -0
- package/dist/backends/in-process.js +5 -2
- package/dist/backends/index.js +2 -2
- package/dist/cli/index.js +143 -76
- package/dist/core/config.js +106 -35
- package/dist/core/env-config.js +3 -5
- package/dist/core/project-name.js +12 -0
- package/dist/core/version.js +5 -0
- package/dist/daemon/index.js +106 -65
- package/dist/daemon/rpc-server.js +38 -18
- package/dist/index.js +113 -89
- package/dist/middleware/auth.js +34 -66
- package/dist/middleware/rate-limit.js +37 -0
- package/dist/observability/logger.js +10 -2
- package/dist/observability/metrics.js +7 -6
- package/dist/projects/registry.js +13 -15
- package/dist/resources/resources.js +17 -0
- package/dist/server.js +216 -129
- package/dist/tools/graph.js +353 -133
- package/dist/tools/index-tools.js +3 -2
- package/dist/tools/profile.js +14 -8
- package/dist/tools/search.js +61 -51
- package/dist/tools/validate.js +48 -0
- package/dist/transports/http-transport.js +94 -55
- package/dist/transports/remote-proxy.js +58 -97
- package/package.json +1 -1
package/dist/core/config.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
3
|
import { homedir, tmpdir } from "node:os";
|
|
4
|
+
import { Logger } from "../observability/logger.js";
|
|
4
5
|
function defaultDataDir() {
|
|
5
|
-
return process.env.CODE_SEARCH_DATA_DIR ?? join(homedir(), ".
|
|
6
|
+
return process.env.CODE_SEARCH_DATA_DIR ?? join(process.env.ANVIL_HOME || join(homedir(), ".anvil"), "knowledge-base");
|
|
6
7
|
}
|
|
7
8
|
const DEFAULTS = {
|
|
8
9
|
server: {
|
|
@@ -59,7 +60,6 @@ const DEFAULTS = {
|
|
|
59
60
|
recordQueries: false
|
|
60
61
|
},
|
|
61
62
|
daemon: {
|
|
62
|
-
autoSpawn: false,
|
|
63
63
|
disabled: false
|
|
64
64
|
}
|
|
65
65
|
};
|
|
@@ -159,14 +159,16 @@ function parseHeaders(s) {
|
|
|
159
159
|
}
|
|
160
160
|
return Object.keys(out).length ? out : void 0;
|
|
161
161
|
}
|
|
162
|
-
function
|
|
163
|
-
if (
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
162
|
+
function parseReindexInterval(raw) {
|
|
163
|
+
if (raw === void 0 || raw === null || raw === "" || raw === 0 || raw === "0" || raw === "none") return 0;
|
|
164
|
+
const s = String(raw).trim();
|
|
165
|
+
const unit = s.match(/^(\d+)(m|h)$/);
|
|
166
|
+
if (unit) return parseInt(unit[1], 10) * (unit[2] === "h" ? 36e5 : 6e4);
|
|
167
|
+
if (/^\d+$/.test(s)) {
|
|
168
|
+
const ms = parseInt(s, 10);
|
|
169
|
+
return ms >= 6e4 ? ms : null;
|
|
168
170
|
}
|
|
169
|
-
return
|
|
171
|
+
return null;
|
|
170
172
|
}
|
|
171
173
|
function envStorageBackends(env) {
|
|
172
174
|
const blobBackend = env("STORAGE_BLOB_BACKEND");
|
|
@@ -220,7 +222,7 @@ function envLayer() {
|
|
|
220
222
|
server: {
|
|
221
223
|
transport,
|
|
222
224
|
port: port ? parseInt(port, 10) : void 0,
|
|
223
|
-
host: env("HOST")
|
|
225
|
+
host: env("HOST")
|
|
224
226
|
},
|
|
225
227
|
auth: {
|
|
226
228
|
mode: authMode,
|
|
@@ -253,7 +255,8 @@ function envLayer() {
|
|
|
253
255
|
},
|
|
254
256
|
indexing: {
|
|
255
257
|
autoIndex: env("AUTO_INDEX") !== void 0 ? env("AUTO_INDEX") !== "false" && env("AUTO_INDEX") !== "0" : void 0,
|
|
256
|
-
|
|
258
|
+
// Raw string; normalised (and validated) by the resolver for every layer.
|
|
259
|
+
reindexIntervalMs: env("REINDEX_INTERVAL"),
|
|
257
260
|
chunking: {
|
|
258
261
|
maxTokens: chunkMaxTokens ? parseInt(chunkMaxTokens, 10) : void 0
|
|
259
262
|
},
|
|
@@ -278,7 +281,6 @@ function envLayer() {
|
|
|
278
281
|
recordQueries: env("TELEMETRY_RECORD_QUERIES") === "1" || env("TELEMETRY_RECORD_QUERIES") === "true" ? true : void 0
|
|
279
282
|
},
|
|
280
283
|
daemon: {
|
|
281
|
-
autoSpawn: env("DAEMON_AUTO_SPAWN") === "1" || env("DAEMON_AUTO_SPAWN") === "true" ? true : void 0,
|
|
282
284
|
disabled: env("DAEMON_DISABLED") === "1" || env("DAEMON_DISABLED") === "true" ? true : void 0
|
|
283
285
|
}
|
|
284
286
|
};
|
|
@@ -296,30 +298,64 @@ const RESERVED_FLAGS = /* @__PURE__ */ new Set([
|
|
|
296
298
|
"--force",
|
|
297
299
|
"--workspace",
|
|
298
300
|
"--no-daemon",
|
|
299
|
-
"--auto-spawn-daemon",
|
|
300
|
-
"--config",
|
|
301
301
|
"--format",
|
|
302
302
|
"--top-k",
|
|
303
303
|
"--mode",
|
|
304
|
-
"--repo"
|
|
305
|
-
"--language"
|
|
304
|
+
"--repo"
|
|
306
305
|
]);
|
|
306
|
+
const ROOT_ALIASES = {
|
|
307
|
+
"--port": ["server", "port"],
|
|
308
|
+
"--host": ["server", "host"],
|
|
309
|
+
"--transport": ["server", "transport"],
|
|
310
|
+
"--auth": ["auth", "mode"]
|
|
311
|
+
};
|
|
312
|
+
const OPTIONAL_LEAVES = [
|
|
313
|
+
"auth.jwtSecret",
|
|
314
|
+
"embedding.model",
|
|
315
|
+
"embedding.apiKey",
|
|
316
|
+
"embedding.baseUrl",
|
|
317
|
+
"embedding.ollamaHost",
|
|
318
|
+
"reranker.model",
|
|
319
|
+
"reranker.apiKey",
|
|
320
|
+
"reranker.baseUrl",
|
|
321
|
+
"reranker.timeoutMs",
|
|
322
|
+
"llm.apiKey",
|
|
323
|
+
"llm.baseUrl",
|
|
324
|
+
"github.token",
|
|
325
|
+
"telemetry.otelEndpoint"
|
|
326
|
+
];
|
|
327
|
+
const OPEN_SUBTREES = ["storage.backends.", "telemetry.otelHeaders."];
|
|
328
|
+
function leafPaths(obj, prefix, out) {
|
|
329
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
330
|
+
if (isPlainObject(v)) leafPaths(v, `${prefix}${k}.`, out);
|
|
331
|
+
else out.add(`${prefix}${k}`);
|
|
332
|
+
}
|
|
333
|
+
return out;
|
|
334
|
+
}
|
|
335
|
+
const KNOWN_LEAVES = leafPaths(DEFAULTS, "", new Set(OPTIONAL_LEAVES));
|
|
336
|
+
function isKnownPath(path) {
|
|
337
|
+
return KNOWN_LEAVES.has(path) || OPEN_SUBTREES.some((p) => path.startsWith(p));
|
|
338
|
+
}
|
|
307
339
|
function parseCliFlags(argv) {
|
|
308
340
|
const patch = {};
|
|
309
341
|
const rest = [];
|
|
342
|
+
const unknown = [];
|
|
310
343
|
for (let i = 0; i < argv.length; i++) {
|
|
311
344
|
const arg = argv[i];
|
|
312
345
|
if (!arg.startsWith("--") || RESERVED_FLAGS.has(arg)) {
|
|
313
346
|
rest.push(arg);
|
|
314
347
|
continue;
|
|
315
348
|
}
|
|
316
|
-
let
|
|
349
|
+
let path = ROOT_ALIASES[arg];
|
|
317
350
|
let negate = false;
|
|
318
|
-
if (
|
|
319
|
-
|
|
320
|
-
|
|
351
|
+
if (!path) {
|
|
352
|
+
let key = arg.slice(2);
|
|
353
|
+
if (key.startsWith("no-")) {
|
|
354
|
+
negate = true;
|
|
355
|
+
key = key.slice(3);
|
|
356
|
+
}
|
|
357
|
+
path = key.split(".").map((seg) => seg.replace(/-([a-z])/g, (_, c) => c.toUpperCase()));
|
|
321
358
|
}
|
|
322
|
-
const path = key.split(".").map((seg) => seg.replace(/-([a-z])/g, (_, c) => c.toUpperCase()));
|
|
323
359
|
let val;
|
|
324
360
|
if (negate) {
|
|
325
361
|
val = false;
|
|
@@ -328,9 +364,13 @@ function parseCliFlags(argv) {
|
|
|
328
364
|
} else {
|
|
329
365
|
val = true;
|
|
330
366
|
}
|
|
367
|
+
if (!isKnownPath(path.join("."))) {
|
|
368
|
+
unknown.push(arg);
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
331
371
|
setDeep(patch, path, val);
|
|
332
372
|
}
|
|
333
|
-
return { patch, rest };
|
|
373
|
+
return { patch, rest, unknown };
|
|
334
374
|
}
|
|
335
375
|
function setDeep(obj, path, value) {
|
|
336
376
|
let cur = obj;
|
|
@@ -347,10 +387,33 @@ function resolveCodeSearchConfig(opts = {}) {
|
|
|
347
387
|
const yaml2 = opts.workspaceDir ? loadYamlFile(join(opts.workspaceDir, ".code-search.yaml")) ?? {} : {};
|
|
348
388
|
const env = envLayer();
|
|
349
389
|
const cli = opts.cli ?? {};
|
|
350
|
-
const
|
|
390
|
+
const layers = [yaml1, yaml2, env, cli];
|
|
391
|
+
const cfg = deepMerge(
|
|
392
|
+
structuredClone(DEFAULTS),
|
|
393
|
+
...layers
|
|
394
|
+
);
|
|
395
|
+
if (cfg.auth.mode !== "none" && !layers.some((l) => l.server?.host !== void 0)) {
|
|
396
|
+
cfg.server.host = "0.0.0.0";
|
|
397
|
+
}
|
|
398
|
+
const rawInterval = cfg.indexing.reindexIntervalMs;
|
|
399
|
+
const intervalMs = parseReindexInterval(rawInterval);
|
|
400
|
+
if (intervalMs === null) {
|
|
401
|
+
warnOnce(cfg, `[code-search] invalid reindex interval ${JSON.stringify(String(rawInterval))} (use 30m, 1h, \u2026 \u2014 at least one minute \u2014 or 0); auto-reindex disabled`);
|
|
402
|
+
}
|
|
403
|
+
cfg.indexing.reindexIntervalMs = intervalMs ?? 0;
|
|
404
|
+
cfg.storage.dataDir = resolve(cfg.storage.dataDir);
|
|
351
405
|
bridgeLegacyEnvVars(cfg);
|
|
352
406
|
return cfg;
|
|
353
407
|
}
|
|
408
|
+
const warned = /* @__PURE__ */ new Set();
|
|
409
|
+
function warnOnce(cfg, msg) {
|
|
410
|
+
if (warned.has(msg)) return;
|
|
411
|
+
warned.add(msg);
|
|
412
|
+
new Logger({ structured: cfg.telemetry.structuredLogs }).warn(msg);
|
|
413
|
+
}
|
|
414
|
+
function exportDataDir(cfg) {
|
|
415
|
+
process.env.CODE_SEARCH_DATA_DIR = cfg.storage.dataDir;
|
|
416
|
+
}
|
|
354
417
|
function bridgeLegacyEnvVars(cfg) {
|
|
355
418
|
process.env.ANVIL_LLM_MODE ??= process.env.CODE_SEARCH_LLM_MODE ?? cfg.llm.mode;
|
|
356
419
|
process.env.ANVIL_LLM_PROVIDER ??= process.env.CODE_SEARCH_LLM_PROVIDER ?? cfg.llm.provider;
|
|
@@ -362,7 +425,7 @@ function bridgeLegacyEnvVars(cfg) {
|
|
|
362
425
|
function toKnowledgeConfig(c) {
|
|
363
426
|
return {
|
|
364
427
|
embedding: { ...c.embedding },
|
|
365
|
-
chunking: { ...c.indexing.chunking },
|
|
428
|
+
chunking: { ...c.indexing.chunking, ignorePatterns: c.indexing.ignorePatterns },
|
|
366
429
|
retrieval: {
|
|
367
430
|
maxChunks: c.retrieval.maxChunks,
|
|
368
431
|
maxTokens: c.retrieval.maxTokens,
|
|
@@ -373,23 +436,29 @@ function toKnowledgeConfig(c) {
|
|
|
373
436
|
...c.storage.backends ? { storage: c.storage.backends } : {}
|
|
374
437
|
};
|
|
375
438
|
}
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
]);
|
|
439
|
+
const SECRET_KEY = /pass(word)?|secret|token|authorization|api[-_]?keys?|credential/i;
|
|
440
|
+
const URI_USERINFO = /([a-z][a-z0-9+.-]*:\/\/)([^/\s@]+)@/gi;
|
|
441
|
+
function redactString(v) {
|
|
442
|
+
return `***redacted (${v.length} chars)`;
|
|
443
|
+
}
|
|
382
444
|
function redactSecrets(value, parentKey = "") {
|
|
445
|
+
if (typeof value === "string") {
|
|
446
|
+
return value.replace(URI_USERINFO, (_m, scheme, info) => {
|
|
447
|
+
const colon = info.indexOf(":");
|
|
448
|
+
return `${scheme}${colon >= 0 ? info.slice(0, colon + 1) : ""}***@`;
|
|
449
|
+
});
|
|
450
|
+
}
|
|
383
451
|
if (Array.isArray(value)) {
|
|
384
452
|
return value.map((v) => redactSecrets(v, parentKey));
|
|
385
453
|
}
|
|
386
454
|
if (isPlainObject(value)) {
|
|
387
455
|
const out = {};
|
|
388
456
|
for (const [k, v] of Object.entries(value)) {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
457
|
+
const secret = SECRET_KEY.test(k) || parentKey === "otelHeaders";
|
|
458
|
+
if (secret && typeof v === "string" && v.length > 0) {
|
|
459
|
+
out[k] = redactString(v);
|
|
460
|
+
} else if (secret && Array.isArray(v)) {
|
|
461
|
+
out[k] = v.map((s) => typeof s === "string" ? redactString(s) : redactSecrets(s, k));
|
|
393
462
|
} else {
|
|
394
463
|
out[k] = redactSecrets(v, k);
|
|
395
464
|
}
|
|
@@ -403,7 +472,9 @@ function printConfig(c) {
|
|
|
403
472
|
}
|
|
404
473
|
export {
|
|
405
474
|
DEFAULTS,
|
|
475
|
+
exportDataDir,
|
|
406
476
|
parseCliFlags,
|
|
477
|
+
parseReindexInterval,
|
|
407
478
|
printConfig,
|
|
408
479
|
redactSecrets,
|
|
409
480
|
resolveCodeSearchConfig,
|
package/dist/core/env-config.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { execSync as _execSync } from "node:child_process";
|
|
2
2
|
import {
|
|
3
3
|
resolveCodeSearchConfig,
|
|
4
|
-
parseCliFlags,
|
|
5
4
|
toKnowledgeConfig
|
|
6
5
|
} from "./config.js";
|
|
7
6
|
import { normalizeRerankerConfig } from "@esankhan3/anvil-knowledge-core";
|
|
8
7
|
let _config = null;
|
|
9
8
|
function resolveLlmMode(explicit, apiKey, claudeBin) {
|
|
10
|
-
if (explicit
|
|
9
|
+
if (explicit === "cli" || explicit === "api" || explicit === "none") {
|
|
11
10
|
if (explicit === "api" && !apiKey) {
|
|
12
11
|
console.error(
|
|
13
12
|
`[code-search-mcp] WARNING: LLM_MODE=api but no API key found. Set CODE_SEARCH_LLM_API_KEY or ANTHROPIC_API_KEY. Falling back to LLM_MODE=none.`
|
|
@@ -32,10 +31,9 @@ function resolvedKnowledgeConfig() {
|
|
|
32
31
|
}
|
|
33
32
|
function loadServerConfig(opts) {
|
|
34
33
|
if (_config) return _config;
|
|
35
|
-
const
|
|
36
|
-
const c = resolveCodeSearchConfig({ cli, workspaceDir: opts?.workspaceDir });
|
|
34
|
+
const c = resolveCodeSearchConfig({ cli: opts?.cli, workspaceDir: opts?.workspaceDir });
|
|
37
35
|
const rerankerStruct = normalizeRerankerConfig(c.reranker);
|
|
38
|
-
const llmMode = resolveLlmMode(c.llm.mode, c.llm.apiKey, c.llm.claudeBin);
|
|
36
|
+
const llmMode = resolveLlmMode(process.env.ANVIL_LLM_MODE ?? c.llm.mode, c.llm.apiKey, c.llm.claudeBin);
|
|
39
37
|
_config = {
|
|
40
38
|
port: c.server.port,
|
|
41
39
|
host: c.server.host,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const RESERVED = /* @__PURE__ */ new Set(["daemon", "projects"]);
|
|
2
|
+
function validateProjectName(name) {
|
|
3
|
+
if (!name || name === "." || name === ".." || /[/\\\0]/.test(name) || RESERVED.has(name)) {
|
|
4
|
+
throw new Error(
|
|
5
|
+
`Invalid project name ${JSON.stringify(name)}: a project name must be a single path segment (no "/" or "\\", not "." or "..") and not one of the reserved names: ${[...RESERVED].join(", ")}.`
|
|
6
|
+
);
|
|
7
|
+
}
|
|
8
|
+
return name;
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
validateProjectName
|
|
12
|
+
};
|
package/dist/daemon/index.js
CHANGED
|
@@ -1,63 +1,83 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { writeFileSync, existsSync } from "node:fs";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import { resolveCodeSearchConfig, parseCliFlags, toKnowledgeConfig } from "../core/config.js";
|
|
2
|
+
import { writeFileSync, existsSync, readFileSync, unlinkSync, mkdirSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
4
|
+
import { resolveCodeSearchConfig, parseCliFlags, toKnowledgeConfig, exportDataDir } from "../core/config.js";
|
|
5
|
+
import { validateProjectName } from "../core/project-name.js";
|
|
5
6
|
import { InProcessBackend } from "../backends/in-process.js";
|
|
6
|
-
import { daemonSocketPath } from "../backends/index.js";
|
|
7
|
+
import { DaemonBackend, daemonSocketPath } from "../backends/index.js";
|
|
8
|
+
import { configureLogger, getComponentLogger } from "../observability/logger.js";
|
|
7
9
|
import { RpcServer } from "./rpc-server.js";
|
|
8
10
|
import { Watcher } from "./watcher.js";
|
|
11
|
+
const log = getComponentLogger("code-search-daemon");
|
|
12
|
+
function usageError(msg) {
|
|
13
|
+
process.stderr.write(`code-search-daemon: ${msg}
|
|
14
|
+
`);
|
|
15
|
+
process.exit(2);
|
|
16
|
+
}
|
|
9
17
|
async function main() {
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
let
|
|
18
|
+
const { patch, rest, unknown } = parseCliFlags(process.argv.slice(2));
|
|
19
|
+
if (unknown.length) usageError(`unknown flag(s): ${unknown.join(", ")}`);
|
|
20
|
+
let workspaceArg = null;
|
|
13
21
|
for (let i = 0; i < rest.length; i++) {
|
|
14
|
-
if (rest[i] === "--workspace" && rest[i + 1])
|
|
15
|
-
|
|
16
|
-
if (!workspaceDir) {
|
|
17
|
-
process.stderr.write("code-search-daemon: missing --workspace <path>\n");
|
|
18
|
-
process.exit(2);
|
|
19
|
-
}
|
|
20
|
-
if (!existsSync(workspaceDir)) {
|
|
21
|
-
process.stderr.write(`code-search-daemon: workspace does not exist: ${workspaceDir}
|
|
22
|
-
`);
|
|
23
|
-
process.exit(2);
|
|
22
|
+
if (rest[i] === "--workspace" && rest[i + 1]) workspaceArg = rest[++i];
|
|
23
|
+
else usageError(`unexpected argument: ${rest[i]} (usage: code-search-daemon --workspace <path> [--<config.path> <value>])`);
|
|
24
24
|
}
|
|
25
|
+
if (!workspaceArg) usageError("missing --workspace <path>");
|
|
26
|
+
const workspaceDir = resolve(workspaceArg);
|
|
27
|
+
if (!existsSync(workspaceDir)) usageError(`workspace does not exist: ${workspaceDir}`);
|
|
25
28
|
const cfg = resolveCodeSearchConfig({ cli: patch, workspaceDir });
|
|
26
|
-
|
|
29
|
+
configureLogger({ structured: cfg.telemetry.structuredLogs });
|
|
30
|
+
exportDataDir(cfg);
|
|
31
|
+
let project;
|
|
32
|
+
try {
|
|
33
|
+
project = validateProjectName(basename(workspaceDir));
|
|
34
|
+
} catch (err) {
|
|
35
|
+
usageError(err.message);
|
|
36
|
+
}
|
|
27
37
|
const knowledge = toKnowledgeConfig(cfg);
|
|
38
|
+
const socketPath = daemonSocketPath(cfg.storage.dataDir, project);
|
|
39
|
+
const pidFile = join(cfg.storage.dataDir, "daemon", `${project}.pid`);
|
|
40
|
+
if (existsSync(socketPath)) {
|
|
41
|
+
const live = await new DaemonBackend({ project, workspaceDir, knowledge, preferDaemon: true, socketPath }).ping();
|
|
42
|
+
if (live) {
|
|
43
|
+
const pid = existsSync(pidFile) ? readFileSync(pidFile, "utf-8").trim() : "unknown";
|
|
44
|
+
log.error(`a daemon for project "${project}" is already running (pid ${pid}, socket ${socketPath}) \u2014 not starting another`);
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
if (process.platform !== "win32") {
|
|
48
|
+
try {
|
|
49
|
+
unlinkSync(socketPath);
|
|
50
|
+
} catch {
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
28
54
|
const backend = new InProcessBackend({
|
|
29
55
|
project,
|
|
30
56
|
workspaceDir,
|
|
31
57
|
knowledge,
|
|
32
58
|
preferDaemon: false
|
|
33
59
|
});
|
|
34
|
-
|
|
35
|
-
`);
|
|
36
|
-
process.stderr.write("[code-search-daemon] building initial index...\n");
|
|
37
|
-
await backend.forceIndex();
|
|
38
|
-
process.stderr.write("[code-search-daemon] initial index ready.\n");
|
|
39
|
-
const socketPath = daemonSocketPath(cfg.storage.dataDir, project);
|
|
40
|
-
const pidFile = join(cfg.storage.dataDir, "daemon", `${project}.pid`);
|
|
60
|
+
let tail = Promise.resolve();
|
|
41
61
|
let queueDepth = 0;
|
|
42
|
-
let
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
62
|
+
let watcherRunPending = false;
|
|
63
|
+
const enqueue = (reason, run) => {
|
|
64
|
+
queueDepth++;
|
|
65
|
+
const job = tail.then(async () => {
|
|
66
|
+
log.info(`reindex started (${reason})`);
|
|
67
|
+
try {
|
|
68
|
+
const out = await run();
|
|
69
|
+
log.info(`reindex finished (${reason})`);
|
|
70
|
+
return out;
|
|
71
|
+
} catch (err) {
|
|
72
|
+
log.error(`reindex failed (${reason}): ${err instanceof Error ? err.message : err}`);
|
|
73
|
+
throw err;
|
|
74
|
+
} finally {
|
|
53
75
|
queueDepth--;
|
|
54
76
|
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
indexing = false;
|
|
60
|
-
}
|
|
77
|
+
});
|
|
78
|
+
tail = job.catch(() => {
|
|
79
|
+
});
|
|
80
|
+
return job;
|
|
61
81
|
};
|
|
62
82
|
const watcher = new Watcher({
|
|
63
83
|
workspaceDir,
|
|
@@ -65,16 +85,18 @@ async function main() {
|
|
|
65
85
|
debounceMs: cfg.indexing.debounceMs
|
|
66
86
|
});
|
|
67
87
|
watcher.on("error", (err) => {
|
|
68
|
-
|
|
69
|
-
`);
|
|
88
|
+
log.error(`watcher error: ${err instanceof Error ? err.message : err}`);
|
|
70
89
|
});
|
|
71
90
|
watcher.onBatch((batch) => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
91
|
+
log.info(`reindex queued (${batch.changed.size} changed, ${batch.removed.size} removed)`);
|
|
92
|
+
if (watcherRunPending) return;
|
|
93
|
+
watcherRunPending = true;
|
|
94
|
+
enqueue("watcher", () => {
|
|
95
|
+
watcherRunPending = false;
|
|
96
|
+
return backend.forceIndex();
|
|
97
|
+
}).catch(() => {
|
|
98
|
+
});
|
|
76
99
|
});
|
|
77
|
-
watcher.start();
|
|
78
100
|
const startedAt = Date.now();
|
|
79
101
|
const rpc = new RpcServer({
|
|
80
102
|
socketPath,
|
|
@@ -84,36 +106,55 @@ async function main() {
|
|
|
84
106
|
const s = await backend.status();
|
|
85
107
|
return { ...s, watching: true, queueDepth };
|
|
86
108
|
},
|
|
87
|
-
"index.force":
|
|
88
|
-
await backend.forceIndex({ force: p?.force });
|
|
89
|
-
return backend.status();
|
|
90
|
-
},
|
|
109
|
+
"index.force": (p) => enqueue("rpc", () => backend.forceIndex({ force: p?.force })),
|
|
91
110
|
"index.invalidate": async (p) => {
|
|
92
|
-
await backend.invalidate(p
|
|
111
|
+
await backend.invalidate(p?.paths ?? []);
|
|
93
112
|
return { ok: true };
|
|
94
113
|
},
|
|
95
114
|
"health": () => ({ ok: true, uptime: Math.floor((Date.now() - startedAt) / 1e3) })
|
|
96
115
|
}
|
|
97
116
|
});
|
|
98
|
-
await rpc.start();
|
|
99
|
-
process.stderr.write(`[code-search-daemon] RPC listening on ${socketPath}
|
|
100
|
-
`);
|
|
101
117
|
try {
|
|
118
|
+
await rpc.start();
|
|
119
|
+
} catch (err) {
|
|
120
|
+
log.error(`cannot listen on ${socketPath}: ${err instanceof Error ? err.message : err} (another daemon for "${project}" starting?)`);
|
|
121
|
+
process.exit(1);
|
|
122
|
+
}
|
|
123
|
+
log.info(`RPC listening on ${socketPath}`);
|
|
124
|
+
try {
|
|
125
|
+
mkdirSync(dirname(pidFile), { recursive: true });
|
|
102
126
|
writeFileSync(pidFile, String(process.pid), "utf-8");
|
|
103
127
|
} catch {
|
|
104
128
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
129
|
+
let stopping = false;
|
|
130
|
+
const stop = async (code) => {
|
|
131
|
+
if (stopping) return;
|
|
132
|
+
stopping = true;
|
|
108
133
|
watcher.stop();
|
|
109
134
|
await rpc.stop();
|
|
110
|
-
|
|
135
|
+
try {
|
|
136
|
+
if (readFileSync(pidFile, "utf-8").trim() === String(process.pid)) unlinkSync(pidFile);
|
|
137
|
+
} catch {
|
|
138
|
+
}
|
|
139
|
+
process.exit(code);
|
|
111
140
|
};
|
|
112
|
-
|
|
113
|
-
|
|
141
|
+
for (const sig of ["SIGINT", "SIGTERM"]) {
|
|
142
|
+
process.on(sig, () => {
|
|
143
|
+
log.info(`${sig} \u2014 draining...`);
|
|
144
|
+
void stop(0);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
log.info(`starting for project="${project}", workspace=${workspaceDir}`);
|
|
148
|
+
log.info("building initial index...");
|
|
149
|
+
try {
|
|
150
|
+
await enqueue("initial", () => backend.forceIndex());
|
|
151
|
+
} catch {
|
|
152
|
+
await stop(1);
|
|
153
|
+
}
|
|
154
|
+
log.info("initial index ready.");
|
|
155
|
+
watcher.start();
|
|
114
156
|
}
|
|
115
157
|
main().catch((err) => {
|
|
116
|
-
|
|
117
|
-
`);
|
|
158
|
+
log.error(`fatal: ${err instanceof Error ? err.stack : err}`);
|
|
118
159
|
process.exit(1);
|
|
119
160
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createServer } from "node:net";
|
|
2
|
-
import {
|
|
2
|
+
import { mkdirSync, statSync } from "node:fs";
|
|
3
3
|
import { dirname } from "node:path";
|
|
4
4
|
import { EventEmitter } from "node:events";
|
|
5
5
|
class RpcServer extends EventEmitter {
|
|
@@ -7,18 +7,16 @@ class RpcServer extends EventEmitter {
|
|
|
7
7
|
handlers;
|
|
8
8
|
server = null;
|
|
9
9
|
clients = /* @__PURE__ */ new Set();
|
|
10
|
+
/** Inode of the socket file this server created — stop() unlinks only that. */
|
|
11
|
+
socketIno = null;
|
|
10
12
|
constructor(opts) {
|
|
11
13
|
super();
|
|
12
14
|
this.socketPath = opts.socketPath;
|
|
13
15
|
this.handlers = opts.handlers;
|
|
14
16
|
}
|
|
17
|
+
/** Listen on the socket. An existing socket file is NOT removed — the
|
|
18
|
+
* caller decides whether it is stale; listening then fails EADDRINUSE. */
|
|
15
19
|
async start() {
|
|
16
|
-
if (process.platform !== "win32" && existsSync(this.socketPath)) {
|
|
17
|
-
try {
|
|
18
|
-
unlinkSync(this.socketPath);
|
|
19
|
-
} catch {
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
20
|
mkdirSync(dirname(this.socketPath), { recursive: true });
|
|
23
21
|
await new Promise((resolveP, rejectP) => {
|
|
24
22
|
this.server = createServer((sock) => this.handleClient(sock));
|
|
@@ -27,6 +25,12 @@ class RpcServer extends EventEmitter {
|
|
|
27
25
|
rejectP(err);
|
|
28
26
|
});
|
|
29
27
|
this.server.listen(this.socketPath, () => {
|
|
28
|
+
if (process.platform !== "win32") {
|
|
29
|
+
try {
|
|
30
|
+
this.socketIno = statSync(this.socketPath).ino;
|
|
31
|
+
} catch {
|
|
32
|
+
}
|
|
33
|
+
}
|
|
30
34
|
this.emit("listening", this.socketPath);
|
|
31
35
|
resolveP();
|
|
32
36
|
});
|
|
@@ -41,14 +45,20 @@ class RpcServer extends EventEmitter {
|
|
|
41
45
|
}
|
|
42
46
|
this.clients.clear();
|
|
43
47
|
if (this.server) {
|
|
44
|
-
|
|
48
|
+
if (process.platform === "win32" || this.ownsSocketFile()) {
|
|
49
|
+
await new Promise((r) => this.server.close(() => r()));
|
|
50
|
+
} else {
|
|
51
|
+
this.server.unref();
|
|
52
|
+
}
|
|
45
53
|
this.server = null;
|
|
46
54
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
55
|
+
this.socketIno = null;
|
|
56
|
+
}
|
|
57
|
+
ownsSocketFile() {
|
|
58
|
+
try {
|
|
59
|
+
return this.socketIno !== null && statSync(this.socketPath).ino === this.socketIno;
|
|
60
|
+
} catch {
|
|
61
|
+
return false;
|
|
52
62
|
}
|
|
53
63
|
}
|
|
54
64
|
handleClient(sock) {
|
|
@@ -77,17 +87,27 @@ class RpcServer extends EventEmitter {
|
|
|
77
87
|
sock.write(this.errorEnvelope(null, -32700, err instanceof Error ? err.message : String(err)));
|
|
78
88
|
return;
|
|
79
89
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
90
|
+
if (Array.isArray(req)) {
|
|
91
|
+
sock.write(this.errorEnvelope(null, -32600, "Batch requests are not supported"));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (typeof req !== "object" || req === null || typeof req.method !== "string") {
|
|
95
|
+
sock.write(this.errorEnvelope(null, -32600, "Invalid request"));
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const isNotification = !Object.prototype.hasOwnProperty.call(req, "id");
|
|
99
|
+
const id = req.id ?? null;
|
|
100
|
+
const handler = Object.prototype.hasOwnProperty.call(this.handlers, req.method) ? this.handlers[req.method] : void 0;
|
|
101
|
+
if (typeof handler !== "function") {
|
|
102
|
+
if (!isNotification) sock.write(this.errorEnvelope(id, -32601, `Method not found: ${req.method}`));
|
|
83
103
|
return;
|
|
84
104
|
}
|
|
85
105
|
try {
|
|
86
106
|
const result = await handler(req.params);
|
|
87
|
-
sock.write(JSON.stringify({ jsonrpc: "2.0", id
|
|
107
|
+
if (!isNotification) sock.write(JSON.stringify({ jsonrpc: "2.0", id, result }) + "\n");
|
|
88
108
|
} catch (err) {
|
|
89
109
|
const msg = err instanceof Error ? err.message : String(err);
|
|
90
|
-
sock.write(this.errorEnvelope(
|
|
110
|
+
if (!isNotification) sock.write(this.errorEnvelope(id, -32e3, msg));
|
|
91
111
|
}
|
|
92
112
|
}
|
|
93
113
|
errorEnvelope(id, code, message) {
|