opencode-codebase-index 0.8.1 → 0.9.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/dist/cli.js CHANGED
@@ -487,7 +487,7 @@ var require_ignore = __commonJS({
487
487
  // path matching.
488
488
  // - check `string` either `MODE_IGNORE` or `MODE_CHECK_IGNORE`
489
489
  // @returns {TestResult} true if a file is ignored
490
- test(path13, checkUnignored, mode) {
490
+ test(path18, checkUnignored, mode) {
491
491
  let ignored = false;
492
492
  let unignored = false;
493
493
  let matchedRule;
@@ -496,7 +496,7 @@ var require_ignore = __commonJS({
496
496
  if (unignored === negative && ignored !== unignored || negative && !ignored && !unignored && !checkUnignored) {
497
497
  return;
498
498
  }
499
- const matched = rule[mode].test(path13);
499
+ const matched = rule[mode].test(path18);
500
500
  if (!matched) {
501
501
  return;
502
502
  }
@@ -517,17 +517,17 @@ var require_ignore = __commonJS({
517
517
  var throwError = (message, Ctor) => {
518
518
  throw new Ctor(message);
519
519
  };
520
- var checkPath = (path13, originalPath, doThrow) => {
521
- if (!isString(path13)) {
520
+ var checkPath = (path18, originalPath, doThrow) => {
521
+ if (!isString(path18)) {
522
522
  return doThrow(
523
523
  `path must be a string, but got \`${originalPath}\``,
524
524
  TypeError
525
525
  );
526
526
  }
527
- if (!path13) {
527
+ if (!path18) {
528
528
  return doThrow(`path must not be empty`, TypeError);
529
529
  }
530
- if (checkPath.isNotRelative(path13)) {
530
+ if (checkPath.isNotRelative(path18)) {
531
531
  const r = "`path.relative()`d";
532
532
  return doThrow(
533
533
  `path should be a ${r} string, but got "${originalPath}"`,
@@ -536,7 +536,7 @@ var require_ignore = __commonJS({
536
536
  }
537
537
  return true;
538
538
  };
539
- var isNotRelative = (path13) => REGEX_TEST_INVALID_PATH.test(path13);
539
+ var isNotRelative = (path18) => REGEX_TEST_INVALID_PATH.test(path18);
540
540
  checkPath.isNotRelative = isNotRelative;
541
541
  checkPath.convert = (p) => p;
542
542
  var Ignore2 = class {
@@ -566,19 +566,19 @@ var require_ignore = __commonJS({
566
566
  }
567
567
  // @returns {TestResult}
568
568
  _test(originalPath, cache, checkUnignored, slices) {
569
- const path13 = originalPath && checkPath.convert(originalPath);
569
+ const path18 = originalPath && checkPath.convert(originalPath);
570
570
  checkPath(
571
- path13,
571
+ path18,
572
572
  originalPath,
573
573
  this._strictPathCheck ? throwError : RETURN_FALSE
574
574
  );
575
- return this._t(path13, cache, checkUnignored, slices);
575
+ return this._t(path18, cache, checkUnignored, slices);
576
576
  }
577
- checkIgnore(path13) {
578
- if (!REGEX_TEST_TRAILING_SLASH.test(path13)) {
579
- return this.test(path13);
577
+ checkIgnore(path18) {
578
+ if (!REGEX_TEST_TRAILING_SLASH.test(path18)) {
579
+ return this.test(path18);
580
580
  }
581
- const slices = path13.split(SLASH).filter(Boolean);
581
+ const slices = path18.split(SLASH).filter(Boolean);
582
582
  slices.pop();
583
583
  if (slices.length) {
584
584
  const parent = this._t(
@@ -591,18 +591,18 @@ var require_ignore = __commonJS({
591
591
  return parent;
592
592
  }
593
593
  }
594
- return this._rules.test(path13, false, MODE_CHECK_IGNORE);
594
+ return this._rules.test(path18, false, MODE_CHECK_IGNORE);
595
595
  }
596
- _t(path13, cache, checkUnignored, slices) {
597
- if (path13 in cache) {
598
- return cache[path13];
596
+ _t(path18, cache, checkUnignored, slices) {
597
+ if (path18 in cache) {
598
+ return cache[path18];
599
599
  }
600
600
  if (!slices) {
601
- slices = path13.split(SLASH).filter(Boolean);
601
+ slices = path18.split(SLASH).filter(Boolean);
602
602
  }
603
603
  slices.pop();
604
604
  if (!slices.length) {
605
- return cache[path13] = this._rules.test(path13, checkUnignored, MODE_IGNORE);
605
+ return cache[path18] = this._rules.test(path18, checkUnignored, MODE_IGNORE);
606
606
  }
607
607
  const parent = this._t(
608
608
  slices.join(SLASH) + SLASH,
@@ -610,29 +610,29 @@ var require_ignore = __commonJS({
610
610
  checkUnignored,
611
611
  slices
612
612
  );
613
- return cache[path13] = parent.ignored ? parent : this._rules.test(path13, checkUnignored, MODE_IGNORE);
613
+ return cache[path18] = parent.ignored ? parent : this._rules.test(path18, checkUnignored, MODE_IGNORE);
614
614
  }
615
- ignores(path13) {
616
- return this._test(path13, this._ignoreCache, false).ignored;
615
+ ignores(path18) {
616
+ return this._test(path18, this._ignoreCache, false).ignored;
617
617
  }
618
618
  createFilter() {
619
- return (path13) => !this.ignores(path13);
619
+ return (path18) => !this.ignores(path18);
620
620
  }
621
621
  filter(paths) {
622
622
  return makeArray(paths).filter(this.createFilter());
623
623
  }
624
624
  // @returns {TestResult}
625
- test(path13) {
626
- return this._test(path13, this._testCache, true);
625
+ test(path18) {
626
+ return this._test(path18, this._testCache, true);
627
627
  }
628
628
  };
629
629
  var factory = (options) => new Ignore2(options);
630
- var isPathValid = (path13) => checkPath(path13 && checkPath.convert(path13), path13, RETURN_FALSE);
630
+ var isPathValid = (path18) => checkPath(path18 && checkPath.convert(path18), path18, RETURN_FALSE);
631
631
  var setupWindows = () => {
632
632
  const makePosix = (str) => /^\\\\\?\\/.test(str) || /["<>|\u0000-\u001F]+/u.test(str) ? str : str.replace(/\\/g, "/");
633
633
  checkPath.convert = makePosix;
634
634
  const REGEX_TEST_WINDOWS_PATH_ABSOLUTE = /^[a-z]:\//i;
635
- checkPath.isNotRelative = (path13) => REGEX_TEST_WINDOWS_PATH_ABSOLUTE.test(path13) || isNotRelative(path13);
635
+ checkPath.isNotRelative = (path18) => REGEX_TEST_WINDOWS_PATH_ABSOLUTE.test(path18) || isNotRelative(path18);
636
636
  };
637
637
  if (
638
638
  // Detect `process` so that it can run in browsers.
@@ -649,7 +649,7 @@ var require_ignore = __commonJS({
649
649
 
650
650
  // src/cli.ts
651
651
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
652
- import * as path12 from "path";
652
+ import * as path17 from "path";
653
653
 
654
654
  // src/config/constants.ts
655
655
  var DEFAULT_INCLUDE = [
@@ -665,7 +665,8 @@ var DEFAULT_INCLUDE = [
665
665
  "**/*.{md,mdx}",
666
666
  "**/*.{sh,bash,zsh}",
667
667
  "**/*.{txt,html,htm}",
668
- "**/*.zig"
668
+ "**/*.zig",
669
+ "**/*.gd"
669
670
  ];
670
671
  var DEFAULT_EXCLUDE = [
671
672
  "**/node_modules/**",
@@ -764,28 +765,7 @@ var AUTO_DETECT_PROVIDER_ORDER = [
764
765
  "google"
765
766
  ];
766
767
 
767
- // src/config/env-substitution.ts
768
- var ENV_REFERENCE_PATTERN = /^\{env:([A-Z_][A-Z0-9_]*)\}$/;
769
- var ENV_REFERENCE_LIKE_PATTERN = /\{env:[^}]+\}/;
770
- function substituteEnvString(value, keyPath) {
771
- const match = value.match(ENV_REFERENCE_PATTERN);
772
- if (!match) {
773
- if (ENV_REFERENCE_LIKE_PATTERN.test(value)) {
774
- throw new Error(
775
- `Invalid environment variable reference at '${keyPath}'. Expected the entire string to match '{env:VAR_NAME}' with VAR_NAME matching [A-Z_][A-Z0-9_]*.`
776
- );
777
- }
778
- return value;
779
- }
780
- const variableName = match[1];
781
- const envValue = process.env[variableName];
782
- if (envValue === void 0) {
783
- throw new Error(`Missing environment variable '${variableName}' referenced by config at '${keyPath}'.`);
784
- }
785
- return envValue;
786
- }
787
-
788
- // src/config/schema.ts
768
+ // src/config/defaults.ts
789
769
  function getDefaultIndexingConfig() {
790
770
  return {
791
771
  autoIndex: false,
@@ -817,12 +797,6 @@ function getDefaultSearchConfig() {
817
797
  routingHints: true
818
798
  };
819
799
  }
820
- function isValidFusionStrategy(value) {
821
- return value === "weighted" || value === "rrf";
822
- }
823
- function isValidRerankerProvider(value) {
824
- return value === "cohere" || value === "jina" || value === "custom";
825
- }
826
800
  function getDefaultRerankerBaseUrl(provider) {
827
801
  switch (provider) {
828
802
  case "cohere":
@@ -845,8 +819,37 @@ function getDefaultDebugConfig() {
845
819
  metrics: true
846
820
  };
847
821
  }
822
+
823
+ // src/config/env-substitution.ts
824
+ var ENV_REFERENCE_PATTERN = /^\{env:([A-Z_][A-Z0-9_]*)\}$/;
825
+ var ENV_REFERENCE_LIKE_PATTERN = /\{env:[^}]+\}/;
826
+ function substituteEnvString(value, keyPath) {
827
+ const match = value.match(ENV_REFERENCE_PATTERN);
828
+ if (!match) {
829
+ if (ENV_REFERENCE_LIKE_PATTERN.test(value)) {
830
+ throw new Error(
831
+ `Invalid environment variable reference at '${keyPath}'. Expected the entire string to match '{env:VAR_NAME}' with VAR_NAME matching [A-Z_][A-Z0-9_]*.`
832
+ );
833
+ }
834
+ return value;
835
+ }
836
+ const variableName = match[1];
837
+ const envValue = process.env[variableName];
838
+ if (envValue === void 0) {
839
+ throw new Error(`Missing environment variable '${variableName}' referenced by config at '${keyPath}'.`);
840
+ }
841
+ return envValue;
842
+ }
843
+
844
+ // src/config/validators.ts
848
845
  var VALID_SCOPES = ["project", "global"];
849
846
  var VALID_LOG_LEVELS = ["error", "warn", "info", "debug"];
847
+ function isValidFusionStrategy(value) {
848
+ return value === "weighted" || value === "rrf";
849
+ }
850
+ function isValidRerankerProvider(value) {
851
+ return value === "cohere" || value === "jina" || value === "custom";
852
+ }
850
853
  function isValidProvider(value) {
851
854
  return typeof value === "string" && Object.keys(EMBEDDING_MODELS).includes(value);
852
855
  }
@@ -874,6 +877,8 @@ function getResolvedStringArray(value, keyPath) {
874
877
  function isValidLogLevel(value) {
875
878
  return typeof value === "string" && VALID_LOG_LEVELS.includes(value);
876
879
  }
880
+
881
+ // src/config/schema.ts
877
882
  function parseConfig(raw) {
878
883
  const input = raw && typeof raw === "object" ? raw : {};
879
884
  const embeddingProviderValue = getResolvedString(input.embeddingProvider, "$root.embeddingProvider");
@@ -928,9 +933,9 @@ function parseConfig(raw) {
928
933
  const rawAdditionalInclude = input.additionalInclude;
929
934
  const additionalInclude = isStringArray(rawAdditionalInclude) ? rawAdditionalInclude.filter((p) => typeof p === "string" && p.trim().length > 0).map((p) => p.trim()) : [];
930
935
  let embeddingProvider;
931
- let embeddingModel = void 0;
932
- let customProvider = void 0;
933
- let reranker = void 0;
936
+ let embeddingModel;
937
+ let customProvider;
938
+ let reranker;
934
939
  if (embeddingProviderValue === "custom") {
935
940
  embeddingProvider = "custom";
936
941
  const rawCustom = input.customProvider && typeof input.customProvider === "object" ? input.customProvider : null;
@@ -963,7 +968,7 @@ function parseConfig(raw) {
963
968
  embeddingProvider = embeddingProviderValue;
964
969
  const rawEmbeddingModel = input.embeddingModel;
965
970
  if (typeof rawEmbeddingModel === "string") {
966
- const embeddingModelValue = substituteEnvString(rawEmbeddingModel, "$root.embeddingModel");
971
+ const embeddingModelValue = getResolvedString(rawEmbeddingModel, "$root.embeddingModel");
967
972
  if (embeddingModelValue) {
968
973
  embeddingModel = isValidModel(embeddingModelValue, embeddingProvider) ? embeddingModelValue : DEFAULT_PROVIDER_MODELS[embeddingProvider];
969
974
  }
@@ -1025,9 +1030,6 @@ var autoDetectProviders = AUTO_DETECT_PROVIDER_ORDER.filter(
1025
1030
  (provider) => provider in EMBEDDING_MODELS
1026
1031
  );
1027
1032
 
1028
- // src/eval/cli.ts
1029
- import * as path10 from "path";
1030
-
1031
1033
  // src/eval/compare.ts
1032
1034
  function metricDelta(current, baseline) {
1033
1035
  const absolute = current - baseline;
@@ -1069,9 +1071,11 @@ function compareSummaries(current, baseline, againstPath) {
1069
1071
  // src/eval/reports.ts
1070
1072
  import { mkdirSync, readFileSync, writeFileSync } from "fs";
1071
1073
  import * as path from "path";
1072
- function assertFiniteNumber(value, path13) {
1074
+
1075
+ // src/eval/report-formatters.ts
1076
+ function assertFiniteNumber(value, path18) {
1073
1077
  if (typeof value !== "number" || Number.isNaN(value) || !Number.isFinite(value)) {
1074
- throw new Error(`${path13} must be a finite number`);
1078
+ throw new Error(`${path18} must be a finite number`);
1075
1079
  }
1076
1080
  return value;
1077
1081
  }
@@ -1111,9 +1115,23 @@ function signed(value, digits = 4) {
1111
1115
  const formatted = value.toFixed(digits);
1112
1116
  return value > 0 ? `+${formatted}` : formatted;
1113
1117
  }
1118
+
1119
+ // src/eval/reports.ts
1114
1120
  function loadSummary(summaryPath, options) {
1115
- const raw = readFileSync(summaryPath, "utf-8");
1116
- return validateSummary(JSON.parse(raw), summaryPath, options);
1121
+ try {
1122
+ const raw = readFileSync(summaryPath, "utf-8");
1123
+ const parsed = JSON.parse(raw);
1124
+ return validateSummary(parsed, summaryPath, options);
1125
+ } catch (error) {
1126
+ if (error instanceof SyntaxError) {
1127
+ const message = error.message;
1128
+ throw new Error(`Failed to parse eval summary JSON at ${summaryPath}: ${message}`);
1129
+ }
1130
+ if (error instanceof Error) {
1131
+ throw error;
1132
+ }
1133
+ throw new Error(`Failed to load eval summary at ${summaryPath}: ${String(error)}`);
1134
+ }
1117
1135
  }
1118
1136
  function createRunDirectory(outputRoot, timestampOverride) {
1119
1137
  const timestamp = (timestampOverride ?? (/* @__PURE__ */ new Date()).toISOString()).replace(/[:.]/g, "-");
@@ -1245,445 +1263,106 @@ function buildPerQueryArtifact(perQuery) {
1245
1263
  }
1246
1264
 
1247
1265
  // src/eval/runner.ts
1248
- import { existsSync as existsSync7 } from "fs";
1249
- import { mkdirSync as mkdirSync4 } from "fs";
1250
- import { readFileSync as readFileSync8 } from "fs";
1251
- import { rmSync } from "fs";
1252
- import { writeFileSync as writeFileSync4 } from "fs";
1253
- import * as os5 from "os";
1254
- import * as path9 from "path";
1266
+ import { existsSync as existsSync8 } from "fs";
1267
+ import * as path12 from "path";
1255
1268
  import { performance as performance3 } from "perf_hooks";
1256
1269
 
1257
- // src/config/merger.ts
1258
- import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync as writeFileSync2 } from "fs";
1259
- import * as os2 from "os";
1260
- import * as path4 from "path";
1270
+ // src/indexer/index.ts
1271
+ import { existsSync as existsSync6, readFileSync as readFileSync6, writeFileSync as writeFileSync2, renameSync, unlinkSync, mkdirSync as mkdirSync2, promises as fsPromises2 } from "fs";
1272
+ import * as path9 from "path";
1273
+ import { performance as performance2 } from "perf_hooks";
1261
1274
 
1262
- // src/config/paths.ts
1263
- import { existsSync as existsSync2 } from "fs";
1264
- import * as os from "os";
1265
- import * as path3 from "path";
1275
+ // node_modules/eventemitter3/index.mjs
1276
+ var import_index = __toESM(require_eventemitter3(), 1);
1266
1277
 
1267
- // src/git/index.ts
1268
- import { existsSync, readFileSync as readFileSync2, readdirSync, statSync } from "fs";
1269
- import * as path2 from "path";
1270
- function readPackedRefs(gitDir) {
1271
- const packedRefsPath = path2.join(gitDir, "packed-refs");
1272
- if (!existsSync(packedRefsPath)) {
1273
- return [];
1274
- }
1275
- try {
1276
- return readFileSync2(packedRefsPath, "utf-8").split("\n").map((line) => line.trim()).filter((line) => line.length > 0 && !line.startsWith("#") && !line.startsWith("^"));
1277
- } catch {
1278
- return [];
1279
- }
1280
- }
1281
- function resolveCommonGitDir(gitDir) {
1282
- const commonDirPath = path2.join(gitDir, "commondir");
1283
- if (!existsSync(commonDirPath)) {
1284
- return gitDir;
1278
+ // node_modules/p-timeout/index.js
1279
+ var TimeoutError = class _TimeoutError extends Error {
1280
+ name = "TimeoutError";
1281
+ constructor(message, options) {
1282
+ super(message, options);
1283
+ Error.captureStackTrace?.(this, _TimeoutError);
1285
1284
  }
1286
- try {
1287
- const raw = readFileSync2(commonDirPath, "utf-8").trim();
1288
- if (!raw) {
1289
- return gitDir;
1285
+ };
1286
+ var getAbortedReason = (signal) => signal.reason ?? new DOMException("This operation was aborted.", "AbortError");
1287
+ function pTimeout(promise, options) {
1288
+ const {
1289
+ milliseconds,
1290
+ fallback,
1291
+ message,
1292
+ customTimers = { setTimeout, clearTimeout },
1293
+ signal
1294
+ } = options;
1295
+ let timer;
1296
+ let abortHandler;
1297
+ const wrappedPromise = new Promise((resolve10, reject) => {
1298
+ if (typeof milliseconds !== "number" || Math.sign(milliseconds) !== 1) {
1299
+ throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
1290
1300
  }
1291
- const resolved = path2.isAbsolute(raw) ? raw : path2.resolve(gitDir, raw);
1292
- if (existsSync(resolved)) {
1293
- return resolved;
1301
+ if (signal?.aborted) {
1302
+ reject(getAbortedReason(signal));
1303
+ return;
1294
1304
  }
1295
- } catch {
1296
- return gitDir;
1297
- }
1298
- return gitDir;
1299
- }
1300
- function resolveWorktreeMainRepoRoot(repoRoot) {
1301
- const gitDir = resolveGitDir(repoRoot);
1302
- if (!gitDir) {
1303
- return null;
1304
- }
1305
- const commonGitDir = resolveCommonGitDir(gitDir);
1306
- if (commonGitDir === gitDir || path2.basename(commonGitDir) !== ".git") {
1307
- return null;
1308
- }
1309
- const mainRepoRoot = path2.dirname(commonGitDir);
1310
- if (!existsSync(mainRepoRoot)) {
1311
- return null;
1312
- }
1313
- return path2.resolve(mainRepoRoot) === path2.resolve(repoRoot) ? null : mainRepoRoot;
1314
- }
1315
- function resolveGitDir(repoRoot) {
1316
- const gitPath = path2.join(repoRoot, ".git");
1317
- if (!existsSync(gitPath)) {
1318
- return null;
1319
- }
1320
- try {
1321
- const stat = statSync(gitPath);
1322
- if (stat.isDirectory()) {
1323
- return gitPath;
1305
+ if (signal) {
1306
+ abortHandler = () => {
1307
+ reject(getAbortedReason(signal));
1308
+ };
1309
+ signal.addEventListener("abort", abortHandler, { once: true });
1324
1310
  }
1325
- if (stat.isFile()) {
1326
- const content = readFileSync2(gitPath, "utf-8").trim();
1327
- const match = content.match(/^gitdir:\s*(.+)$/);
1328
- if (match) {
1329
- const gitdir = match[1];
1330
- const resolvedPath = path2.isAbsolute(gitdir) ? gitdir : path2.resolve(repoRoot, gitdir);
1331
- if (existsSync(resolvedPath)) {
1332
- return resolvedPath;
1311
+ promise.then(resolve10, reject);
1312
+ if (milliseconds === Number.POSITIVE_INFINITY) {
1313
+ return;
1314
+ }
1315
+ const timeoutError = new TimeoutError();
1316
+ timer = customTimers.setTimeout.call(void 0, () => {
1317
+ if (fallback) {
1318
+ try {
1319
+ resolve10(fallback());
1320
+ } catch (error) {
1321
+ reject(error);
1333
1322
  }
1323
+ return;
1334
1324
  }
1335
- }
1336
- } catch {
1337
- }
1338
- return null;
1339
- }
1340
- function isGitRepo(dir) {
1341
- return resolveGitDir(dir) !== null;
1342
- }
1343
- function getCurrentBranch(repoRoot) {
1344
- const gitDir = resolveGitDir(repoRoot);
1345
- if (!gitDir) {
1346
- return null;
1347
- }
1348
- const headPath = path2.join(gitDir, "HEAD");
1349
- if (!existsSync(headPath)) {
1350
- return null;
1351
- }
1352
- try {
1353
- const headContent = readFileSync2(headPath, "utf-8").trim();
1354
- const match = headContent.match(/^ref: refs\/heads\/(.+)$/);
1355
- if (match) {
1356
- return match[1];
1357
- }
1358
- if (/^[0-9a-f]{40}$/i.test(headContent)) {
1359
- return headContent.slice(0, 7);
1360
- }
1361
- return null;
1362
- } catch {
1363
- return null;
1364
- }
1365
- }
1366
- function getBaseBranch(repoRoot) {
1367
- const gitDir = resolveGitDir(repoRoot);
1368
- const refStoreDir = gitDir ? resolveCommonGitDir(gitDir) : null;
1369
- const candidates = ["main", "master", "develop", "trunk"];
1370
- if (refStoreDir) {
1371
- for (const candidate of candidates) {
1372
- const refPath = path2.join(refStoreDir, "refs", "heads", candidate);
1373
- if (existsSync(refPath)) {
1374
- return candidate;
1325
+ if (typeof promise.cancel === "function") {
1326
+ promise.cancel();
1375
1327
  }
1376
- const packedRefs = readPackedRefs(refStoreDir);
1377
- if (packedRefs.some((line) => line.endsWith(` refs/heads/${candidate}`))) {
1378
- return candidate;
1328
+ if (message === false) {
1329
+ resolve10();
1330
+ } else if (message instanceof Error) {
1331
+ reject(message);
1332
+ } else {
1333
+ timeoutError.message = message ?? `Promise timed out after ${milliseconds} milliseconds`;
1334
+ reject(timeoutError);
1379
1335
  }
1336
+ }, milliseconds);
1337
+ });
1338
+ const cancelablePromise = wrappedPromise.finally(() => {
1339
+ cancelablePromise.clear();
1340
+ if (abortHandler && signal) {
1341
+ signal.removeEventListener("abort", abortHandler);
1380
1342
  }
1381
- }
1382
- return getCurrentBranch(repoRoot) ?? "main";
1383
- }
1384
- function getBranchOrDefault(repoRoot) {
1385
- if (!isGitRepo(repoRoot)) {
1386
- return "default";
1387
- }
1388
- return getCurrentBranch(repoRoot) ?? "default";
1343
+ });
1344
+ cancelablePromise.clear = () => {
1345
+ customTimers.clearTimeout.call(void 0, timer);
1346
+ timer = void 0;
1347
+ };
1348
+ return cancelablePromise;
1389
1349
  }
1390
1350
 
1391
- // src/config/paths.ts
1392
- var PROJECT_CONFIG_RELATIVE_PATH = path3.join(".opencode", "codebase-index.json");
1393
- var PROJECT_INDEX_RELATIVE_PATH = path3.join(".opencode", "index");
1394
- function resolveWorktreeFallbackPath(projectRoot, relativePath) {
1395
- const mainRepoRoot = resolveWorktreeMainRepoRoot(projectRoot);
1396
- if (!mainRepoRoot) {
1397
- return null;
1351
+ // node_modules/p-queue/dist/lower-bound.js
1352
+ function lowerBound(array, value, comparator) {
1353
+ let first = 0;
1354
+ let count = array.length;
1355
+ while (count > 0) {
1356
+ const step = Math.trunc(count / 2);
1357
+ let it = first + step;
1358
+ if (comparator(array[it], value) <= 0) {
1359
+ first = ++it;
1360
+ count -= step + 1;
1361
+ } else {
1362
+ count = step;
1363
+ }
1398
1364
  }
1399
- const fallbackPath = path3.join(mainRepoRoot, relativePath);
1400
- return existsSync2(fallbackPath) ? fallbackPath : null;
1401
- }
1402
- function hasProjectConfig(projectRoot) {
1403
- return existsSync2(path3.join(projectRoot, PROJECT_CONFIG_RELATIVE_PATH));
1404
- }
1405
- function getGlobalIndexPath() {
1406
- return path3.join(os.homedir(), ".opencode", "global-index");
1407
- }
1408
- function resolveProjectConfigPath(projectRoot) {
1409
- const localConfigPath = path3.join(projectRoot, PROJECT_CONFIG_RELATIVE_PATH);
1410
- if (existsSync2(localConfigPath)) {
1411
- return localConfigPath;
1412
- }
1413
- return resolveWorktreeFallbackPath(projectRoot, PROJECT_CONFIG_RELATIVE_PATH) ?? localConfigPath;
1414
- }
1415
- function resolveProjectIndexPath(projectRoot, scope) {
1416
- if (scope === "global") {
1417
- return getGlobalIndexPath();
1418
- }
1419
- const localIndexPath = path3.join(projectRoot, PROJECT_INDEX_RELATIVE_PATH);
1420
- if (existsSync2(localIndexPath)) {
1421
- return localIndexPath;
1422
- }
1423
- if (hasProjectConfig(projectRoot)) {
1424
- return localIndexPath;
1425
- }
1426
- return resolveWorktreeFallbackPath(projectRoot, PROJECT_INDEX_RELATIVE_PATH) ?? localIndexPath;
1427
- }
1428
-
1429
- // src/config/merger.ts
1430
- function loadJsonFile(filePath) {
1431
- try {
1432
- if (existsSync3(filePath)) {
1433
- const content = readFileSync3(filePath, "utf-8");
1434
- return JSON.parse(content);
1435
- }
1436
- } catch {
1437
- }
1438
- return null;
1439
- }
1440
- function normalizeRelativeConfigPath(candidate) {
1441
- return candidate.replace(/\\/g, "/");
1442
- }
1443
- function rebasePathEntries(values, fromDir, toDir) {
1444
- if (!Array.isArray(values)) {
1445
- return [];
1446
- }
1447
- return values.filter((value) => typeof value === "string").map((value) => {
1448
- const trimmed = value.trim();
1449
- if (!trimmed || path4.isAbsolute(trimmed)) {
1450
- return trimmed;
1451
- }
1452
- return normalizeRelativeConfigPath(path4.normalize(path4.relative(toDir, path4.resolve(fromDir, trimmed))));
1453
- }).filter(Boolean);
1454
- }
1455
- function isWithinRoot(rootDir, targetPath) {
1456
- const relativePath = path4.relative(rootDir, targetPath);
1457
- return relativePath === "" || !relativePath.startsWith("..") && !path4.isAbsolute(relativePath);
1458
- }
1459
- function resolveInheritedKnowledgeBaseEntries(values, sourceRoot, targetRoot) {
1460
- if (!Array.isArray(values)) {
1461
- return [];
1462
- }
1463
- return values.filter((value) => typeof value === "string").map((value) => {
1464
- const trimmed = value.trim();
1465
- if (!trimmed) {
1466
- return trimmed;
1467
- }
1468
- if (path4.isAbsolute(trimmed)) {
1469
- if (isWithinRoot(sourceRoot, trimmed)) {
1470
- return normalizeRelativeConfigPath(path4.normalize(path4.relative(sourceRoot, trimmed) || "."));
1471
- }
1472
- return path4.normalize(trimmed);
1473
- }
1474
- const resolvedFromSource = path4.resolve(sourceRoot, trimmed);
1475
- if (isWithinRoot(sourceRoot, resolvedFromSource)) {
1476
- return normalizeRelativeConfigPath(path4.normalize(trimmed));
1477
- }
1478
- return normalizeRelativeConfigPath(path4.normalize(path4.relative(targetRoot, resolvedFromSource)));
1479
- }).filter(Boolean);
1480
- }
1481
- function materializeLocalProjectConfig(projectRoot, config) {
1482
- const localConfigPath = path4.join(projectRoot, ".opencode", "codebase-index.json");
1483
- mkdirSync2(path4.dirname(localConfigPath), { recursive: true });
1484
- writeFileSync2(localConfigPath, JSON.stringify(config, null, 2), "utf-8");
1485
- return localConfigPath;
1486
- }
1487
- function loadProjectConfigLayer(projectRoot) {
1488
- const projectConfigPath = resolveProjectConfigPath(projectRoot);
1489
- const projectConfig = loadJsonFile(projectConfigPath);
1490
- if (!projectConfig) {
1491
- return {};
1492
- }
1493
- const normalizedConfig = { ...projectConfig };
1494
- const projectConfigBaseDir = path4.dirname(path4.dirname(projectConfigPath));
1495
- if (Array.isArray(normalizedConfig.knowledgeBases)) {
1496
- normalizedConfig.knowledgeBases = resolveInheritedKnowledgeBaseEntries(
1497
- normalizedConfig.knowledgeBases,
1498
- projectConfigBaseDir,
1499
- projectRoot
1500
- );
1501
- }
1502
- return normalizedConfig;
1503
- }
1504
- function loadMergedConfig(projectRoot) {
1505
- const globalConfigPath = os2.homedir() + "/.config/opencode/codebase-index.json";
1506
- const globalConfig = loadJsonFile(globalConfigPath);
1507
- const projectConfigPath = resolveProjectConfigPath(projectRoot);
1508
- const projectConfig = loadJsonFile(projectConfigPath);
1509
- const normalizedProjectConfig = loadProjectConfigLayer(projectRoot);
1510
- if (!globalConfig && !projectConfig) {
1511
- return {};
1512
- }
1513
- if (!projectConfig && globalConfig) {
1514
- return globalConfig;
1515
- }
1516
- if (!globalConfig && projectConfig) {
1517
- return normalizedProjectConfig;
1518
- }
1519
- const merged = { ...globalConfig };
1520
- if (projectConfig && "embeddingProvider" in normalizedProjectConfig) {
1521
- merged.embeddingProvider = normalizedProjectConfig.embeddingProvider;
1522
- } else if (globalConfig && globalConfig.embeddingProvider) {
1523
- merged.embeddingProvider = globalConfig.embeddingProvider;
1524
- }
1525
- if (projectConfig && "customProvider" in normalizedProjectConfig) {
1526
- merged.customProvider = normalizedProjectConfig.customProvider;
1527
- } else if (globalConfig && globalConfig.customProvider) {
1528
- merged.customProvider = globalConfig.customProvider;
1529
- }
1530
- if (projectConfig && "embeddingModel" in normalizedProjectConfig) {
1531
- merged.embeddingModel = normalizedProjectConfig.embeddingModel;
1532
- } else if (globalConfig && globalConfig.embeddingModel) {
1533
- merged.embeddingModel = globalConfig.embeddingModel;
1534
- }
1535
- if (projectConfig && "reranker" in normalizedProjectConfig) {
1536
- merged.reranker = normalizedProjectConfig.reranker;
1537
- } else if (globalConfig && globalConfig.reranker) {
1538
- merged.reranker = globalConfig.reranker;
1539
- }
1540
- if (projectConfig && "include" in normalizedProjectConfig) {
1541
- merged.include = normalizedProjectConfig.include;
1542
- } else if (globalConfig && globalConfig.include) {
1543
- merged.include = globalConfig.include;
1544
- }
1545
- if (projectConfig && "exclude" in normalizedProjectConfig) {
1546
- merged.exclude = normalizedProjectConfig.exclude;
1547
- } else if (globalConfig && globalConfig.exclude) {
1548
- merged.exclude = globalConfig.exclude;
1549
- }
1550
- if (projectConfig && "indexing" in normalizedProjectConfig) {
1551
- merged.indexing = normalizedProjectConfig.indexing;
1552
- } else if (globalConfig && globalConfig.indexing) {
1553
- merged.indexing = globalConfig.indexing;
1554
- }
1555
- if (projectConfig && "search" in normalizedProjectConfig) {
1556
- merged.search = normalizedProjectConfig.search;
1557
- } else if (globalConfig && globalConfig.search) {
1558
- merged.search = globalConfig.search;
1559
- }
1560
- if (projectConfig && "debug" in normalizedProjectConfig) {
1561
- merged.debug = normalizedProjectConfig.debug;
1562
- } else if (globalConfig && globalConfig.debug) {
1563
- merged.debug = globalConfig.debug;
1564
- }
1565
- if (projectConfig && "scope" in normalizedProjectConfig) {
1566
- merged.scope = normalizedProjectConfig.scope;
1567
- } else if (globalConfig && "scope" in globalConfig) {
1568
- merged.scope = globalConfig.scope;
1569
- }
1570
- if (projectConfig) {
1571
- for (const key of Object.keys(projectConfig)) {
1572
- if (key === "embeddingProvider" || key === "customProvider" || key === "embeddingModel" || key === "reranker" || key === "include" || key === "exclude" || key === "indexing" || key === "search" || key === "debug" || key === "scope" || key === "knowledgeBases" || key === "additionalInclude") {
1573
- continue;
1574
- }
1575
- merged[key] = normalizedProjectConfig[key];
1576
- }
1577
- }
1578
- const globalKbs = globalConfig && Array.isArray(globalConfig.knowledgeBases) ? globalConfig.knowledgeBases : [];
1579
- const projectKbs = projectConfig ? Array.isArray(normalizedProjectConfig.knowledgeBases) ? normalizedProjectConfig.knowledgeBases : [] : [];
1580
- const allKbs = [...globalKbs, ...projectKbs];
1581
- const uniqueKbs = [...new Set(allKbs.map((p) => String(p).trim()))];
1582
- merged.knowledgeBases = uniqueKbs;
1583
- const globalAdditional = globalConfig && Array.isArray(globalConfig.additionalInclude) ? globalConfig.additionalInclude : [];
1584
- const projectAdditional = projectConfig && Array.isArray(projectConfig.additionalInclude) ? projectConfig.additionalInclude : [];
1585
- const allAdditional = [...globalAdditional, ...projectAdditional];
1586
- const uniqueAdditional = [...new Set(allAdditional.map((p) => String(p).trim()))];
1587
- merged.additionalInclude = uniqueAdditional;
1588
- return merged;
1589
- }
1590
-
1591
- // src/indexer/index.ts
1592
- import { existsSync as existsSync6, readFileSync as readFileSync6, writeFileSync as writeFileSync3, renameSync, unlinkSync, mkdirSync as mkdirSync3, promises as fsPromises2 } from "fs";
1593
- import * as path8 from "path";
1594
- import { performance as performance2 } from "perf_hooks";
1595
-
1596
- // node_modules/eventemitter3/index.mjs
1597
- var import_index = __toESM(require_eventemitter3(), 1);
1598
-
1599
- // node_modules/p-timeout/index.js
1600
- var TimeoutError = class _TimeoutError extends Error {
1601
- name = "TimeoutError";
1602
- constructor(message, options) {
1603
- super(message, options);
1604
- Error.captureStackTrace?.(this, _TimeoutError);
1605
- }
1606
- };
1607
- var getAbortedReason = (signal) => signal.reason ?? new DOMException("This operation was aborted.", "AbortError");
1608
- function pTimeout(promise, options) {
1609
- const {
1610
- milliseconds,
1611
- fallback,
1612
- message,
1613
- customTimers = { setTimeout, clearTimeout },
1614
- signal
1615
- } = options;
1616
- let timer;
1617
- let abortHandler;
1618
- const wrappedPromise = new Promise((resolve8, reject) => {
1619
- if (typeof milliseconds !== "number" || Math.sign(milliseconds) !== 1) {
1620
- throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
1621
- }
1622
- if (signal?.aborted) {
1623
- reject(getAbortedReason(signal));
1624
- return;
1625
- }
1626
- if (signal) {
1627
- abortHandler = () => {
1628
- reject(getAbortedReason(signal));
1629
- };
1630
- signal.addEventListener("abort", abortHandler, { once: true });
1631
- }
1632
- promise.then(resolve8, reject);
1633
- if (milliseconds === Number.POSITIVE_INFINITY) {
1634
- return;
1635
- }
1636
- const timeoutError = new TimeoutError();
1637
- timer = customTimers.setTimeout.call(void 0, () => {
1638
- if (fallback) {
1639
- try {
1640
- resolve8(fallback());
1641
- } catch (error) {
1642
- reject(error);
1643
- }
1644
- return;
1645
- }
1646
- if (typeof promise.cancel === "function") {
1647
- promise.cancel();
1648
- }
1649
- if (message === false) {
1650
- resolve8();
1651
- } else if (message instanceof Error) {
1652
- reject(message);
1653
- } else {
1654
- timeoutError.message = message ?? `Promise timed out after ${milliseconds} milliseconds`;
1655
- reject(timeoutError);
1656
- }
1657
- }, milliseconds);
1658
- });
1659
- const cancelablePromise = wrappedPromise.finally(() => {
1660
- cancelablePromise.clear();
1661
- if (abortHandler && signal) {
1662
- signal.removeEventListener("abort", abortHandler);
1663
- }
1664
- });
1665
- cancelablePromise.clear = () => {
1666
- customTimers.clearTimeout.call(void 0, timer);
1667
- timer = void 0;
1668
- };
1669
- return cancelablePromise;
1670
- }
1671
-
1672
- // node_modules/p-queue/dist/lower-bound.js
1673
- function lowerBound(array, value, comparator) {
1674
- let first = 0;
1675
- let count = array.length;
1676
- while (count > 0) {
1677
- const step = Math.trunc(count / 2);
1678
- let it = first + step;
1679
- if (comparator(array[it], value) <= 0) {
1680
- first = ++it;
1681
- count -= step + 1;
1682
- } else {
1683
- count = step;
1684
- }
1685
- }
1686
- return first;
1365
+ return first;
1687
1366
  }
1688
1367
 
1689
1368
  // node_modules/p-queue/dist/priority-queue.js
@@ -2049,7 +1728,7 @@ var PQueue = class extends import_index.default {
2049
1728
  // Assign unique ID if not provided
2050
1729
  id: options.id ?? (this.#idAssigner++).toString()
2051
1730
  };
2052
- return new Promise((resolve8, reject) => {
1731
+ return new Promise((resolve10, reject) => {
2053
1732
  const taskSymbol = /* @__PURE__ */ Symbol(`task-${options.id}`);
2054
1733
  let cleanupQueueAbortHandler = () => void 0;
2055
1734
  const run = async () => {
@@ -2089,7 +1768,7 @@ var PQueue = class extends import_index.default {
2089
1768
  })]);
2090
1769
  }
2091
1770
  const result = await operation;
2092
- resolve8(result);
1771
+ resolve10(result);
2093
1772
  this.emit("completed", result);
2094
1773
  } catch (error) {
2095
1774
  reject(error);
@@ -2277,13 +1956,13 @@ var PQueue = class extends import_index.default {
2277
1956
  });
2278
1957
  }
2279
1958
  async #onEvent(event, filter) {
2280
- return new Promise((resolve8) => {
1959
+ return new Promise((resolve10) => {
2281
1960
  const listener = () => {
2282
1961
  if (filter && !filter()) {
2283
1962
  return;
2284
1963
  }
2285
1964
  this.off(event, listener);
2286
- resolve8();
1965
+ resolve10();
2287
1966
  };
2288
1967
  this.on(event, listener);
2289
1968
  });
@@ -2569,7 +2248,7 @@ async function onAttemptFailure({ error, attemptNumber, retriesConsumed, startTi
2569
2248
  const finalDelay = Math.min(delayTime, remainingTime);
2570
2249
  options.signal?.throwIfAborted();
2571
2250
  if (finalDelay > 0) {
2572
- await new Promise((resolve8, reject) => {
2251
+ await new Promise((resolve10, reject) => {
2573
2252
  const onAbort = () => {
2574
2253
  clearTimeout(timeoutToken);
2575
2254
  options.signal?.removeEventListener("abort", onAbort);
@@ -2577,7 +2256,7 @@ async function onAttemptFailure({ error, attemptNumber, retriesConsumed, startTi
2577
2256
  };
2578
2257
  const timeoutToken = setTimeout(() => {
2579
2258
  options.signal?.removeEventListener("abort", onAbort);
2580
- resolve8();
2259
+ resolve10();
2581
2260
  }, finalDelay);
2582
2261
  if (options.unref) {
2583
2262
  timeoutToken.unref?.();
@@ -2638,17 +2317,17 @@ async function pRetry(input, options = {}) {
2638
2317
  }
2639
2318
 
2640
2319
  // src/embeddings/detector.ts
2641
- import { existsSync as existsSync4, readFileSync as readFileSync4 } from "fs";
2642
- import * as path5 from "path";
2643
- import * as os3 from "os";
2320
+ import { existsSync, readFileSync as readFileSync2 } from "fs";
2321
+ import * as path2 from "path";
2322
+ import * as os from "os";
2644
2323
  function getOpenCodeAuthPath() {
2645
- return path5.join(os3.homedir(), ".local", "share", "opencode", "auth.json");
2324
+ return path2.join(os.homedir(), ".local", "share", "opencode", "auth.json");
2646
2325
  }
2647
2326
  function loadOpenCodeAuth() {
2648
2327
  const authPath = getOpenCodeAuthPath();
2649
2328
  try {
2650
- if (existsSync4(authPath)) {
2651
- return JSON.parse(readFileSync4(authPath, "utf-8"));
2329
+ if (existsSync(authPath)) {
2330
+ return JSON.parse(readFileSync2(authPath, "utf-8"));
2652
2331
  }
2653
2332
  } catch {
2654
2333
  }
@@ -2715,7 +2394,8 @@ function getGitHubCopilotCredentials() {
2715
2394
  if (!copilotAuth || copilotAuth.type !== "oauth") {
2716
2395
  return null;
2717
2396
  }
2718
- const baseUrl = copilotAuth.enterpriseUrl ? `https://copilot-api.${copilotAuth.enterpriseUrl.replace(/^https?:\/\//, "").replace(/\/$/, "")}` : "https://models.github.ai";
2397
+ const auth = copilotAuth;
2398
+ const baseUrl = auth.enterpriseUrl ? `https://copilot-api.${auth.enterpriseUrl.replace(/^https?:\/\//, "").replace(/\/$/, "")}` : "https://models.github.ai";
2719
2399
  return {
2720
2400
  provider: "github-copilot",
2721
2401
  baseUrl,
@@ -2811,42 +2491,12 @@ function createCustomProviderInfo(config) {
2811
2491
  };
2812
2492
  }
2813
2493
 
2814
- // src/embeddings/provider.ts
2815
- var CustomProviderNonRetryableError = class extends Error {
2816
- constructor(message) {
2817
- super(message);
2818
- this.name = "CustomProviderNonRetryableError";
2819
- }
2820
- };
2821
- function createEmbeddingProvider(configuredProviderInfo) {
2822
- switch (configuredProviderInfo.provider) {
2823
- case "github-copilot":
2824
- return new GitHubCopilotEmbeddingProvider(configuredProviderInfo.credentials, configuredProviderInfo.modelInfo);
2825
- case "openai":
2826
- return new OpenAIEmbeddingProvider(configuredProviderInfo.credentials, configuredProviderInfo.modelInfo);
2827
- case "google":
2828
- return new GoogleEmbeddingProvider(configuredProviderInfo.credentials, configuredProviderInfo.modelInfo);
2829
- case "ollama":
2830
- return new OllamaEmbeddingProvider(configuredProviderInfo.credentials, configuredProviderInfo.modelInfo);
2831
- case "custom":
2832
- return new CustomEmbeddingProvider(configuredProviderInfo.credentials, configuredProviderInfo.modelInfo);
2833
- default: {
2834
- const _exhaustive = configuredProviderInfo;
2835
- throw new Error(`Unsupported embedding provider: ${_exhaustive.provider}`);
2836
- }
2837
- }
2838
- }
2839
- var GitHubCopilotEmbeddingProvider = class {
2494
+ // src/embeddings/provider-types.ts
2495
+ var BaseEmbeddingProvider = class {
2840
2496
  constructor(credentials, modelInfo) {
2841
2497
  this.credentials = credentials;
2842
2498
  this.modelInfo = modelInfo;
2843
2499
  }
2844
- getToken() {
2845
- if (!this.credentials.refreshToken) {
2846
- throw new Error("No OAuth token available for GitHub");
2847
- }
2848
- return this.credentials.refreshToken;
2849
- }
2850
2500
  async embedQuery(query) {
2851
2501
  const result = await this.embedBatch([query]);
2852
2502
  return {
@@ -2861,69 +2511,204 @@ var GitHubCopilotEmbeddingProvider = class {
2861
2511
  tokensUsed: result.totalTokensUsed
2862
2512
  };
2863
2513
  }
2864
- async embedBatch(texts) {
2865
- const token = this.getToken();
2866
- const response = await fetch(`${this.credentials.baseUrl}/inference/embeddings`, {
2867
- method: "POST",
2868
- headers: {
2869
- Authorization: `Bearer ${token}`,
2870
- "Content-Type": "application/json",
2871
- Accept: "application/vnd.github+json",
2872
- "X-GitHub-Api-Version": "2022-11-28"
2873
- },
2874
- body: JSON.stringify({
2875
- model: `openai/${this.modelInfo.model}`,
2876
- input: texts
2877
- })
2878
- });
2879
- if (!response.ok) {
2880
- const error = await response.text();
2881
- throw new Error(`GitHub Copilot embedding API error: ${response.status} - ${error}`);
2882
- }
2883
- const data = await response.json();
2884
- return {
2885
- embeddings: data.data.map((d) => d.embedding),
2886
- totalTokensUsed: data.usage.total_tokens
2887
- };
2888
- }
2889
2514
  getModelInfo() {
2890
2515
  return this.modelInfo;
2891
2516
  }
2892
2517
  };
2893
- var OpenAIEmbeddingProvider = class {
2894
- constructor(credentials, modelInfo) {
2895
- this.credentials = credentials;
2896
- this.modelInfo = modelInfo;
2518
+ var CustomProviderNonRetryableError = class extends Error {
2519
+ constructor(message) {
2520
+ super(message);
2521
+ this.name = "CustomProviderNonRetryableError";
2897
2522
  }
2898
- async embedQuery(query) {
2899
- const result = await this.embedBatch([query]);
2900
- return {
2901
- embedding: result.embeddings[0],
2902
- tokensUsed: result.totalTokensUsed
2523
+ };
2524
+
2525
+ // src/utils/url-validation.ts
2526
+ var BLOCKED_METADATA_IPS = [
2527
+ /^169\.254\.169\.254$/,
2528
+ // AWS/Azure/GCP metadata
2529
+ /^169\.254\.170\.2$/,
2530
+ // AWS ECS task metadata
2531
+ /^fd00:ec2::254$/
2532
+ // AWS IMDSv2 IPv6
2533
+ ];
2534
+ var BLOCKED_HOSTNAMES = /* @__PURE__ */ new Set([
2535
+ "metadata.google.internal",
2536
+ "metadata.google",
2537
+ "metadata.goog",
2538
+ "kubernetes.default.svc"
2539
+ ]);
2540
+ function validateExternalUrl(urlString) {
2541
+ let parsed;
2542
+ try {
2543
+ parsed = new URL(urlString);
2544
+ } catch {
2545
+ return { valid: false, reason: `Invalid URL: ${sanitizeUrlForError(urlString)}` };
2546
+ }
2547
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
2548
+ return { valid: false, reason: `Blocked protocol: ${parsed.protocol}` };
2549
+ }
2550
+ const hostname = parsed.hostname.toLowerCase();
2551
+ if (BLOCKED_HOSTNAMES.has(hostname)) {
2552
+ return { valid: false, reason: `Blocked: cloud metadata service (${hostname})` };
2553
+ }
2554
+ for (const pattern of BLOCKED_METADATA_IPS) {
2555
+ if (pattern.test(hostname)) {
2556
+ return { valid: false, reason: `Blocked: cloud metadata IP (${hostname})` };
2557
+ }
2558
+ }
2559
+ if (/^169\.254\./.test(hostname)) {
2560
+ return { valid: false, reason: `Blocked: link-local address (${hostname})` };
2561
+ }
2562
+ return { valid: true };
2563
+ }
2564
+ function sanitizeUrlForError(url) {
2565
+ try {
2566
+ const parsed = new URL(url);
2567
+ parsed.username = "";
2568
+ parsed.password = "";
2569
+ return parsed.toString();
2570
+ } catch {
2571
+ const maxLen = 80;
2572
+ if (url.length > maxLen) {
2573
+ return url.slice(0, maxLen) + "...";
2574
+ }
2575
+ return url;
2576
+ }
2577
+ }
2578
+
2579
+ // src/embeddings/providers/custom.ts
2580
+ var CustomEmbeddingProvider = class extends BaseEmbeddingProvider {
2581
+ constructor(credentials, modelInfo) {
2582
+ super(credentials, modelInfo);
2583
+ }
2584
+ splitIntoRequestBatches(texts) {
2585
+ const maxBatchSize = this.modelInfo.maxBatchSize;
2586
+ if (!maxBatchSize || texts.length <= maxBatchSize) {
2587
+ return [texts];
2588
+ }
2589
+ const batches = [];
2590
+ for (let i = 0; i < texts.length; i += maxBatchSize) {
2591
+ batches.push(texts.slice(i, i + maxBatchSize));
2592
+ }
2593
+ return batches;
2594
+ }
2595
+ async embedRequest(texts) {
2596
+ if (texts.length === 0) {
2597
+ return {
2598
+ embeddings: [],
2599
+ totalTokensUsed: 0
2600
+ };
2601
+ }
2602
+ const headers = {
2603
+ "Content-Type": "application/json"
2903
2604
  };
2605
+ if (this.credentials.apiKey) {
2606
+ headers.Authorization = `Bearer ${this.credentials.apiKey}`;
2607
+ }
2608
+ const baseUrl = this.credentials.baseUrl ?? "";
2609
+ const fullUrl = `${baseUrl}/embeddings`;
2610
+ const urlCheck = validateExternalUrl(fullUrl);
2611
+ if (!urlCheck.valid) {
2612
+ throw new CustomProviderNonRetryableError(
2613
+ `Custom embedding provider URL blocked (SSRF protection): ${urlCheck.reason}`
2614
+ );
2615
+ }
2616
+ const timeoutMs = this.modelInfo.timeoutMs;
2617
+ const controller = new AbortController();
2618
+ const timeout = setTimeout(() => controller.abort(), timeoutMs);
2619
+ let response;
2620
+ try {
2621
+ response = await fetch(fullUrl, {
2622
+ method: "POST",
2623
+ headers,
2624
+ body: JSON.stringify({
2625
+ model: this.modelInfo.model,
2626
+ input: texts
2627
+ }),
2628
+ signal: controller.signal
2629
+ });
2630
+ } catch (error) {
2631
+ if (error instanceof Error && error.name === "AbortError") {
2632
+ throw new Error(`Custom embedding API request timed out after ${timeoutMs}ms for ${sanitizeUrlForError(fullUrl)}`);
2633
+ }
2634
+ throw error;
2635
+ } finally {
2636
+ clearTimeout(timeout);
2637
+ }
2638
+ if (!response.ok) {
2639
+ const errorText = (await response.text()).slice(0, 500);
2640
+ if (response.status >= 400 && response.status < 500 && response.status !== 429) {
2641
+ throw new CustomProviderNonRetryableError(`Custom embedding API error (non-retryable): ${response.status} - ${errorText}`);
2642
+ }
2643
+ throw new Error(`Custom embedding API error: ${response.status} - ${errorText}`);
2644
+ }
2645
+ const data = await response.json();
2646
+ if (data.data && Array.isArray(data.data)) {
2647
+ if (data.data.length > 0) {
2648
+ const actualDims = data.data[0].embedding.length;
2649
+ if (actualDims !== this.modelInfo.dimensions) {
2650
+ throw new Error(
2651
+ `Dimension mismatch: customProvider.dimensions is ${this.modelInfo.dimensions}, but the API returned vectors with ${actualDims} dimensions. Update your config to match the model's actual output dimensions.`
2652
+ );
2653
+ }
2654
+ }
2655
+ if (data.data.length !== texts.length) {
2656
+ throw new Error(
2657
+ `Embedding count mismatch: sent ${texts.length} texts but received ${data.data.length} embeddings. The custom embedding server may not support batch input.`
2658
+ );
2659
+ }
2660
+ return {
2661
+ embeddings: data.data.map((d) => d.embedding),
2662
+ totalTokensUsed: data.usage?.total_tokens ?? texts.reduce((sum, t) => sum + Math.ceil(t.length / 4), 0)
2663
+ };
2664
+ }
2665
+ throw new Error("Custom embedding API returned unexpected response format. Expected OpenAI-compatible format with data[].embedding.");
2904
2666
  }
2905
- async embedDocument(document) {
2906
- const result = await this.embedBatch([document]);
2667
+ async embedBatch(texts) {
2668
+ const requestBatches = this.splitIntoRequestBatches(texts);
2669
+ const embeddings = [];
2670
+ let totalTokensUsed = 0;
2671
+ for (const batch of requestBatches) {
2672
+ const result = await this.embedRequest(batch);
2673
+ embeddings.push(...result.embeddings);
2674
+ totalTokensUsed += result.totalTokensUsed;
2675
+ }
2907
2676
  return {
2908
- embedding: result.embeddings[0],
2909
- tokensUsed: result.totalTokensUsed
2677
+ embeddings,
2678
+ totalTokensUsed
2910
2679
  };
2911
2680
  }
2681
+ };
2682
+
2683
+ // src/embeddings/providers/github-copilot.ts
2684
+ var GitHubCopilotEmbeddingProvider = class extends BaseEmbeddingProvider {
2685
+ constructor(credentials, modelInfo) {
2686
+ super(credentials, modelInfo);
2687
+ }
2688
+ getToken() {
2689
+ if (!this.credentials.refreshToken) {
2690
+ throw new Error("No OAuth token available for GitHub");
2691
+ }
2692
+ return this.credentials.refreshToken;
2693
+ }
2912
2694
  async embedBatch(texts) {
2913
- const response = await fetch(`${this.credentials.baseUrl}/embeddings`, {
2695
+ const token = this.getToken();
2696
+ const response = await fetch(`${this.credentials.baseUrl}/inference/embeddings`, {
2914
2697
  method: "POST",
2915
2698
  headers: {
2916
- Authorization: `Bearer ${this.credentials.apiKey}`,
2917
- "Content-Type": "application/json"
2699
+ Authorization: `Bearer ${token}`,
2700
+ "Content-Type": "application/json",
2701
+ Accept: "application/vnd.github+json",
2702
+ "X-GitHub-Api-Version": "2022-11-28"
2918
2703
  },
2919
2704
  body: JSON.stringify({
2920
- model: this.modelInfo.model,
2705
+ model: `openai/${this.modelInfo.model}`,
2921
2706
  input: texts
2922
2707
  })
2923
2708
  });
2924
2709
  if (!response.ok) {
2925
- const error = await response.text();
2926
- throw new Error(`OpenAI embedding API error: ${response.status} - ${error}`);
2710
+ const error = (await response.text()).slice(0, 500);
2711
+ throw new Error(`GitHub Copilot embedding API error: ${response.status} - ${error}`);
2927
2712
  }
2928
2713
  const data = await response.json();
2929
2714
  return {
@@ -2931,16 +2716,14 @@ var OpenAIEmbeddingProvider = class {
2931
2716
  totalTokensUsed: data.usage.total_tokens
2932
2717
  };
2933
2718
  }
2934
- getModelInfo() {
2935
- return this.modelInfo;
2936
- }
2937
2719
  };
2938
- var GoogleEmbeddingProvider = class _GoogleEmbeddingProvider {
2720
+
2721
+ // src/embeddings/providers/google.ts
2722
+ var GoogleEmbeddingProvider = class _GoogleEmbeddingProvider extends BaseEmbeddingProvider {
2723
+ static BATCH_SIZE = 20;
2939
2724
  constructor(credentials, modelInfo) {
2940
- this.credentials = credentials;
2941
- this.modelInfo = modelInfo;
2725
+ super(credentials, modelInfo);
2942
2726
  }
2943
- static BATCH_SIZE = 20;
2944
2727
  async embedQuery(query) {
2945
2728
  const taskType = this.modelInfo.taskAble ? "CODE_RETRIEVAL_QUERY" : void 0;
2946
2729
  const result = await this.embedWithTaskType([query], taskType);
@@ -2961,12 +2744,6 @@ var GoogleEmbeddingProvider = class _GoogleEmbeddingProvider {
2961
2744
  const taskType = this.modelInfo.taskAble ? "RETRIEVAL_DOCUMENT" : void 0;
2962
2745
  return this.embedWithTaskType(texts, taskType);
2963
2746
  }
2964
- /**
2965
- * Embeds texts using the Google embedContent API.
2966
- * Sends multiple texts as parts in batched requests (up to BATCH_SIZE per call).
2967
- * When taskType is provided (gemini-embedding-001), includes it in the request
2968
- * for task-specific embedding optimization.
2969
- */
2970
2747
  async embedWithTaskType(texts, taskType) {
2971
2748
  const batches = [];
2972
2749
  for (let i = 0; i < texts.length; i += _GoogleEmbeddingProvider.BATCH_SIZE) {
@@ -2983,17 +2760,18 @@ var GoogleEmbeddingProvider = class _GoogleEmbeddingProvider {
2983
2760
  outputDimensionality: this.modelInfo.dimensions
2984
2761
  }));
2985
2762
  const response = await fetch(
2986
- `${this.credentials.baseUrl}/models/${this.modelInfo.model}:batchEmbedContents?key=${this.credentials.apiKey}`,
2763
+ `${this.credentials.baseUrl}/models/${this.modelInfo.model}:batchEmbedContents`,
2987
2764
  {
2988
2765
  method: "POST",
2989
2766
  headers: {
2990
- "Content-Type": "application/json"
2767
+ "Content-Type": "application/json",
2768
+ ...this.credentials.apiKey && { "x-goog-api-key": this.credentials.apiKey }
2991
2769
  },
2992
2770
  body: JSON.stringify({ requests })
2993
2771
  }
2994
2772
  );
2995
2773
  if (!response.ok) {
2996
- const error = await response.text();
2774
+ const error = (await response.text()).slice(0, 500);
2997
2775
  throw new Error(`Google embedding API error: ${response.status} - ${error}`);
2998
2776
  }
2999
2777
  const data = await response.json();
@@ -3008,29 +2786,13 @@ var GoogleEmbeddingProvider = class _GoogleEmbeddingProvider {
3008
2786
  totalTokensUsed: batchResults.reduce((sum, r) => sum + r.tokensUsed, 0)
3009
2787
  };
3010
2788
  }
3011
- getModelInfo() {
3012
- return this.modelInfo;
3013
- }
3014
2789
  };
3015
- var OllamaEmbeddingProvider = class _OllamaEmbeddingProvider {
3016
- constructor(credentials, modelInfo) {
3017
- this.credentials = credentials;
3018
- this.modelInfo = modelInfo;
3019
- }
2790
+
2791
+ // src/embeddings/providers/ollama.ts
2792
+ var OllamaEmbeddingProvider = class _OllamaEmbeddingProvider extends BaseEmbeddingProvider {
3020
2793
  static MIN_TRUNCATION_CHARS = 512;
3021
- async embedQuery(query) {
3022
- const result = await this.embedBatch([query]);
3023
- return {
3024
- embedding: result.embeddings[0],
3025
- tokensUsed: result.totalTokensUsed
3026
- };
3027
- }
3028
- async embedDocument(document) {
3029
- const result = await this.embedBatch([document]);
3030
- return {
3031
- embedding: result.embeddings[0],
3032
- tokensUsed: result.totalTokensUsed
3033
- };
2794
+ constructor(credentials, modelInfo) {
2795
+ super(credentials, modelInfo);
3034
2796
  }
3035
2797
  estimateTokens(text) {
3036
2798
  return Math.ceil(text.length / 4);
@@ -3115,7 +2877,7 @@ var OllamaEmbeddingProvider = class _OllamaEmbeddingProvider {
3115
2877
  })
3116
2878
  });
3117
2879
  if (!response.ok) {
3118
- const error = await response.text();
2880
+ const error = (await response.text()).slice(0, 500);
3119
2881
  throw new Error(`Ollama embedding API error: ${response.status} - ${error}`);
3120
2882
  }
3121
2883
  const data = await response.json();
@@ -3134,125 +2896,56 @@ var OllamaEmbeddingProvider = class _OllamaEmbeddingProvider {
3134
2896
  totalTokensUsed: results.reduce((sum, r) => sum + r.tokensUsed, 0)
3135
2897
  };
3136
2898
  }
3137
- getModelInfo() {
3138
- return this.modelInfo;
3139
- }
3140
2899
  };
3141
- var CustomEmbeddingProvider = class {
2900
+
2901
+ // src/embeddings/providers/openai.ts
2902
+ var OpenAIEmbeddingProvider = class extends BaseEmbeddingProvider {
3142
2903
  constructor(credentials, modelInfo) {
3143
- this.credentials = credentials;
3144
- this.modelInfo = modelInfo;
2904
+ super(credentials, modelInfo);
3145
2905
  }
3146
- splitIntoRequestBatches(texts) {
3147
- const maxBatchSize = this.modelInfo.maxBatchSize;
3148
- if (!maxBatchSize || texts.length <= maxBatchSize) {
3149
- return [texts];
3150
- }
3151
- const batches = [];
3152
- for (let i = 0; i < texts.length; i += maxBatchSize) {
3153
- batches.push(texts.slice(i, i + maxBatchSize));
3154
- }
3155
- return batches;
3156
- }
3157
- async embedRequest(texts) {
3158
- if (texts.length === 0) {
3159
- return {
3160
- embeddings: [],
3161
- totalTokensUsed: 0
3162
- };
3163
- }
3164
- const headers = {
3165
- "Content-Type": "application/json"
3166
- };
3167
- if (this.credentials.apiKey) {
3168
- headers["Authorization"] = `Bearer ${this.credentials.apiKey}`;
3169
- }
3170
- const baseUrl = this.credentials.baseUrl ?? "";
3171
- const timeoutMs = this.modelInfo.timeoutMs;
3172
- const controller = new AbortController();
3173
- const timeout = setTimeout(() => controller.abort(), timeoutMs);
3174
- let response;
3175
- try {
3176
- response = await fetch(`${baseUrl}/embeddings`, {
3177
- method: "POST",
3178
- headers,
3179
- body: JSON.stringify({
3180
- model: this.modelInfo.model,
3181
- input: texts
3182
- }),
3183
- signal: controller.signal
3184
- });
3185
- } catch (error) {
3186
- if (error instanceof Error && error.name === "AbortError") {
3187
- throw new Error(`Custom embedding API request timed out after ${timeoutMs}ms for ${baseUrl}/embeddings`);
3188
- }
3189
- throw error;
3190
- } finally {
3191
- clearTimeout(timeout);
3192
- }
2906
+ async embedBatch(texts) {
2907
+ const response = await fetch(`${this.credentials.baseUrl}/embeddings`, {
2908
+ method: "POST",
2909
+ headers: {
2910
+ Authorization: `Bearer ${this.credentials.apiKey}`,
2911
+ "Content-Type": "application/json"
2912
+ },
2913
+ body: JSON.stringify({
2914
+ model: this.modelInfo.model,
2915
+ input: texts
2916
+ })
2917
+ });
3193
2918
  if (!response.ok) {
3194
- const errorText = await response.text();
3195
- if (response.status >= 400 && response.status < 500 && response.status !== 429) {
3196
- throw new CustomProviderNonRetryableError(`Custom embedding API error (non-retryable): ${response.status} - ${errorText}`);
3197
- }
3198
- throw new Error(`Custom embedding API error: ${response.status} - ${errorText}`);
2919
+ const error = (await response.text()).slice(0, 500);
2920
+ throw new Error(`OpenAI embedding API error: ${response.status} - ${error}`);
3199
2921
  }
3200
2922
  const data = await response.json();
3201
- if (data.data && Array.isArray(data.data)) {
3202
- if (data.data.length > 0) {
3203
- const actualDims = data.data[0].embedding.length;
3204
- if (actualDims !== this.modelInfo.dimensions) {
3205
- throw new Error(
3206
- `Dimension mismatch: customProvider.dimensions is ${this.modelInfo.dimensions}, but the API returned vectors with ${actualDims} dimensions. Update your config to match the model's actual output dimensions.`
3207
- );
3208
- }
3209
- }
3210
- if (data.data.length !== texts.length) {
3211
- throw new Error(
3212
- `Embedding count mismatch: sent ${texts.length} texts but received ${data.data.length} embeddings. The custom embedding server may not support batch input.`
3213
- );
3214
- }
3215
- return {
3216
- embeddings: data.data.map((d) => d.embedding),
3217
- // Rough estimate: ~4 chars per token. Used as fallback when the server
3218
- // doesn't return usage.total_tokens (e.g. llama.cpp, some vLLM configs).
3219
- totalTokensUsed: data.usage?.total_tokens ?? texts.reduce((sum, t) => sum + Math.ceil(t.length / 4), 0)
3220
- };
3221
- }
3222
- throw new Error("Custom embedding API returned unexpected response format. Expected OpenAI-compatible format with data[].embedding.");
3223
- }
3224
- async embedQuery(query) {
3225
- const result = await this.embedBatch([query]);
3226
- return {
3227
- embedding: result.embeddings[0],
3228
- tokensUsed: result.totalTokensUsed
3229
- };
3230
- }
3231
- async embedDocument(document) {
3232
- const result = await this.embedBatch([document]);
3233
2923
  return {
3234
- embedding: result.embeddings[0],
3235
- tokensUsed: result.totalTokensUsed
2924
+ embeddings: data.data.map((d) => d.embedding),
2925
+ totalTokensUsed: data.usage.total_tokens
3236
2926
  };
3237
2927
  }
3238
- async embedBatch(texts) {
3239
- const requestBatches = this.splitIntoRequestBatches(texts);
3240
- const embeddings = [];
3241
- let totalTokensUsed = 0;
3242
- for (const batch of requestBatches) {
3243
- const result = await this.embedRequest(batch);
3244
- embeddings.push(...result.embeddings);
3245
- totalTokensUsed += result.totalTokensUsed;
2928
+ };
2929
+
2930
+ // src/embeddings/provider.ts
2931
+ function createEmbeddingProvider(configuredProviderInfo) {
2932
+ switch (configuredProviderInfo.provider) {
2933
+ case "github-copilot":
2934
+ return new GitHubCopilotEmbeddingProvider(configuredProviderInfo.credentials, configuredProviderInfo.modelInfo);
2935
+ case "openai":
2936
+ return new OpenAIEmbeddingProvider(configuredProviderInfo.credentials, configuredProviderInfo.modelInfo);
2937
+ case "google":
2938
+ return new GoogleEmbeddingProvider(configuredProviderInfo.credentials, configuredProviderInfo.modelInfo);
2939
+ case "ollama":
2940
+ return new OllamaEmbeddingProvider(configuredProviderInfo.credentials, configuredProviderInfo.modelInfo);
2941
+ case "custom":
2942
+ return new CustomEmbeddingProvider(configuredProviderInfo.credentials, configuredProviderInfo.modelInfo);
2943
+ default: {
2944
+ const _exhaustive = configuredProviderInfo;
2945
+ throw new Error(`Unsupported embedding provider: ${_exhaustive.provider}`);
3246
2946
  }
3247
- return {
3248
- embeddings,
3249
- totalTokensUsed
3250
- };
3251
2947
  }
3252
- getModelInfo() {
3253
- return this.modelInfo;
3254
- }
3255
- };
2948
+ }
3256
2949
 
3257
2950
  // src/rerank/index.ts
3258
2951
  function createReranker(config) {
@@ -3289,7 +2982,10 @@ var SiliconFlowReranker = class {
3289
2982
  if (this.config.apiKey) {
3290
2983
  headers["Authorization"] = `Bearer ${this.config.apiKey}`;
3291
2984
  }
3292
- const baseUrl = this.config.baseUrl ?? "https://api.siliconflow.cn/v1";
2985
+ const baseUrl = this.config.baseUrl;
2986
+ if (!baseUrl) {
2987
+ throw new Error("Reranker baseUrl is required. Configure reranker.baseUrl in your codebase-index.json.");
2988
+ }
3293
2989
  const timeoutMs = this.config.timeoutMs ?? 3e4;
3294
2990
  const controller = new AbortController();
3295
2991
  const timeout = setTimeout(() => controller.abort(), timeoutMs);
@@ -3332,8 +3028,22 @@ var SiliconFlowReranker = class {
3332
3028
 
3333
3029
  // src/utils/files.ts
3334
3030
  var import_ignore = __toESM(require_ignore(), 1);
3335
- import { existsSync as existsSync5, readFileSync as readFileSync5, promises as fsPromises } from "fs";
3336
- import * as path6 from "path";
3031
+ import { existsSync as existsSync2, readFileSync as readFileSync3, promises as fsPromises } from "fs";
3032
+ import * as path4 from "path";
3033
+
3034
+ // src/utils/paths.ts
3035
+ import * as path3 from "path";
3036
+ function normalizePathSeparators(value) {
3037
+ return value.replace(/\\/g, "/");
3038
+ }
3039
+ function isHiddenPathSegment(part) {
3040
+ return part.startsWith(".") && part !== "." && part !== "..";
3041
+ }
3042
+ function isBuildPathSegment(part) {
3043
+ return part.toLowerCase().includes("build");
3044
+ }
3045
+
3046
+ // src/utils/files.ts
3337
3047
  function createIgnoreFilter(projectRoot) {
3338
3048
  const ig = (0, import_ignore.default)();
3339
3049
  const defaultIgnores = [
@@ -3354,9 +3064,9 @@ function createIgnoreFilter(projectRoot) {
3354
3064
  "**/*build*/**"
3355
3065
  ];
3356
3066
  ig.add(defaultIgnores);
3357
- const gitignorePath = path6.join(projectRoot, ".gitignore");
3358
- if (existsSync5(gitignorePath)) {
3359
- const gitignoreContent = readFileSync5(gitignorePath, "utf-8");
3067
+ const gitignorePath = path4.join(projectRoot, ".gitignore");
3068
+ if (existsSync2(gitignorePath)) {
3069
+ const gitignoreContent = readFileSync3(gitignorePath, "utf-8");
3360
3070
  ig.add(gitignoreContent);
3361
3071
  }
3362
3072
  return ig;
@@ -3381,15 +3091,15 @@ async function* walkDirectory(dir, projectRoot, includePatterns, excludePatterns
3381
3091
  const filesInDir = [];
3382
3092
  const subdirs = [];
3383
3093
  for (const entry of entries) {
3384
- const fullPath = path6.join(dir, entry.name);
3385
- const relativePath = path6.relative(projectRoot, fullPath);
3386
- if (entry.name.startsWith(".") && entry.name !== "." && entry.name !== "..") {
3094
+ const fullPath = path4.join(dir, entry.name);
3095
+ const relativePath = path4.relative(projectRoot, fullPath);
3096
+ if (isHiddenPathSegment(entry.name)) {
3387
3097
  if (entry.isDirectory()) {
3388
3098
  skipped.push({ path: relativePath, reason: "excluded" });
3389
3099
  }
3390
3100
  continue;
3391
3101
  }
3392
- if (entry.isDirectory() && entry.name.toLowerCase().includes("build")) {
3102
+ if (entry.isDirectory() && isBuildPathSegment(entry.name)) {
3393
3103
  skipped.push({ path: relativePath, reason: "excluded" });
3394
3104
  continue;
3395
3105
  }
@@ -3431,7 +3141,7 @@ async function* walkDirectory(dir, projectRoot, includePatterns, excludePatterns
3431
3141
  yield f;
3432
3142
  }
3433
3143
  for (let i = options.maxFilesPerDirectory; i < filesInDir.length; i++) {
3434
- skipped.push({ path: path6.relative(projectRoot, filesInDir[i].path), reason: "excluded" });
3144
+ skipped.push({ path: path4.relative(projectRoot, filesInDir[i].path), reason: "excluded" });
3435
3145
  }
3436
3146
  const canRecurse = options.maxDepth === -1 || currentDepth < options.maxDepth;
3437
3147
  if (canRecurse) {
@@ -3471,8 +3181,8 @@ async function collectFiles(projectRoot, includePatterns, excludePatterns, maxFi
3471
3181
  if (additionalRoots && additionalRoots.length > 0) {
3472
3182
  const normalizedRoots = /* @__PURE__ */ new Set();
3473
3183
  for (const kbRoot of additionalRoots) {
3474
- const resolved = path6.normalize(
3475
- path6.isAbsolute(kbRoot) ? kbRoot : path6.resolve(projectRoot, kbRoot)
3184
+ const resolved = path4.normalize(
3185
+ path4.isAbsolute(kbRoot) ? kbRoot : path4.resolve(projectRoot, kbRoot)
3476
3186
  );
3477
3187
  normalizedRoots.add(resolved);
3478
3188
  }
@@ -3541,20 +3251,21 @@ function createCostEstimate(files, provider) {
3541
3251
  }
3542
3252
  function formatCostEstimate(estimate) {
3543
3253
  const sizeFormatted = formatBytes(estimate.totalSizeBytes);
3254
+ const filesFormatted = `${estimate.filesCount.toLocaleString()} files`;
3544
3255
  const costFormatted = estimate.isFree ? "Free" : `~$${estimate.estimatedCost.toFixed(4)}`;
3545
3256
  return `
3546
3257
  \u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510
3547
3258
  \u2502 \u{1F4CA} Indexing Estimate \u2502
3548
3259
  \u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524
3549
3260
  \u2502 \u2502
3550
- \u2502 Files to index: ${padRight(estimate.filesCount.toLocaleString() + " files", 40)}\u2502
3551
- \u2502 Total size: ${padRight(sizeFormatted, 40)}\u2502
3552
- \u2502 Estimated chunks: ${padRight("~" + estimate.estimatedChunks.toLocaleString() + " chunks", 40)}\u2502
3553
- \u2502 Estimated tokens: ${padRight("~" + estimate.estimatedTokens.toLocaleString() + " tokens", 40)}\u2502
3261
+ \u2502 Files to index: ${filesFormatted.padEnd(40)}\u2502
3262
+ \u2502 Total size: ${sizeFormatted.padEnd(40)}\u2502
3263
+ \u2502 Estimated chunks: ${("~" + estimate.estimatedChunks.toLocaleString() + " chunks").padEnd(40)}\u2502
3264
+ \u2502 Estimated tokens: ${("~" + estimate.estimatedTokens.toLocaleString() + " tokens").padEnd(40)}\u2502
3554
3265
  \u2502 \u2502
3555
- \u2502 Provider: ${padRight(estimate.provider, 52)}\u2502
3556
- \u2502 Model: ${padRight(estimate.model, 52)}\u2502
3557
- \u2502 Cost: ${padRight(costFormatted, 52)}\u2502
3266
+ \u2502 Provider: ${estimate.provider.padEnd(52)}\u2502
3267
+ \u2502 Model: ${estimate.model.padEnd(52)}\u2502
3268
+ \u2502 Cost: ${costFormatted.padEnd(52)}\u2502
3558
3269
  \u2502 \u2502
3559
3270
  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518
3560
3271
  `;
@@ -3566,9 +3277,6 @@ function formatBytes(bytes) {
3566
3277
  const i = Math.floor(Math.log(bytes) / Math.log(k));
3567
3278
  return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + " " + sizes[i];
3568
3279
  }
3569
- function padRight(str, length) {
3570
- return str.padEnd(length);
3571
- }
3572
3280
 
3573
3281
  // src/utils/logger.ts
3574
3282
  var LOG_LEVEL_PRIORITY = {
@@ -3635,6 +3343,10 @@ var Logger = class {
3635
3343
  this.logs.shift();
3636
3344
  }
3637
3345
  }
3346
+ withMetrics(fn) {
3347
+ if (!this.config.metrics) return;
3348
+ fn();
3349
+ }
3638
3350
  search(level, message, data) {
3639
3351
  if (this.config.logSearch) {
3640
3352
  this.log(level, "search", message, data);
@@ -3673,89 +3385,107 @@ var Logger = class {
3673
3385
  this.log("debug", "general", message, data);
3674
3386
  }
3675
3387
  recordIndexingStart() {
3676
- if (!this.config.metrics) return;
3677
- this.metrics.indexingStartTime = Date.now();
3388
+ this.withMetrics(() => {
3389
+ this.metrics.indexingStartTime = Date.now();
3390
+ });
3678
3391
  }
3679
3392
  recordIndexingEnd() {
3680
- if (!this.config.metrics) return;
3681
- this.metrics.indexingEndTime = Date.now();
3393
+ this.withMetrics(() => {
3394
+ this.metrics.indexingEndTime = Date.now();
3395
+ });
3682
3396
  }
3683
3397
  recordFilesScanned(count) {
3684
- if (!this.config.metrics) return;
3685
- this.metrics.filesScanned = count;
3398
+ this.withMetrics(() => {
3399
+ this.metrics.filesScanned = count;
3400
+ });
3686
3401
  }
3687
3402
  recordFilesParsed(count) {
3688
- if (!this.config.metrics) return;
3689
- this.metrics.filesParsed = count;
3403
+ this.withMetrics(() => {
3404
+ this.metrics.filesParsed = count;
3405
+ });
3690
3406
  }
3691
3407
  recordParseDuration(durationMs) {
3692
- if (!this.config.metrics) return;
3693
- this.metrics.parseMs = durationMs;
3408
+ this.withMetrics(() => {
3409
+ this.metrics.parseMs = durationMs;
3410
+ });
3694
3411
  }
3695
3412
  recordChunksProcessed(count) {
3696
- if (!this.config.metrics) return;
3697
- this.metrics.chunksProcessed += count;
3413
+ this.withMetrics(() => {
3414
+ this.metrics.chunksProcessed += count;
3415
+ });
3698
3416
  }
3699
3417
  recordChunksEmbedded(count) {
3700
- if (!this.config.metrics) return;
3701
- this.metrics.chunksEmbedded += count;
3418
+ this.withMetrics(() => {
3419
+ this.metrics.chunksEmbedded += count;
3420
+ });
3702
3421
  }
3703
3422
  recordChunksFromCache(count) {
3704
- if (!this.config.metrics) return;
3705
- this.metrics.chunksFromCache += count;
3423
+ this.withMetrics(() => {
3424
+ this.metrics.chunksFromCache += count;
3425
+ });
3706
3426
  }
3707
3427
  recordChunksRemoved(count) {
3708
- if (!this.config.metrics) return;
3709
- this.metrics.chunksRemoved += count;
3428
+ this.withMetrics(() => {
3429
+ this.metrics.chunksRemoved += count;
3430
+ });
3710
3431
  }
3711
3432
  recordEmbeddingApiCall(tokens) {
3712
- if (!this.config.metrics) return;
3713
- this.metrics.embeddingApiCalls++;
3714
- this.metrics.embeddingTokensUsed += tokens;
3433
+ this.withMetrics(() => {
3434
+ this.metrics.embeddingApiCalls++;
3435
+ this.metrics.embeddingTokensUsed += tokens;
3436
+ });
3715
3437
  }
3716
3438
  recordEmbeddingError() {
3717
- if (!this.config.metrics) return;
3718
- this.metrics.embeddingErrors++;
3439
+ this.withMetrics(() => {
3440
+ this.metrics.embeddingErrors++;
3441
+ });
3719
3442
  }
3720
3443
  recordSearch(durationMs, breakdown) {
3721
- if (!this.config.metrics) return;
3722
- this.metrics.searchCount++;
3723
- this.metrics.searchTotalMs += durationMs;
3724
- this.metrics.searchLastMs = durationMs;
3725
- this.metrics.searchAvgMs = this.metrics.searchTotalMs / this.metrics.searchCount;
3726
- if (breakdown) {
3727
- this.metrics.embeddingCallMs = breakdown.embeddingMs;
3728
- this.metrics.vectorSearchMs = breakdown.vectorMs;
3729
- this.metrics.keywordSearchMs = breakdown.keywordMs;
3730
- this.metrics.fusionMs = breakdown.fusionMs;
3731
- }
3444
+ this.withMetrics(() => {
3445
+ this.metrics.searchCount++;
3446
+ this.metrics.searchTotalMs += durationMs;
3447
+ this.metrics.searchLastMs = durationMs;
3448
+ this.metrics.searchAvgMs = this.metrics.searchTotalMs / this.metrics.searchCount;
3449
+ if (breakdown) {
3450
+ this.metrics.embeddingCallMs = breakdown.embeddingMs;
3451
+ this.metrics.vectorSearchMs = breakdown.vectorMs;
3452
+ this.metrics.keywordSearchMs = breakdown.keywordMs;
3453
+ this.metrics.fusionMs = breakdown.fusionMs;
3454
+ }
3455
+ });
3732
3456
  }
3733
3457
  recordCacheHit() {
3734
- if (!this.config.metrics) return;
3735
- this.metrics.cacheHits++;
3458
+ this.withMetrics(() => {
3459
+ this.metrics.cacheHits++;
3460
+ });
3736
3461
  }
3737
3462
  recordCacheMiss() {
3738
- if (!this.config.metrics) return;
3739
- this.metrics.cacheMisses++;
3463
+ this.withMetrics(() => {
3464
+ this.metrics.cacheMisses++;
3465
+ });
3740
3466
  }
3741
3467
  recordQueryCacheHit() {
3742
- if (!this.config.metrics) return;
3743
- this.metrics.queryCacheHits++;
3468
+ this.withMetrics(() => {
3469
+ this.metrics.queryCacheHits++;
3470
+ });
3744
3471
  }
3745
3472
  recordQueryCacheSimilarHit() {
3746
- if (!this.config.metrics) return;
3747
- this.metrics.queryCacheSimilarHits++;
3473
+ this.withMetrics(() => {
3474
+ this.metrics.queryCacheSimilarHits++;
3475
+ });
3748
3476
  }
3749
3477
  recordQueryCacheMiss() {
3750
- if (!this.config.metrics) return;
3751
- this.metrics.queryCacheMisses++;
3478
+ this.withMetrics(() => {
3479
+ this.metrics.queryCacheMisses++;
3480
+ });
3752
3481
  }
3753
3482
  recordGc(orphans, chunks, embeddings) {
3754
- if (!this.config.metrics) return;
3755
- this.metrics.gcRuns++;
3756
- this.metrics.gcOrphansRemoved += orphans;
3757
- this.metrics.gcChunksRemoved += chunks;
3758
- this.metrics.gcEmbeddingsRemoved += embeddings;
3483
+ this.withMetrics(() => {
3484
+ this.metrics.gcRuns++;
3485
+ this.metrics.gcOrphansRemoved += orphans;
3486
+ this.metrics.gcChunksRemoved += chunks;
3487
+ this.metrics.gcEmbeddingsRemoved += embeddings;
3488
+ });
3759
3489
  }
3760
3490
  getMetrics() {
3761
3491
  return { ...this.metrics };
@@ -3862,13 +3592,13 @@ function initializeLogger(config) {
3862
3592
  }
3863
3593
 
3864
3594
  // src/native/index.ts
3865
- import * as path7 from "path";
3866
- import * as os4 from "os";
3595
+ import * as path5 from "path";
3596
+ import * as os2 from "os";
3867
3597
  import * as module from "module";
3868
3598
  import { fileURLToPath } from "url";
3869
3599
  function getNativeBinding() {
3870
- const platform2 = os4.platform();
3871
- const arch2 = os4.arch();
3600
+ const platform2 = os2.platform();
3601
+ const arch2 = os2.arch();
3872
3602
  let bindingName;
3873
3603
  if (platform2 === "darwin" && arch2 === "arm64") {
3874
3604
  bindingName = "codebase-index-native.darwin-arm64.node";
@@ -3886,19 +3616,19 @@ function getNativeBinding() {
3886
3616
  let currentDir;
3887
3617
  let requireTarget;
3888
3618
  if (typeof import.meta !== "undefined" && import.meta.url) {
3889
- currentDir = path7.dirname(fileURLToPath(import.meta.url));
3619
+ currentDir = path5.dirname(fileURLToPath(import.meta.url));
3890
3620
  requireTarget = import.meta.url;
3891
3621
  } else if (typeof __dirname !== "undefined") {
3892
3622
  currentDir = __dirname;
3893
3623
  requireTarget = __filename;
3894
3624
  } else {
3895
3625
  currentDir = process.cwd();
3896
- requireTarget = path7.join(currentDir, "index.js");
3626
+ requireTarget = path5.join(currentDir, "index.js");
3897
3627
  }
3898
3628
  const normalizedDir = currentDir.replace(/\\/g, "/");
3899
- const isDevMode = normalizedDir.includes("/src/native") || currentDir.includes(path7.join("src", "native"));
3900
- const packageRoot = isDevMode ? path7.resolve(currentDir, "../..") : path7.resolve(currentDir, "..");
3901
- const nativePath = path7.join(packageRoot, "native", bindingName);
3629
+ const isDevMode = normalizedDir.includes("/src/native") || currentDir.includes(path5.join("src", "native"));
3630
+ const packageRoot = isDevMode ? path5.resolve(currentDir, "../..") : path5.resolve(currentDir, "..");
3631
+ const nativePath = path5.join(packageRoot, "native", bindingName);
3902
3632
  const require2 = module.createRequire(requireTarget);
3903
3633
  return require2(nativePath);
3904
3634
  }
@@ -4468,7 +4198,6 @@ var Database = class {
4468
4198
  this.throwIfClosed();
4469
4199
  return this.inner.getStats();
4470
4200
  }
4471
- // ── Symbol methods ──────────────────────────────────────────────
4472
4201
  upsertSymbol(symbol) {
4473
4202
  this.throwIfClosed();
4474
4203
  this.inner.upsertSymbol(symbol);
@@ -4498,7 +4227,6 @@ var Database = class {
4498
4227
  this.throwIfClosed();
4499
4228
  return this.inner.deleteSymbolsByFile(filePath);
4500
4229
  }
4501
- // ── Call Edge methods ────────────────────────────────────────────
4502
4230
  upsertCallEdge(edge) {
4503
4231
  this.throwIfClosed();
4504
4232
  this.inner.upsertCallEdge(edge);
@@ -4524,56 +4252,225 @@ var Database = class {
4524
4252
  this.throwIfClosed();
4525
4253
  return this.inner.deleteCallEdgesByFile(filePath);
4526
4254
  }
4527
- resolveCallEdge(edgeId, toSymbolId) {
4528
- this.throwIfClosed();
4529
- this.inner.resolveCallEdge(edgeId, toSymbolId);
4255
+ resolveCallEdge(edgeId, toSymbolId) {
4256
+ this.throwIfClosed();
4257
+ this.inner.resolveCallEdge(edgeId, toSymbolId);
4258
+ }
4259
+ addSymbolsToBranch(branch, symbolIds) {
4260
+ this.throwIfClosed();
4261
+ this.inner.addSymbolsToBranch(branch, symbolIds);
4262
+ }
4263
+ addSymbolsToBranchBatch(branch, symbolIds) {
4264
+ this.throwIfClosed();
4265
+ if (symbolIds.length === 0) return;
4266
+ this.inner.addSymbolsToBranchBatch(branch, symbolIds);
4267
+ }
4268
+ getBranchSymbolIds(branch) {
4269
+ this.throwIfClosed();
4270
+ return this.inner.getBranchSymbolIds(branch);
4271
+ }
4272
+ clearBranchSymbols(branch) {
4273
+ this.throwIfClosed();
4274
+ return this.inner.clearBranchSymbols(branch);
4275
+ }
4276
+ getReferencedSymbolIds(symbolIds) {
4277
+ this.throwIfClosed();
4278
+ if (symbolIds.length === 0) return [];
4279
+ return this.inner.getReferencedSymbolIds(symbolIds);
4280
+ }
4281
+ deleteBranchSymbolsBySymbolIds(symbolIds) {
4282
+ this.throwIfClosed();
4283
+ if (symbolIds.length === 0) return 0;
4284
+ return this.inner.deleteBranchSymbolsBySymbolIds(symbolIds);
4285
+ }
4286
+ deleteBranchSymbolsForBranch(branch, symbolIds) {
4287
+ this.throwIfClosed();
4288
+ if (symbolIds.length === 0) return 0;
4289
+ return this.inner.deleteBranchSymbolsForBranch(branch, symbolIds);
4290
+ }
4291
+ gcOrphanSymbols() {
4292
+ this.throwIfClosed();
4293
+ return this.inner.gcOrphanSymbols();
4294
+ }
4295
+ gcOrphanCallEdges() {
4296
+ this.throwIfClosed();
4297
+ return this.inner.gcOrphanCallEdges();
4298
+ }
4299
+ };
4300
+
4301
+ // src/git/index.ts
4302
+ import { existsSync as existsSync4, readFileSync as readFileSync5, statSync as statSync2 } from "fs";
4303
+ import * as path7 from "path";
4304
+
4305
+ // src/git/refs.ts
4306
+ import { existsSync as existsSync3, readFileSync as readFileSync4, readdirSync, statSync } from "fs";
4307
+ import * as path6 from "path";
4308
+ function readPackedRefs(gitDir) {
4309
+ const packedRefsPath = path6.join(gitDir, "packed-refs");
4310
+ if (!existsSync3(packedRefsPath)) {
4311
+ return [];
4312
+ }
4313
+ try {
4314
+ return readFileSync4(packedRefsPath, "utf-8").split("\n").map((line) => line.trim()).filter((line) => line.length > 0 && !line.startsWith("#") && !line.startsWith("^"));
4315
+ } catch {
4316
+ return [];
4317
+ }
4318
+ }
4319
+ function resolveCommonGitDir(gitDir) {
4320
+ const commonDirPath = path6.join(gitDir, "commondir");
4321
+ if (!existsSync3(commonDirPath)) {
4322
+ return gitDir;
4323
+ }
4324
+ try {
4325
+ const raw = readFileSync4(commonDirPath, "utf-8").trim();
4326
+ if (!raw) {
4327
+ return gitDir;
4328
+ }
4329
+ const resolved = path6.isAbsolute(raw) ? raw : path6.resolve(gitDir, raw);
4330
+ if (existsSync3(resolved)) {
4331
+ return resolved;
4332
+ }
4333
+ } catch {
4334
+ return gitDir;
4335
+ }
4336
+ return gitDir;
4337
+ }
4338
+
4339
+ // src/git/index.ts
4340
+ function resolveWorktreeMainRepoRoot(repoRoot) {
4341
+ const gitDir = resolveGitDir(repoRoot);
4342
+ if (!gitDir) {
4343
+ return null;
4344
+ }
4345
+ const commonGitDir = resolveCommonGitDir(gitDir);
4346
+ if (commonGitDir === gitDir || path7.basename(commonGitDir) !== ".git") {
4347
+ return null;
4348
+ }
4349
+ const mainRepoRoot = path7.dirname(commonGitDir);
4350
+ if (!existsSync4(mainRepoRoot)) {
4351
+ return null;
4352
+ }
4353
+ return path7.resolve(mainRepoRoot) === path7.resolve(repoRoot) ? null : mainRepoRoot;
4354
+ }
4355
+ function resolveGitDir(repoRoot) {
4356
+ const gitPath = path7.join(repoRoot, ".git");
4357
+ if (!existsSync4(gitPath)) {
4358
+ return null;
4359
+ }
4360
+ try {
4361
+ const stat = statSync2(gitPath);
4362
+ if (stat.isDirectory()) {
4363
+ return gitPath;
4364
+ }
4365
+ if (stat.isFile()) {
4366
+ const content = readFileSync5(gitPath, "utf-8").trim();
4367
+ const match = content.match(/^gitdir:\s*(.+)$/);
4368
+ if (match) {
4369
+ const gitdir = match[1];
4370
+ const resolvedPath = path7.isAbsolute(gitdir) ? gitdir : path7.resolve(repoRoot, gitdir);
4371
+ if (existsSync4(resolvedPath)) {
4372
+ return resolvedPath;
4373
+ }
4374
+ }
4375
+ }
4376
+ } catch {
4377
+ }
4378
+ return null;
4379
+ }
4380
+ function isGitRepo(dir) {
4381
+ return resolveGitDir(dir) !== null;
4382
+ }
4383
+ function getCurrentBranch(repoRoot) {
4384
+ const gitDir = resolveGitDir(repoRoot);
4385
+ if (!gitDir) {
4386
+ return null;
4530
4387
  }
4531
- // ── Branch Symbol methods ────────────────────────────────────────
4532
- addSymbolsToBranch(branch, symbolIds) {
4533
- this.throwIfClosed();
4534
- this.inner.addSymbolsToBranch(branch, symbolIds);
4388
+ const headPath = path7.join(gitDir, "HEAD");
4389
+ if (!existsSync4(headPath)) {
4390
+ return null;
4535
4391
  }
4536
- addSymbolsToBranchBatch(branch, symbolIds) {
4537
- this.throwIfClosed();
4538
- if (symbolIds.length === 0) return;
4539
- this.inner.addSymbolsToBranchBatch(branch, symbolIds);
4392
+ try {
4393
+ const headContent = readFileSync5(headPath, "utf-8").trim();
4394
+ const match = headContent.match(/^ref: refs\/heads\/(.+)$/);
4395
+ if (match) {
4396
+ return match[1];
4397
+ }
4398
+ if (/^[0-9a-f]{40}$/i.test(headContent)) {
4399
+ return headContent.slice(0, 7);
4400
+ }
4401
+ return null;
4402
+ } catch {
4403
+ return null;
4540
4404
  }
4541
- getBranchSymbolIds(branch) {
4542
- this.throwIfClosed();
4543
- return this.inner.getBranchSymbolIds(branch);
4405
+ }
4406
+ function getBaseBranch(repoRoot) {
4407
+ const gitDir = resolveGitDir(repoRoot);
4408
+ const refStoreDir = gitDir ? resolveCommonGitDir(gitDir) : null;
4409
+ const candidates = ["main", "master", "develop", "trunk"];
4410
+ if (refStoreDir) {
4411
+ for (const candidate of candidates) {
4412
+ const refPath = path7.join(refStoreDir, "refs", "heads", candidate);
4413
+ if (existsSync4(refPath)) {
4414
+ return candidate;
4415
+ }
4416
+ const packedRefs = readPackedRefs(refStoreDir);
4417
+ if (packedRefs.some((line) => line.endsWith(` refs/heads/${candidate}`))) {
4418
+ return candidate;
4419
+ }
4420
+ }
4544
4421
  }
4545
- clearBranchSymbols(branch) {
4546
- this.throwIfClosed();
4547
- return this.inner.clearBranchSymbols(branch);
4422
+ return getCurrentBranch(repoRoot) ?? "main";
4423
+ }
4424
+ function getBranchOrDefault(repoRoot) {
4425
+ if (!isGitRepo(repoRoot)) {
4426
+ return "default";
4548
4427
  }
4549
- getReferencedSymbolIds(symbolIds) {
4550
- this.throwIfClosed();
4551
- if (symbolIds.length === 0) return [];
4552
- return this.inner.getReferencedSymbolIds(symbolIds);
4428
+ return getCurrentBranch(repoRoot) ?? "default";
4429
+ }
4430
+
4431
+ // src/config/paths.ts
4432
+ import { existsSync as existsSync5 } from "fs";
4433
+ import * as os3 from "os";
4434
+ import * as path8 from "path";
4435
+ var PROJECT_CONFIG_RELATIVE_PATH = path8.join(".opencode", "codebase-index.json");
4436
+ var PROJECT_INDEX_RELATIVE_PATH = path8.join(".opencode", "index");
4437
+ function resolveWorktreeFallbackPath(projectRoot, relativePath) {
4438
+ const mainRepoRoot = resolveWorktreeMainRepoRoot(projectRoot);
4439
+ if (!mainRepoRoot) {
4440
+ return null;
4553
4441
  }
4554
- deleteBranchSymbolsBySymbolIds(symbolIds) {
4555
- this.throwIfClosed();
4556
- if (symbolIds.length === 0) return 0;
4557
- return this.inner.deleteBranchSymbolsBySymbolIds(symbolIds);
4442
+ const fallbackPath = path8.join(mainRepoRoot, relativePath);
4443
+ return existsSync5(fallbackPath) ? fallbackPath : null;
4444
+ }
4445
+ function hasProjectConfig(projectRoot) {
4446
+ return existsSync5(path8.join(projectRoot, PROJECT_CONFIG_RELATIVE_PATH));
4447
+ }
4448
+ function getGlobalIndexPath() {
4449
+ return path8.join(os3.homedir(), ".opencode", "global-index");
4450
+ }
4451
+ function resolveProjectConfigPath(projectRoot) {
4452
+ const localConfigPath = path8.join(projectRoot, PROJECT_CONFIG_RELATIVE_PATH);
4453
+ if (existsSync5(localConfigPath)) {
4454
+ return localConfigPath;
4558
4455
  }
4559
- deleteBranchSymbolsForBranch(branch, symbolIds) {
4560
- this.throwIfClosed();
4561
- if (symbolIds.length === 0) return 0;
4562
- return this.inner.deleteBranchSymbolsForBranch(branch, symbolIds);
4456
+ return resolveWorktreeFallbackPath(projectRoot, PROJECT_CONFIG_RELATIVE_PATH) ?? localConfigPath;
4457
+ }
4458
+ function resolveProjectIndexPath(projectRoot, scope) {
4459
+ if (scope === "global") {
4460
+ return getGlobalIndexPath();
4563
4461
  }
4564
- // ── GC methods for symbols/edges ─────────────────────────────────
4565
- gcOrphanSymbols() {
4566
- this.throwIfClosed();
4567
- return this.inner.gcOrphanSymbols();
4462
+ const localIndexPath = path8.join(projectRoot, PROJECT_INDEX_RELATIVE_PATH);
4463
+ if (existsSync5(localIndexPath)) {
4464
+ return localIndexPath;
4568
4465
  }
4569
- gcOrphanCallEdges() {
4570
- this.throwIfClosed();
4571
- return this.inner.gcOrphanCallEdges();
4466
+ if (hasProjectConfig(projectRoot)) {
4467
+ return localIndexPath;
4572
4468
  }
4573
- };
4469
+ return resolveWorktreeFallbackPath(projectRoot, PROJECT_INDEX_RELATIVE_PATH) ?? localIndexPath;
4470
+ }
4574
4471
 
4575
4472
  // src/indexer/index.ts
4576
- var CALL_GRAPH_LANGUAGES = /* @__PURE__ */ new Set(["typescript", "tsx", "javascript", "jsx", "python", "go", "rust", "php", "apex", "zig"]);
4473
+ var CALL_GRAPH_LANGUAGES = /* @__PURE__ */ new Set(["typescript", "tsx", "javascript", "jsx", "python", "go", "rust", "php", "apex", "zig", "gdscript", "matlab"]);
4577
4474
  var CASE_INSENSITIVE_LANGUAGES = /* @__PURE__ */ new Set(["apex"]);
4578
4475
  var CALL_GRAPH_SYMBOL_CHUNK_TYPES = /* @__PURE__ */ new Set([
4579
4476
  "function_declaration",
@@ -4600,7 +4497,15 @@ var CALL_GRAPH_SYMBOL_CHUNK_TYPES = /* @__PURE__ */ new Set([
4600
4497
  "trigger_declaration",
4601
4498
  "test_declaration",
4602
4499
  "struct_declaration",
4603
- "union_declaration"
4500
+ "union_declaration",
4501
+ // GDScript declarations whose names participate in the call graph.
4502
+ // `function_definition` and `class_definition` are already in the set
4503
+ // above (shared with Python/C/Bash and Python, respectively).
4504
+ "constructor_definition",
4505
+ "enum_definition",
4506
+ "signal_statement",
4507
+ "const_statement",
4508
+ "class_name_statement"
4604
4509
  ]);
4605
4510
  function float32ArrayToBuffer(arr) {
4606
4511
  const float32 = new Float32Array(arr);
@@ -4802,9 +4707,9 @@ function hasAllEmbeddingParts(parts, expectedPartCount) {
4802
4707
  return true;
4803
4708
  }
4804
4709
  function isPathWithinRoot(filePath, rootPath) {
4805
- const normalizedFilePath = path8.resolve(filePath);
4806
- const normalizedRoot = path8.resolve(rootPath);
4807
- return normalizedFilePath === normalizedRoot || normalizedFilePath.startsWith(`${normalizedRoot}${path8.sep}`);
4710
+ const normalizedFilePath = path9.resolve(filePath);
4711
+ const normalizedRoot = path9.resolve(rootPath);
4712
+ return normalizedFilePath === normalizedRoot || normalizedFilePath.startsWith(`${normalizedRoot}${path9.sep}`);
4808
4713
  }
4809
4714
  var rankingQueryTokenCache = /* @__PURE__ */ new Map();
4810
4715
  var rankingNameTokenCache = /* @__PURE__ */ new Map();
@@ -5857,22 +5762,28 @@ var Indexer = class {
5857
5762
  this.projectRoot = projectRoot;
5858
5763
  this.config = config;
5859
5764
  this.indexPath = this.getIndexPath();
5860
- this.fileHashCachePath = path8.join(this.indexPath, "file-hashes.json");
5861
- this.failedBatchesPath = path8.join(this.indexPath, "failed-batches.json");
5862
- this.indexingLockPath = path8.join(this.indexPath, "indexing.lock");
5765
+ this.fileHashCachePath = path9.join(this.indexPath, "file-hashes.json");
5766
+ this.failedBatchesPath = path9.join(this.indexPath, "failed-batches.json");
5767
+ this.indexingLockPath = path9.join(this.indexPath, "indexing.lock");
5863
5768
  this.logger = initializeLogger(config.debug);
5864
5769
  }
5865
5770
  getIndexPath() {
5866
5771
  return resolveProjectIndexPath(this.projectRoot, this.config.scope);
5867
5772
  }
5868
5773
  loadFileHashCache() {
5774
+ if (!existsSync6(this.fileHashCachePath)) {
5775
+ return;
5776
+ }
5869
5777
  try {
5870
- if (existsSync6(this.fileHashCachePath)) {
5871
- const data = readFileSync6(this.fileHashCachePath, "utf-8");
5872
- const parsed = JSON.parse(data);
5873
- this.fileHashCache = new Map(Object.entries(parsed));
5874
- }
5875
- } catch {
5778
+ const data = readFileSync6(this.fileHashCachePath, "utf-8");
5779
+ const parsed = JSON.parse(data);
5780
+ this.fileHashCache = new Map(Object.entries(parsed));
5781
+ } catch (error) {
5782
+ const message = error instanceof Error ? error.message : String(error);
5783
+ this.logger.warn("Failed to load file hash cache, resetting cache state", {
5784
+ fileHashCachePath: this.fileHashCachePath,
5785
+ error: message
5786
+ });
5876
5787
  this.fileHashCache = /* @__PURE__ */ new Map();
5877
5788
  }
5878
5789
  }
@@ -5885,14 +5796,14 @@ var Indexer = class {
5885
5796
  }
5886
5797
  atomicWriteSync(targetPath, data) {
5887
5798
  const tempPath = `${targetPath}.tmp`;
5888
- mkdirSync3(path8.dirname(targetPath), { recursive: true });
5889
- writeFileSync3(tempPath, data);
5799
+ mkdirSync2(path9.dirname(targetPath), { recursive: true });
5800
+ writeFileSync2(tempPath, data);
5890
5801
  renameSync(tempPath, targetPath);
5891
5802
  }
5892
5803
  getScopedRoots() {
5893
- const roots = /* @__PURE__ */ new Set([path8.resolve(this.projectRoot)]);
5804
+ const roots = /* @__PURE__ */ new Set([path9.resolve(this.projectRoot)]);
5894
5805
  for (const kbRoot of this.config.knowledgeBases) {
5895
- roots.add(path8.resolve(this.projectRoot, kbRoot));
5806
+ roots.add(path9.resolve(this.projectRoot, kbRoot));
5896
5807
  }
5897
5808
  return Array.from(roots);
5898
5809
  }
@@ -5901,22 +5812,22 @@ var Indexer = class {
5901
5812
  if (this.config.scope !== "global") {
5902
5813
  return branchName;
5903
5814
  }
5904
- const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
5815
+ const projectHash = hashContent(path9.resolve(this.projectRoot)).slice(0, 16);
5905
5816
  return `${projectHash}:${branchName}`;
5906
5817
  }
5907
5818
  getLegacyBranchCatalogKey() {
5908
5819
  return this.currentBranch || "default";
5909
5820
  }
5910
5821
  getLegacyMigrationMetadataKey() {
5911
- const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
5822
+ const projectHash = hashContent(path9.resolve(this.projectRoot)).slice(0, 16);
5912
5823
  return `index.globalBranchMigration.${projectHash}`;
5913
5824
  }
5914
5825
  getProjectEmbeddingStrategyMetadataKey() {
5915
- const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
5826
+ const projectHash = hashContent(path9.resolve(this.projectRoot)).slice(0, 16);
5916
5827
  return `index.embeddingStrategyVersion.${projectHash}`;
5917
5828
  }
5918
5829
  getProjectForceReembedMetadataKey() {
5919
- const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
5830
+ const projectHash = hashContent(path9.resolve(this.projectRoot)).slice(0, 16);
5920
5831
  return `index.forceReembed.${projectHash}`;
5921
5832
  }
5922
5833
  hasProjectForceReembedPending() {
@@ -6010,7 +5921,7 @@ var Indexer = class {
6010
5921
  if (!this.database) {
6011
5922
  return { chunkIds, symbolIds };
6012
5923
  }
6013
- const projectRootPath = path8.resolve(this.projectRoot);
5924
+ const projectRootPath = path9.resolve(this.projectRoot);
6014
5925
  const projectLocalFilePaths = /* @__PURE__ */ new Set([
6015
5926
  ...Array.from(this.fileHashCache.keys()).filter(
6016
5927
  (filePath) => this.isFileInCurrentScope(filePath, roots) && isPathWithinRoot(filePath, projectRootPath)
@@ -6033,7 +5944,7 @@ var Indexer = class {
6033
5944
  if (this.config.scope !== "global") {
6034
5945
  return this.getBranchCatalogCleanupKeys();
6035
5946
  }
6036
- const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
5947
+ const projectHash = hashContent(path9.resolve(this.projectRoot)).slice(0, 16);
6037
5948
  const keys = /* @__PURE__ */ new Set();
6038
5949
  const projectChunkIdSet = new Set(projectChunkIds);
6039
5950
  const projectSymbolIdSet = new Set(projectSymbolIds);
@@ -6117,7 +6028,7 @@ var Indexer = class {
6117
6028
  if (!this.database || this.config.scope !== "global") {
6118
6029
  return false;
6119
6030
  }
6120
- const projectHash = hashContent(path8.resolve(this.projectRoot)).slice(0, 16);
6031
+ const projectHash = hashContent(path9.resolve(this.projectRoot)).slice(0, 16);
6121
6032
  const roots = this.getScopedRoots();
6122
6033
  const { chunkIds: projectLocalChunkIds, symbolIds: projectLocalSymbolIds } = this.getProjectLocalScopedOwnershipIds(roots);
6123
6034
  return this.database.getAllBranches().some(
@@ -6151,7 +6062,7 @@ var Indexer = class {
6151
6062
  ...Array.from(this.fileHashCache.keys()).filter((filePath) => this.isFileInCurrentScope(filePath, roots)),
6152
6063
  ...scopedEntries.map(({ metadata }) => metadata.filePath)
6153
6064
  ]);
6154
- const projectRootPath = path8.resolve(this.projectRoot);
6065
+ const projectRootPath = path9.resolve(this.projectRoot);
6155
6066
  const projectLocalFilePaths = new Set(
6156
6067
  Array.from(filePaths).filter((filePath) => isPathWithinRoot(filePath, projectRootPath))
6157
6068
  );
@@ -6227,7 +6138,7 @@ var Indexer = class {
6227
6138
  startedAt: (/* @__PURE__ */ new Date()).toISOString(),
6228
6139
  pid: process.pid
6229
6140
  };
6230
- writeFileSync3(this.indexingLockPath, JSON.stringify(lockData));
6141
+ writeFileSync2(this.indexingLockPath, JSON.stringify(lockData));
6231
6142
  }
6232
6143
  releaseIndexingLock() {
6233
6144
  if (existsSync6(this.indexingLockPath)) {
@@ -6246,7 +6157,12 @@ var Indexer = class {
6246
6157
  loadFailedBatches(maxChunkTokens) {
6247
6158
  try {
6248
6159
  return this.loadSerializedFailedBatches().map((batch) => normalizeFailedBatch(batch, maxChunkTokens)).filter((batch) => batch !== null);
6249
- } catch {
6160
+ } catch (error) {
6161
+ const message = error instanceof Error ? error.message : String(error);
6162
+ this.logger.warn("Failed to load failed batch state, skipping persisted retries", {
6163
+ failedBatchesPath: this.failedBatchesPath,
6164
+ error: message
6165
+ });
6250
6166
  return [];
6251
6167
  }
6252
6168
  }
@@ -6279,7 +6195,7 @@ var Indexer = class {
6279
6195
  }
6280
6196
  return;
6281
6197
  }
6282
- writeFileSync3(this.failedBatchesPath, JSON.stringify(batches, null, 2));
6198
+ writeFileSync2(this.failedBatchesPath, JSON.stringify(batches, null, 2));
6283
6199
  }
6284
6200
  collectRetryableFailedChunks(currentFileHashes, unchangedFilePaths, maxChunkTokens) {
6285
6201
  const retryableById = /* @__PURE__ */ new Map();
@@ -6506,13 +6422,13 @@ var Indexer = class {
6506
6422
  }
6507
6423
  await fsPromises2.mkdir(this.indexPath, { recursive: true });
6508
6424
  const dimensions = this.configuredProviderInfo.modelInfo.dimensions;
6509
- const storePath = path8.join(this.indexPath, "vectors");
6425
+ const storePath = path9.join(this.indexPath, "vectors");
6510
6426
  this.store = new VectorStore(storePath, dimensions);
6511
- const indexFilePath = path8.join(this.indexPath, "vectors.usearch");
6427
+ const indexFilePath = path9.join(this.indexPath, "vectors.usearch");
6512
6428
  if (existsSync6(indexFilePath)) {
6513
6429
  this.store.load();
6514
6430
  }
6515
- const invertedIndexPath = path8.join(this.indexPath, "inverted-index.json");
6431
+ const invertedIndexPath = path9.join(this.indexPath, "inverted-index.json");
6516
6432
  this.invertedIndex = new InvertedIndex(invertedIndexPath);
6517
6433
  try {
6518
6434
  this.invertedIndex.load();
@@ -6522,7 +6438,7 @@ var Indexer = class {
6522
6438
  }
6523
6439
  this.invertedIndex = new InvertedIndex(invertedIndexPath);
6524
6440
  }
6525
- const dbPath = path8.join(this.indexPath, "codebase.db");
6441
+ const dbPath = path9.join(this.indexPath, "codebase.db");
6526
6442
  let dbIsNew = !existsSync6(dbPath);
6527
6443
  try {
6528
6444
  this.database = new Database(dbPath);
@@ -6603,7 +6519,7 @@ var Indexer = class {
6603
6519
  if (await this.tryResetCorruptedIndex("running automatic orphan garbage collection", error)) {
6604
6520
  return {
6605
6521
  resetCorruptedIndex: true,
6606
- warning: this.getCorruptedIndexWarning(path8.join(this.indexPath, "codebase.db"))
6522
+ warning: this.getCorruptedIndexWarning(path9.join(this.indexPath, "codebase.db"))
6607
6523
  };
6608
6524
  }
6609
6525
  throw error;
@@ -6618,7 +6534,7 @@ var Indexer = class {
6618
6534
  return;
6619
6535
  }
6620
6536
  const retainedEntries = store.getAllMetadata().filter(({ key }) => !excludedSet.has(key));
6621
- const storeBasePath = path8.join(this.indexPath, "vectors");
6537
+ const storeBasePath = path9.join(this.indexPath, "vectors");
6622
6538
  const storeIndexPath = `${storeBasePath}.usearch`;
6623
6539
  const storeMetadataPath = `${storeBasePath}.meta.json`;
6624
6540
  const backupIndexPath = `${storeIndexPath}.bak`;
@@ -6703,7 +6619,7 @@ var Indexer = class {
6703
6619
  if (!isSqliteCorruptionError(error)) {
6704
6620
  return false;
6705
6621
  }
6706
- const dbPath = path8.join(this.indexPath, "codebase.db");
6622
+ const dbPath = path9.join(this.indexPath, "codebase.db");
6707
6623
  const warning = this.getCorruptedIndexWarning(dbPath);
6708
6624
  const errorMessage = getErrorMessage(error);
6709
6625
  if (this.config.scope === "global") {
@@ -6726,15 +6642,15 @@ var Indexer = class {
6726
6642
  this.indexCompatibility = null;
6727
6643
  this.fileHashCache.clear();
6728
6644
  const resetPaths = [
6729
- path8.join(this.indexPath, "codebase.db"),
6730
- path8.join(this.indexPath, "codebase.db-shm"),
6731
- path8.join(this.indexPath, "codebase.db-wal"),
6732
- path8.join(this.indexPath, "vectors.usearch"),
6733
- path8.join(this.indexPath, "inverted-index.json"),
6734
- path8.join(this.indexPath, "file-hashes.json"),
6735
- path8.join(this.indexPath, "failed-batches.json"),
6736
- path8.join(this.indexPath, "indexing.lock"),
6737
- path8.join(this.indexPath, "vectors")
6645
+ path9.join(this.indexPath, "codebase.db"),
6646
+ path9.join(this.indexPath, "codebase.db-shm"),
6647
+ path9.join(this.indexPath, "codebase.db-wal"),
6648
+ path9.join(this.indexPath, "vectors.usearch"),
6649
+ path9.join(this.indexPath, "inverted-index.json"),
6650
+ path9.join(this.indexPath, "file-hashes.json"),
6651
+ path9.join(this.indexPath, "failed-batches.json"),
6652
+ path9.join(this.indexPath, "indexing.lock"),
6653
+ path9.join(this.indexPath, "vectors")
6738
6654
  ];
6739
6655
  await Promise.all(resetPaths.map(async (targetPath) => {
6740
6656
  try {
@@ -6999,7 +6915,7 @@ var Indexer = class {
6999
6915
  for (const parsed of parsedFiles) {
7000
6916
  currentFilePaths.add(parsed.path);
7001
6917
  if (parsed.chunks.length === 0) {
7002
- const relativePath = path8.relative(this.projectRoot, parsed.path);
6918
+ const relativePath = path9.relative(this.projectRoot, parsed.path);
7003
6919
  stats.parseFailures.push(relativePath);
7004
6920
  }
7005
6921
  let fileChunkCount = 0;
@@ -7282,7 +7198,7 @@ var Indexer = class {
7282
7198
  for (const requestBatch of requestBatches) {
7283
7199
  queue.add(async () => {
7284
7200
  if (rateLimitBackoffMs > 0) {
7285
- await new Promise((resolve8) => setTimeout(resolve8, rateLimitBackoffMs));
7201
+ await new Promise((resolve10) => setTimeout(resolve10, rateLimitBackoffMs));
7286
7202
  }
7287
7203
  try {
7288
7204
  const result = await pRetry(
@@ -7843,8 +7759,8 @@ var Indexer = class {
7843
7759
  this.indexCompatibility = compatibility;
7844
7760
  return;
7845
7761
  }
7846
- const localProjectIndexPath = path8.join(this.projectRoot, ".opencode", "index");
7847
- if (path8.resolve(this.indexPath) !== path8.resolve(localProjectIndexPath)) {
7762
+ const localProjectIndexPath = path9.join(this.projectRoot, ".opencode", "index");
7763
+ if (path9.resolve(this.indexPath) !== path9.resolve(localProjectIndexPath)) {
7848
7764
  throw new Error(
7849
7765
  "Project-scoped force rebuild is unsafe while using an inherited worktree index. Create a local project config boundary before clearing the index."
7850
7766
  );
@@ -7932,7 +7848,7 @@ var Indexer = class {
7932
7848
  gcOrphanSymbols: 0,
7933
7849
  gcOrphanCallEdges: 0,
7934
7850
  resetCorruptedIndex: true,
7935
- warning: this.getCorruptedIndexWarning(path8.join(this.indexPath, "codebase.db"))
7851
+ warning: this.getCorruptedIndexWarning(path9.join(this.indexPath, "codebase.db"))
7936
7852
  };
7937
7853
  }
7938
7854
  this.logger.recordGc(removedCount, gcOrphanChunks, gcOrphanEmbeddings);
@@ -8378,7 +8294,7 @@ function percentile(values, p) {
8378
8294
  return sorted[lowerIndex] + fraction * (sorted[upperIndex] - sorted[lowerIndex]);
8379
8295
  }
8380
8296
  function normalizePath(input) {
8381
- return input.replace(/\\/g, "/");
8297
+ return normalizePathSeparators(input);
8382
8298
  }
8383
8299
  function uniqueResultsByPath(results) {
8384
8300
  const seen = /* @__PURE__ */ new Set();
@@ -8541,53 +8457,257 @@ function computeEvalMetrics(queries, perQuery, embeddingCallCount, embeddingToke
8541
8457
  };
8542
8458
  }
8543
8459
 
8460
+ // src/eval/runner-config.ts
8461
+ import { existsSync as existsSync7, mkdirSync as mkdirSync3, readFileSync as readFileSync7, rmSync, writeFileSync as writeFileSync3 } from "fs";
8462
+ import * as os4 from "os";
8463
+ import * as path11 from "path";
8464
+
8465
+ // src/config/rebase.ts
8466
+ import * as path10 from "path";
8467
+ function isWithinRoot(rootDir, targetPath) {
8468
+ const relativePath = path10.relative(rootDir, targetPath);
8469
+ return relativePath === "" || !relativePath.startsWith("..") && !path10.isAbsolute(relativePath);
8470
+ }
8471
+ function rebasePathEntries(values, fromDir, toDir) {
8472
+ if (!Array.isArray(values)) {
8473
+ return [];
8474
+ }
8475
+ return values.filter((value) => typeof value === "string").map((value) => {
8476
+ const trimmed = value.trim();
8477
+ if (!trimmed || path10.isAbsolute(trimmed)) {
8478
+ return trimmed;
8479
+ }
8480
+ return normalizePathSeparators(path10.normalize(path10.relative(toDir, path10.resolve(fromDir, trimmed))));
8481
+ }).filter(Boolean);
8482
+ }
8483
+ function resolveInheritedKnowledgeBaseEntries(values, sourceRoot, targetRoot) {
8484
+ if (!Array.isArray(values)) {
8485
+ return [];
8486
+ }
8487
+ return values.filter((value) => typeof value === "string").map((value) => {
8488
+ const trimmed = value.trim();
8489
+ if (!trimmed) {
8490
+ return trimmed;
8491
+ }
8492
+ if (path10.isAbsolute(trimmed)) {
8493
+ if (isWithinRoot(sourceRoot, trimmed)) {
8494
+ return normalizePathSeparators(path10.normalize(path10.relative(sourceRoot, trimmed) || "."));
8495
+ }
8496
+ return path10.normalize(trimmed);
8497
+ }
8498
+ const resolvedFromSource = path10.resolve(sourceRoot, trimmed);
8499
+ if (isWithinRoot(sourceRoot, resolvedFromSource)) {
8500
+ return normalizePathSeparators(path10.normalize(trimmed));
8501
+ }
8502
+ return normalizePathSeparators(path10.normalize(path10.relative(targetRoot, resolvedFromSource)));
8503
+ }).filter(Boolean);
8504
+ }
8505
+
8506
+ // src/eval/runner-config.ts
8507
+ function isRecord(value) {
8508
+ return typeof value === "object" && value !== null && !Array.isArray(value);
8509
+ }
8510
+ function isStringArray2(value) {
8511
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
8512
+ }
8513
+ function validateEvalConfigShape(rawConfig, filePath) {
8514
+ if (!isRecord(rawConfig)) {
8515
+ throw new Error(`Eval config at ${filePath} must contain a JSON object at the root.`);
8516
+ }
8517
+ const config = rawConfig;
8518
+ if (config.knowledgeBases !== void 0 && !isStringArray2(config.knowledgeBases)) {
8519
+ throw new Error(`Eval config at ${filePath} field 'knowledgeBases' must be an array of strings.`);
8520
+ }
8521
+ if (config.additionalInclude !== void 0 && !isStringArray2(config.additionalInclude)) {
8522
+ throw new Error(`Eval config at ${filePath} field 'additionalInclude' must be an array of strings.`);
8523
+ }
8524
+ if (config.include !== void 0 && !isStringArray2(config.include)) {
8525
+ throw new Error(`Eval config at ${filePath} field 'include' must be an array of strings.`);
8526
+ }
8527
+ if (config.exclude !== void 0 && !isStringArray2(config.exclude)) {
8528
+ throw new Error(`Eval config at ${filePath} field 'exclude' must be an array of strings.`);
8529
+ }
8530
+ for (const section of ["customProvider", "indexing", "search", "debug", "reranker"]) {
8531
+ const value = config[section];
8532
+ if (value !== void 0 && !isRecord(value)) {
8533
+ throw new Error(`Eval config at ${filePath} field '${section}' must be an object.`);
8534
+ }
8535
+ }
8536
+ return config;
8537
+ }
8538
+ function parseJsonConfigFile(filePath) {
8539
+ try {
8540
+ return validateEvalConfigShape(JSON.parse(readFileSync7(filePath, "utf-8")), filePath);
8541
+ } catch (error) {
8542
+ if (error instanceof Error && error.message.startsWith("Eval config at ")) {
8543
+ throw error;
8544
+ }
8545
+ const message = error instanceof Error ? error.message : String(error);
8546
+ throw new Error(`Failed to parse eval config JSON at ${filePath}: ${message}`);
8547
+ }
8548
+ }
8549
+ function toAbsolute(projectRoot, maybeRelative) {
8550
+ return path11.isAbsolute(maybeRelative) ? maybeRelative : path11.join(projectRoot, maybeRelative);
8551
+ }
8552
+ function isProjectScopedConfigPath(configPath) {
8553
+ return path11.basename(configPath) === "codebase-index.json" && path11.basename(path11.dirname(configPath)) === ".opencode";
8554
+ }
8555
+ function normalizeEvalConfigKnowledgeBases(rawConfig, projectRoot, resolvedConfigPath) {
8556
+ const config = rawConfig && typeof rawConfig === "object" ? { ...rawConfig } : {};
8557
+ const rebaseEntries = (values) => isProjectScopedConfigPath(resolvedConfigPath) ? resolveInheritedKnowledgeBaseEntries(
8558
+ values,
8559
+ path11.dirname(path11.dirname(resolvedConfigPath)),
8560
+ projectRoot
8561
+ ) : rebasePathEntries(
8562
+ values,
8563
+ path11.dirname(resolvedConfigPath),
8564
+ projectRoot
8565
+ );
8566
+ if (Array.isArray(config.knowledgeBases)) {
8567
+ config.knowledgeBases = rebaseEntries(config.knowledgeBases);
8568
+ }
8569
+ if (Array.isArray(config.additionalInclude)) {
8570
+ config.additionalInclude = rebaseEntries(config.additionalInclude);
8571
+ }
8572
+ return config;
8573
+ }
8574
+ function loadRawConfig(projectRoot, configPath) {
8575
+ const fromPath = configPath ? toAbsolute(projectRoot, configPath) : null;
8576
+ if (fromPath && existsSync7(fromPath)) {
8577
+ return normalizeEvalConfigKnowledgeBases(
8578
+ parseJsonConfigFile(fromPath),
8579
+ projectRoot,
8580
+ fromPath
8581
+ );
8582
+ }
8583
+ const projectConfig = resolveProjectConfigPath(projectRoot);
8584
+ if (existsSync7(projectConfig)) {
8585
+ return normalizeEvalConfigKnowledgeBases(
8586
+ parseJsonConfigFile(projectConfig),
8587
+ projectRoot,
8588
+ projectConfig
8589
+ );
8590
+ }
8591
+ const globalConfig = path11.join(os4.homedir(), ".config", "opencode", "codebase-index.json");
8592
+ if (existsSync7(globalConfig)) {
8593
+ return parseJsonConfigFile(globalConfig);
8594
+ }
8595
+ return {};
8596
+ }
8597
+ function getIndexRootPath(projectRoot, scope) {
8598
+ return scope === "global" ? getGlobalIndexPath() : resolveProjectIndexPath(projectRoot, scope);
8599
+ }
8600
+ function getLocalProjectIndexRoot(projectRoot) {
8601
+ return path11.join(projectRoot, ".opencode", "index");
8602
+ }
8603
+ function getLocalProjectConfigPath(projectRoot) {
8604
+ return path11.join(projectRoot, ".opencode", "codebase-index.json");
8605
+ }
8606
+ function clearIndexRoot(projectRoot, scope) {
8607
+ const indexRoot = scope === "global" ? getIndexRootPath(projectRoot, scope) : getLocalProjectIndexRoot(projectRoot);
8608
+ if (existsSync7(indexRoot)) {
8609
+ rmSync(indexRoot, { recursive: true, force: true });
8610
+ }
8611
+ }
8612
+ function ensureLocalEvalProjectConfig(projectRoot, configPath) {
8613
+ const localConfigPath = getLocalProjectConfigPath(projectRoot);
8614
+ const resolvedConfigPath = configPath ? toAbsolute(projectRoot, configPath) : resolveProjectConfigPath(projectRoot);
8615
+ if (!configPath && existsSync7(localConfigPath)) {
8616
+ return localConfigPath;
8617
+ }
8618
+ if (!existsSync7(resolvedConfigPath) || resolvedConfigPath === localConfigPath) {
8619
+ return resolvedConfigPath;
8620
+ }
8621
+ const sourceConfig = normalizeEvalConfigKnowledgeBases(
8622
+ parseJsonConfigFile(resolvedConfigPath),
8623
+ projectRoot,
8624
+ resolvedConfigPath
8625
+ );
8626
+ mkdirSync3(path11.dirname(localConfigPath), { recursive: true });
8627
+ writeFileSync3(localConfigPath, JSON.stringify(sourceConfig, null, 2), "utf-8");
8628
+ return localConfigPath;
8629
+ }
8630
+ function loadParsedConfig(projectRoot, configPath) {
8631
+ const raw = loadRawConfig(projectRoot, configPath);
8632
+ return parseConfig(raw);
8633
+ }
8634
+ function resolveSearchConfig(parsedConfig, overrides) {
8635
+ const nextSearch = {
8636
+ ...parsedConfig.search
8637
+ };
8638
+ if (overrides?.fusionStrategy !== void 0) {
8639
+ nextSearch.fusionStrategy = overrides.fusionStrategy;
8640
+ }
8641
+ if (overrides?.hybridWeight !== void 0) {
8642
+ nextSearch.hybridWeight = overrides.hybridWeight;
8643
+ }
8644
+ if (overrides?.rrfK !== void 0) {
8645
+ nextSearch.rrfK = overrides.rrfK;
8646
+ }
8647
+ if (overrides?.rerankTopN !== void 0) {
8648
+ nextSearch.rerankTopN = overrides.rerankTopN;
8649
+ }
8650
+ return {
8651
+ ...parsedConfig,
8652
+ search: nextSearch
8653
+ };
8654
+ }
8655
+ function getEmbeddingCostPer1MTokens(embeddingProvider) {
8656
+ return embeddingProvider === "custom" || embeddingProvider === "auto" ? 0 : getDefaultModelForProvider(embeddingProvider).costPer1MTokens;
8657
+ }
8658
+
8544
8659
  // src/eval/schema.ts
8545
- import { readFileSync as readFileSync7 } from "fs";
8660
+ import { readFileSync as readFileSync8 } from "fs";
8546
8661
  function parseJsonFile(filePath) {
8547
- const content = readFileSync7(filePath, "utf-8");
8548
- return JSON.parse(content);
8662
+ const content = readFileSync8(filePath, "utf-8");
8663
+ try {
8664
+ return JSON.parse(content);
8665
+ } catch (error) {
8666
+ const message = error instanceof Error ? error.message : String(error);
8667
+ throw new Error(`Failed to parse JSON from ${filePath}: ${message}`);
8668
+ }
8549
8669
  }
8550
- function isRecord(value) {
8670
+ function isRecord2(value) {
8551
8671
  return typeof value === "object" && value !== null;
8552
8672
  }
8553
- function isStringArray2(value) {
8673
+ function isStringArray3(value) {
8554
8674
  return Array.isArray(value) && value.every((item) => typeof item === "string");
8555
8675
  }
8556
- function asPositiveNumber(value, path13) {
8676
+ function asPositiveNumber(value, path18) {
8557
8677
  if (typeof value !== "number" || Number.isNaN(value) || value < 0) {
8558
- throw new Error(`${path13} must be a non-negative number`);
8678
+ throw new Error(`${path18} must be a non-negative number`);
8559
8679
  }
8560
8680
  return value;
8561
8681
  }
8562
- function parseQueryType(value, path13) {
8682
+ function parseQueryType(value, path18) {
8563
8683
  if (value === "definition" || value === "implementation-intent" || value === "similarity" || value === "keyword-heavy") {
8564
8684
  return value;
8565
8685
  }
8566
8686
  throw new Error(
8567
- `${path13} must be one of: definition, implementation-intent, similarity, keyword-heavy`
8687
+ `${path18} must be one of: definition, implementation-intent, similarity, keyword-heavy`
8568
8688
  );
8569
8689
  }
8570
- function parseExpected(input, path13) {
8571
- if (!isRecord(input)) {
8572
- throw new Error(`${path13} must be an object`);
8690
+ function parseExpected(input, path18) {
8691
+ if (!isRecord2(input)) {
8692
+ throw new Error(`${path18} must be an object`);
8573
8693
  }
8574
8694
  const filePathRaw = input.filePath;
8575
8695
  const acceptableFilesRaw = input.acceptableFiles;
8576
8696
  const symbolRaw = input.symbol;
8577
8697
  const branchRaw = input.branch;
8578
8698
  const filePath = typeof filePathRaw === "string" ? filePathRaw : void 0;
8579
- const acceptableFiles = isStringArray2(acceptableFilesRaw) ? acceptableFilesRaw : void 0;
8699
+ const acceptableFiles = isStringArray3(acceptableFilesRaw) ? acceptableFilesRaw : void 0;
8580
8700
  if (!filePath && (!acceptableFiles || acceptableFiles.length === 0)) {
8581
- throw new Error(`${path13} must include either expected.filePath or expected.acceptableFiles`);
8701
+ throw new Error(`${path18} must include either expected.filePath or expected.acceptableFiles`);
8582
8702
  }
8583
- if (acceptableFilesRaw !== void 0 && !isStringArray2(acceptableFilesRaw)) {
8584
- throw new Error(`${path13}.acceptableFiles must be an array of strings`);
8703
+ if (acceptableFilesRaw !== void 0 && !isStringArray3(acceptableFilesRaw)) {
8704
+ throw new Error(`${path18}.acceptableFiles must be an array of strings`);
8585
8705
  }
8586
8706
  if (symbolRaw !== void 0 && typeof symbolRaw !== "string") {
8587
- throw new Error(`${path13}.symbol must be a string when provided`);
8707
+ throw new Error(`${path18}.symbol must be a string when provided`);
8588
8708
  }
8589
8709
  if (branchRaw !== void 0 && typeof branchRaw !== "string") {
8590
- throw new Error(`${path13}.branch must be a string when provided`);
8710
+ throw new Error(`${path18}.branch must be a string when provided`);
8591
8711
  }
8592
8712
  return {
8593
8713
  filePath,
@@ -8597,29 +8717,29 @@ function parseExpected(input, path13) {
8597
8717
  };
8598
8718
  }
8599
8719
  function parseQuery(input, index) {
8600
- const path13 = `queries[${index}]`;
8601
- if (!isRecord(input)) {
8602
- throw new Error(`${path13} must be an object`);
8720
+ const path18 = `queries[${index}]`;
8721
+ if (!isRecord2(input)) {
8722
+ throw new Error(`${path18} must be an object`);
8603
8723
  }
8604
8724
  const id = input.id;
8605
8725
  const query = input.query;
8606
8726
  const queryType = input.queryType;
8607
8727
  const expected = input.expected;
8608
8728
  if (typeof id !== "string" || id.trim().length === 0) {
8609
- throw new Error(`${path13}.id must be a non-empty string`);
8729
+ throw new Error(`${path18}.id must be a non-empty string`);
8610
8730
  }
8611
8731
  if (typeof query !== "string" || query.trim().length === 0) {
8612
- throw new Error(`${path13}.query must be a non-empty string`);
8732
+ throw new Error(`${path18}.query must be a non-empty string`);
8613
8733
  }
8614
8734
  return {
8615
8735
  id,
8616
8736
  query,
8617
- queryType: parseQueryType(queryType, `${path13}.queryType`),
8618
- expected: parseExpected(expected, `${path13}.expected`)
8737
+ queryType: parseQueryType(queryType, `${path18}.queryType`),
8738
+ expected: parseExpected(expected, `${path18}.expected`)
8619
8739
  };
8620
8740
  }
8621
8741
  function parseGoldenDataset(raw, sourceLabel) {
8622
- if (!isRecord(raw)) {
8742
+ if (!isRecord2(raw)) {
8623
8743
  throw new Error(`${sourceLabel} must be a JSON object`);
8624
8744
  }
8625
8745
  const version = raw.version;
@@ -8660,8 +8780,11 @@ function loadGoldenDataset(datasetPath) {
8660
8780
  const parsed = parseJsonFile(datasetPath);
8661
8781
  return parseGoldenDataset(parsed, datasetPath);
8662
8782
  }
8783
+ function parseThresholdValue(value, fieldName, sourceLabel) {
8784
+ return value === void 0 ? void 0 : asPositiveNumber(value, `${sourceLabel}.thresholds.${fieldName}`);
8785
+ }
8663
8786
  function parseBudget(raw, sourceLabel) {
8664
- if (!isRecord(raw)) {
8787
+ if (!isRecord2(raw)) {
8665
8788
  throw new Error(`${sourceLabel} must be a JSON object`);
8666
8789
  }
8667
8790
  const name = raw.name;
@@ -8674,7 +8797,7 @@ function parseBudget(raw, sourceLabel) {
8674
8797
  if (baselinePath !== void 0 && typeof baselinePath !== "string") {
8675
8798
  throw new Error(`${sourceLabel}.baselinePath must be a string when provided`);
8676
8799
  }
8677
- if (!isRecord(thresholds)) {
8800
+ if (!isRecord2(thresholds)) {
8678
8801
  throw new Error(`${sourceLabel}.thresholds must be an object`);
8679
8802
  }
8680
8803
  return {
@@ -8682,25 +8805,45 @@ function parseBudget(raw, sourceLabel) {
8682
8805
  baselinePath: typeof baselinePath === "string" ? baselinePath : void 0,
8683
8806
  failOnMissingBaseline: typeof failOnMissingBaseline === "boolean" ? failOnMissingBaseline : true,
8684
8807
  thresholds: {
8685
- hitAt5MaxDrop: thresholds.hitAt5MaxDrop === void 0 ? void 0 : asPositiveNumber(thresholds.hitAt5MaxDrop, `${sourceLabel}.thresholds.hitAt5MaxDrop`),
8686
- mrrAt10MaxDrop: thresholds.mrrAt10MaxDrop === void 0 ? void 0 : asPositiveNumber(thresholds.mrrAt10MaxDrop, `${sourceLabel}.thresholds.mrrAt10MaxDrop`),
8687
- rawDistinctTop3RatioMaxDrop: thresholds.rawDistinctTop3RatioMaxDrop === void 0 ? void 0 : asPositiveNumber(
8808
+ hitAt5MaxDrop: parseThresholdValue(
8809
+ thresholds.hitAt5MaxDrop,
8810
+ "hitAt5MaxDrop",
8811
+ sourceLabel
8812
+ ),
8813
+ mrrAt10MaxDrop: parseThresholdValue(
8814
+ thresholds.mrrAt10MaxDrop,
8815
+ "mrrAt10MaxDrop",
8816
+ sourceLabel
8817
+ ),
8818
+ rawDistinctTop3RatioMaxDrop: parseThresholdValue(
8688
8819
  thresholds.rawDistinctTop3RatioMaxDrop,
8689
- `${sourceLabel}.thresholds.rawDistinctTop3RatioMaxDrop`
8820
+ "rawDistinctTop3RatioMaxDrop",
8821
+ sourceLabel
8690
8822
  ),
8691
- p95LatencyMaxMultiplier: thresholds.p95LatencyMaxMultiplier === void 0 ? void 0 : asPositiveNumber(
8823
+ p95LatencyMaxMultiplier: parseThresholdValue(
8692
8824
  thresholds.p95LatencyMaxMultiplier,
8693
- `${sourceLabel}.thresholds.p95LatencyMaxMultiplier`
8825
+ "p95LatencyMaxMultiplier",
8826
+ sourceLabel
8694
8827
  ),
8695
- p95LatencyMaxAbsoluteMs: thresholds.p95LatencyMaxAbsoluteMs === void 0 ? void 0 : asPositiveNumber(
8828
+ p95LatencyMaxAbsoluteMs: parseThresholdValue(
8696
8829
  thresholds.p95LatencyMaxAbsoluteMs,
8697
- `${sourceLabel}.thresholds.p95LatencyMaxAbsoluteMs`
8830
+ "p95LatencyMaxAbsoluteMs",
8831
+ sourceLabel
8698
8832
  ),
8699
- minHitAt5: thresholds.minHitAt5 === void 0 ? void 0 : asPositiveNumber(thresholds.minHitAt5, `${sourceLabel}.thresholds.minHitAt5`),
8700
- minMrrAt10: thresholds.minMrrAt10 === void 0 ? void 0 : asPositiveNumber(thresholds.minMrrAt10, `${sourceLabel}.thresholds.minMrrAt10`),
8701
- minRawDistinctTop3Ratio: thresholds.minRawDistinctTop3Ratio === void 0 ? void 0 : asPositiveNumber(
8833
+ minHitAt5: parseThresholdValue(
8834
+ thresholds.minHitAt5,
8835
+ "minHitAt5",
8836
+ sourceLabel
8837
+ ),
8838
+ minMrrAt10: parseThresholdValue(
8839
+ thresholds.minMrrAt10,
8840
+ "minMrrAt10",
8841
+ sourceLabel
8842
+ ),
8843
+ minRawDistinctTop3Ratio: parseThresholdValue(
8702
8844
  thresholds.minRawDistinctTop3Ratio,
8703
- `${sourceLabel}.thresholds.minRawDistinctTop3Ratio`
8845
+ "minRawDistinctTop3Ratio",
8846
+ sourceLabel
8704
8847
  )
8705
8848
  }
8706
8849
  };
@@ -8711,109 +8854,6 @@ function loadBudget(budgetPath) {
8711
8854
  }
8712
8855
 
8713
8856
  // src/eval/runner.ts
8714
- function toAbsolute(projectRoot, maybeRelative) {
8715
- return path9.isAbsolute(maybeRelative) ? maybeRelative : path9.join(projectRoot, maybeRelative);
8716
- }
8717
- function isProjectScopedConfigPath(configPath) {
8718
- return path9.basename(configPath) === "codebase-index.json" && path9.basename(path9.dirname(configPath)) === ".opencode";
8719
- }
8720
- function normalizeEvalConfigKnowledgeBases(rawConfig, projectRoot, resolvedConfigPath) {
8721
- const config = rawConfig && typeof rawConfig === "object" ? { ...rawConfig } : {};
8722
- if (!Array.isArray(config.knowledgeBases)) {
8723
- return config;
8724
- }
8725
- config.knowledgeBases = isProjectScopedConfigPath(resolvedConfigPath) ? resolveInheritedKnowledgeBaseEntries(
8726
- config.knowledgeBases,
8727
- path9.dirname(path9.dirname(resolvedConfigPath)),
8728
- projectRoot
8729
- ) : rebasePathEntries(
8730
- config.knowledgeBases,
8731
- path9.dirname(resolvedConfigPath),
8732
- projectRoot
8733
- );
8734
- return config;
8735
- }
8736
- function loadRawConfig(projectRoot, configPath) {
8737
- const fromPath = configPath ? toAbsolute(projectRoot, configPath) : null;
8738
- if (fromPath && existsSync7(fromPath)) {
8739
- return normalizeEvalConfigKnowledgeBases(
8740
- JSON.parse(readFileSync8(fromPath, "utf-8")),
8741
- projectRoot,
8742
- fromPath
8743
- );
8744
- }
8745
- const projectConfig = resolveProjectConfigPath(projectRoot);
8746
- if (existsSync7(projectConfig)) {
8747
- return normalizeEvalConfigKnowledgeBases(
8748
- JSON.parse(readFileSync8(projectConfig, "utf-8")),
8749
- projectRoot,
8750
- projectConfig
8751
- );
8752
- }
8753
- const globalConfig = path9.join(os5.homedir(), ".config", "opencode", "codebase-index.json");
8754
- if (existsSync7(globalConfig)) {
8755
- return JSON.parse(readFileSync8(globalConfig, "utf-8"));
8756
- }
8757
- return {};
8758
- }
8759
- function getIndexRootPath(projectRoot, scope) {
8760
- return scope === "global" ? getGlobalIndexPath() : resolveProjectIndexPath(projectRoot, scope);
8761
- }
8762
- function getLocalProjectIndexRoot(projectRoot) {
8763
- return path9.join(projectRoot, ".opencode", "index");
8764
- }
8765
- function getLocalProjectConfigPath(projectRoot) {
8766
- return path9.join(projectRoot, ".opencode", "codebase-index.json");
8767
- }
8768
- function clearIndexRoot(projectRoot, scope) {
8769
- const indexRoot = scope === "global" ? getIndexRootPath(projectRoot, scope) : getLocalProjectIndexRoot(projectRoot);
8770
- if (existsSync7(indexRoot)) {
8771
- rmSync(indexRoot, { recursive: true, force: true });
8772
- }
8773
- }
8774
- function ensureLocalEvalProjectConfig(projectRoot, configPath) {
8775
- const localConfigPath = getLocalProjectConfigPath(projectRoot);
8776
- const resolvedConfigPath = configPath ? toAbsolute(projectRoot, configPath) : resolveProjectConfigPath(projectRoot);
8777
- if (!configPath && existsSync7(localConfigPath)) {
8778
- return localConfigPath;
8779
- }
8780
- if (!existsSync7(resolvedConfigPath) || resolvedConfigPath === localConfigPath) {
8781
- return resolvedConfigPath;
8782
- }
8783
- const sourceConfig = normalizeEvalConfigKnowledgeBases(
8784
- JSON.parse(readFileSync8(resolvedConfigPath, "utf-8")),
8785
- projectRoot,
8786
- resolvedConfigPath
8787
- );
8788
- mkdirSync4(path9.dirname(localConfigPath), { recursive: true });
8789
- writeFileSync4(localConfigPath, JSON.stringify(sourceConfig, null, 2), "utf-8");
8790
- return localConfigPath;
8791
- }
8792
- function loadParsedConfig(projectRoot, configPath) {
8793
- const raw = loadRawConfig(projectRoot, configPath);
8794
- return parseConfig(raw);
8795
- }
8796
- function resolveSearchConfig(parsedConfig, overrides) {
8797
- const nextSearch = {
8798
- ...parsedConfig.search
8799
- };
8800
- if (overrides?.fusionStrategy !== void 0) {
8801
- nextSearch.fusionStrategy = overrides.fusionStrategy;
8802
- }
8803
- if (overrides?.hybridWeight !== void 0) {
8804
- nextSearch.hybridWeight = overrides.hybridWeight;
8805
- }
8806
- if (overrides?.rrfK !== void 0) {
8807
- nextSearch.rrfK = overrides.rrfK;
8808
- }
8809
- if (overrides?.rerankTopN !== void 0) {
8810
- nextSearch.rerankTopN = overrides.rerankTopN;
8811
- }
8812
- return {
8813
- ...parsedConfig,
8814
- search: nextSearch
8815
- };
8816
- }
8817
8857
  async function runEvaluation(options) {
8818
8858
  const datasetPath = toAbsolute(options.projectRoot, options.datasetPath);
8819
8859
  const againstPath = options.againstPath ? toAbsolute(options.projectRoot, options.againstPath) : void 0;
@@ -8838,7 +8878,7 @@ async function runEvaluation(options) {
8838
8878
  const start = performance3.now();
8839
8879
  const result = await indexer.search(query.query, 10, {
8840
8880
  metadataOnly: true,
8841
- filterByBranch: query.expected.branch ? true : false
8881
+ filterByBranch: !!query.expected.branch
8842
8882
  });
8843
8883
  const elapsed = performance3.now() - start;
8844
8884
  const materialized = result.map((item) => ({
@@ -8853,7 +8893,7 @@ async function runEvaluation(options) {
8853
8893
  }
8854
8894
  const logger = indexer.getLogger();
8855
8895
  const metricSnapshot = logger.getMetrics();
8856
- const costPer1MTokensUsd = effectiveConfig.embeddingProvider === "custom" || effectiveConfig.embeddingProvider === "auto" ? 0 : getDefaultModelForProvider(effectiveConfig.embeddingProvider).costPer1MTokens;
8896
+ const costPer1MTokensUsd = getEmbeddingCostPer1MTokens(effectiveConfig.embeddingProvider);
8857
8897
  const summary = {
8858
8898
  generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
8859
8899
  projectRoot: options.projectRoot,
@@ -8878,13 +8918,13 @@ async function runEvaluation(options) {
8878
8918
  };
8879
8919
  const outputDir = createRunDirectory(toAbsolute(options.projectRoot, options.outputRoot));
8880
8920
  const perQueryArtifact = buildPerQueryArtifact(perQuery);
8881
- writeJson(path9.join(outputDir, "summary.json"), summary);
8882
- writeJson(path9.join(outputDir, "per-query.json"), perQueryArtifact);
8921
+ writeJson(path12.join(outputDir, "summary.json"), summary);
8922
+ writeJson(path12.join(outputDir, "per-query.json"), perQueryArtifact);
8883
8923
  let comparison;
8884
8924
  if (againstPath) {
8885
8925
  const baseline = loadSummary(againstPath);
8886
8926
  comparison = compareSummaries(summary, baseline, againstPath);
8887
- writeJson(path9.join(outputDir, "compare.json"), comparison);
8927
+ writeJson(path12.join(outputDir, "compare.json"), comparison);
8888
8928
  }
8889
8929
  let gate;
8890
8930
  if (options.ciMode) {
@@ -8894,10 +8934,10 @@ async function runEvaluation(options) {
8894
8934
  const budget = loadBudget(budgetPath);
8895
8935
  if (!comparison && budget.baselinePath) {
8896
8936
  const resolvedBaseline = toAbsolute(options.projectRoot, budget.baselinePath);
8897
- if (existsSync7(resolvedBaseline)) {
8937
+ if (existsSync8(resolvedBaseline)) {
8898
8938
  const baselineSummary = loadSummary(resolvedBaseline);
8899
8939
  comparison = compareSummaries(summary, baselineSummary, resolvedBaseline);
8900
- writeJson(path9.join(outputDir, "compare.json"), comparison);
8940
+ writeJson(path12.join(outputDir, "compare.json"), comparison);
8901
8941
  } else if (budget.failOnMissingBaseline) {
8902
8942
  throw new Error(
8903
8943
  `Budget baseline is missing: ${resolvedBaseline}. Set failOnMissingBaseline=false to allow CI run without baseline.`
@@ -8907,7 +8947,7 @@ async function runEvaluation(options) {
8907
8947
  gate = evaluateBudgetGate(budget, summary, comparison);
8908
8948
  }
8909
8949
  const markdown = createSummaryMarkdown(summary, comparison, gate);
8910
- writeText(path9.join(outputDir, "summary.md"), markdown);
8950
+ writeText(path12.join(outputDir, "summary.md"), markdown);
8911
8951
  return { outputDir, summary, perQuery, comparison, gate };
8912
8952
  } finally {
8913
8953
  await indexer.close();
@@ -8965,19 +9005,23 @@ async function runSweep(options, sweep) {
8965
9005
  bestByMrrAt10,
8966
9006
  bestByP95Latency
8967
9007
  };
8968
- writeJson(path9.join(outputDir, "compare.json"), aggregate);
9008
+ writeJson(path12.join(outputDir, "compare.json"), aggregate);
8969
9009
  const md = createSummaryMarkdown(
8970
9010
  bestByHitAt5?.summary ?? runs[0].summary,
8971
9011
  bestByHitAt5?.comparison,
8972
9012
  void 0,
8973
9013
  aggregate
8974
9014
  );
8975
- writeText(path9.join(outputDir, "summary.md"), md);
8976
- writeJson(path9.join(outputDir, "summary.json"), bestByHitAt5?.summary ?? runs[0].summary);
9015
+ writeText(path12.join(outputDir, "summary.md"), md);
9016
+ writeJson(path12.join(outputDir, "summary.json"), bestByHitAt5?.summary ?? runs[0].summary);
8977
9017
  return { outputDir, aggregate };
8978
9018
  }
8979
9019
 
8980
9020
  // src/eval/cli.ts
9021
+ import * as path14 from "path";
9022
+
9023
+ // src/eval/cli-parser.ts
9024
+ import * as path13 from "path";
8981
9025
  function printUsage() {
8982
9026
  console.log(`
8983
9027
  Usage:
@@ -9049,12 +9093,12 @@ function parseEvalArgs(argv, cwd) {
9049
9093
  const arg = argv[i];
9050
9094
  const next = argv[i + 1];
9051
9095
  if (arg === "--project" && next) {
9052
- parsed.projectRoot = path10.resolve(cwd, next);
9096
+ parsed.projectRoot = path13.resolve(cwd, next);
9053
9097
  i += 1;
9054
9098
  continue;
9055
9099
  }
9056
9100
  if (arg === "--config" && next) {
9057
- parsed.configPath = path10.resolve(cwd, next);
9101
+ parsed.configPath = path13.resolve(cwd, next);
9058
9102
  i += 1;
9059
9103
  continue;
9060
9104
  }
@@ -9173,6 +9217,8 @@ function toRunOptions(parsed) {
9173
9217
  }
9174
9218
  };
9175
9219
  }
9220
+
9221
+ // src/eval/cli.ts
9176
9222
  async function handleEvalCommand(args, cwd) {
9177
9223
  const subcommand = args[0];
9178
9224
  if (!subcommand || subcommand === "--help" || subcommand === "-h") {
@@ -9210,72 +9256,326 @@ async function handleEvalCommand(args, cwd) {
9210
9256
  }
9211
9257
  return 0;
9212
9258
  }
9213
- if (subcommand === "compare") {
9214
- const { parsed, explicitAgainst } = parseEvalSubcommandOptions(args.slice(1), cwd);
9215
- if (!explicitAgainst) {
9216
- throw new Error("eval compare requires --against <baseline summary.json>");
9217
- }
9218
- parsed.againstPath = explicitAgainst;
9219
- const runOptions = toRunOptions(parsed);
9220
- if (hasSweepOptions(parsed.sweep)) {
9221
- const sweep = await runSweep(runOptions, parsed.sweep);
9222
- console.log(`Eval compare sweep complete. Artifacts: ${sweep.outputDir}`);
9223
- if (parsed.ciMode && sweep.aggregate.gatePassed === false) {
9224
- console.error(
9225
- `[CI-GATE] Sweep failed: ${sweep.aggregate.failedGateRuns ?? 0} run(s) violated budget/baseline gates`
9226
- );
9227
- return 1;
9228
- }
9229
- return 0;
9230
- }
9231
- const result = await runEvaluation(runOptions);
9232
- console.log(`Eval compare complete. Artifacts: ${result.outputDir}`);
9233
- return 0;
9259
+ if (subcommand === "compare") {
9260
+ const { parsed, explicitAgainst } = parseEvalSubcommandOptions(args.slice(1), cwd);
9261
+ if (!explicitAgainst) {
9262
+ throw new Error("eval compare requires --against <baseline summary.json>");
9263
+ }
9264
+ parsed.againstPath = explicitAgainst;
9265
+ const runOptions = toRunOptions(parsed);
9266
+ if (hasSweepOptions(parsed.sweep)) {
9267
+ const sweep = await runSweep(runOptions, parsed.sweep);
9268
+ console.log(`Eval compare sweep complete. Artifacts: ${sweep.outputDir}`);
9269
+ if (parsed.ciMode && sweep.aggregate.gatePassed === false) {
9270
+ console.error(
9271
+ `[CI-GATE] Sweep failed: ${sweep.aggregate.failedGateRuns ?? 0} run(s) violated budget/baseline gates`
9272
+ );
9273
+ return 1;
9274
+ }
9275
+ return 0;
9276
+ }
9277
+ const result = await runEvaluation(runOptions);
9278
+ console.log(`Eval compare complete. Artifacts: ${result.outputDir}`);
9279
+ return 0;
9280
+ }
9281
+ if (subcommand === "diff") {
9282
+ const { parsed, explicitAgainst } = parseEvalSubcommandOptions(args.slice(1), cwd);
9283
+ if (!explicitAgainst) {
9284
+ throw new Error("eval diff requires --against <baseline summary.json>");
9285
+ }
9286
+ if (!parsed.currentPath) {
9287
+ throw new Error("eval diff requires --current <current summary.json>");
9288
+ }
9289
+ parsed.againstPath = explicitAgainst;
9290
+ const currentPath = parsed.currentPath;
9291
+ if (!currentPath.endsWith(".json")) {
9292
+ throw new Error("eval diff --current must point to a summary JSON file");
9293
+ }
9294
+ if (!parsed.againstPath.endsWith(".json")) {
9295
+ throw new Error("eval diff --against must point to a summary JSON file");
9296
+ }
9297
+ const currentSummary = loadSummary(path14.resolve(parsed.projectRoot, currentPath), {
9298
+ allowLegacyDiversityMetrics: true
9299
+ });
9300
+ const baselineSummary = loadSummary(path14.resolve(parsed.projectRoot, parsed.againstPath), {
9301
+ allowLegacyDiversityMetrics: true
9302
+ });
9303
+ const comparison = compareSummaries(
9304
+ currentSummary,
9305
+ baselineSummary,
9306
+ path14.resolve(parsed.projectRoot, parsed.againstPath)
9307
+ );
9308
+ const outputDir = createRunDirectory(path14.resolve(parsed.projectRoot, parsed.outputRoot));
9309
+ const summaryMd = createSummaryMarkdown(currentSummary, comparison);
9310
+ writeJson(path14.join(outputDir, "compare.json"), comparison);
9311
+ writeText(path14.join(outputDir, "summary.md"), summaryMd);
9312
+ writeJson(path14.join(outputDir, "summary.json"), currentSummary);
9313
+ console.log(`Eval diff complete. Artifacts: ${outputDir}`);
9314
+ return 0;
9315
+ }
9316
+ throw new Error(`Unknown eval subcommand: ${subcommand}`);
9317
+ }
9318
+
9319
+ // src/mcp-server.ts
9320
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
9321
+ import * as path16 from "path";
9322
+ import { existsSync as existsSync10 } from "fs";
9323
+
9324
+ // src/mcp-server/register-prompts.ts
9325
+ import { z } from "zod";
9326
+ function registerMcpPrompts(server) {
9327
+ server.prompt(
9328
+ "search",
9329
+ "Search codebase by meaning using semantic search",
9330
+ { query: z.string().describe("What to search for in the codebase") },
9331
+ (args) => ({
9332
+ messages: [{
9333
+ role: "user",
9334
+ content: {
9335
+ type: "text",
9336
+ text: `Search the codebase for: "${args.query}"
9337
+
9338
+ Use the codebase_search tool with this query. If you need just locations first, use codebase_peek instead to save tokens.`
9339
+ }
9340
+ }]
9341
+ })
9342
+ );
9343
+ server.prompt(
9344
+ "find",
9345
+ "Find code using hybrid approach (semantic + grep)",
9346
+ { query: z.string().describe("What to find in the codebase") },
9347
+ (args) => ({
9348
+ messages: [{
9349
+ role: "user",
9350
+ content: {
9351
+ type: "text",
9352
+ text: `Find code related to: "${args.query}"
9353
+
9354
+ Use a hybrid approach:
9355
+ 1. First use codebase_peek to find semantic matches by meaning
9356
+ 2. Then use grep for exact identifier matches
9357
+ 3. Combine results for comprehensive coverage`
9358
+ }
9359
+ }]
9360
+ })
9361
+ );
9362
+ server.prompt(
9363
+ "index",
9364
+ "Index the codebase for semantic search",
9365
+ { options: z.string().optional().describe("Options: 'force' to rebuild, 'estimate' to check costs") },
9366
+ (args) => {
9367
+ const opts = args.options?.toLowerCase() ?? "";
9368
+ let instruction = "Use the index_codebase tool to index the codebase for semantic search.";
9369
+ if (opts.includes("force")) {
9370
+ instruction = "Use the index_codebase tool with force=true to rebuild the entire index from scratch.";
9371
+ } else if (opts.includes("estimate")) {
9372
+ instruction = "Use the index_codebase tool with estimateOnly=true to check the cost estimate before indexing.";
9373
+ }
9374
+ return {
9375
+ messages: [{
9376
+ role: "user",
9377
+ content: { type: "text", text: instruction }
9378
+ }]
9379
+ };
9380
+ }
9381
+ );
9382
+ server.prompt(
9383
+ "status",
9384
+ "Check if the codebase is indexed and ready",
9385
+ {},
9386
+ () => ({
9387
+ messages: [{
9388
+ role: "user",
9389
+ content: {
9390
+ type: "text",
9391
+ text: "Use the index_status tool to check if the codebase index is ready and show its current state."
9392
+ }
9393
+ }]
9394
+ })
9395
+ );
9396
+ server.prompt(
9397
+ "definition",
9398
+ "Find where a symbol is defined in the codebase",
9399
+ { query: z.string().describe("Symbol name or description to find the definition of") },
9400
+ (args) => ({
9401
+ messages: [{
9402
+ role: "user",
9403
+ content: {
9404
+ type: "text",
9405
+ text: `Find the definition of: "${args.query}"
9406
+
9407
+ Use the implementation_lookup tool to find where this symbol is defined. This prioritizes real implementation files over tests, docs, and examples. If no definition is found, fall back to codebase_search for broader discovery.`
9408
+ }
9409
+ }]
9410
+ })
9411
+ );
9412
+ }
9413
+
9414
+ // src/mcp-server/register-tools.ts
9415
+ import { z as z2 } from "zod";
9416
+
9417
+ // src/config/merger.ts
9418
+ import { existsSync as existsSync9, mkdirSync as mkdirSync4, readFileSync as readFileSync9, writeFileSync as writeFileSync4 } from "fs";
9419
+ import * as os5 from "os";
9420
+ import * as path15 from "path";
9421
+ var PROJECT_OVERRIDE_KEYS = [
9422
+ "embeddingProvider",
9423
+ "customProvider",
9424
+ "embeddingModel",
9425
+ "reranker",
9426
+ "include",
9427
+ "exclude",
9428
+ "indexing",
9429
+ "search",
9430
+ "debug",
9431
+ "scope"
9432
+ ];
9433
+ var MERGE_ARRAY_KEYS = ["knowledgeBases", "additionalInclude"];
9434
+ function isRecord3(value) {
9435
+ return typeof value === "object" && value !== null && !Array.isArray(value);
9436
+ }
9437
+ function isStringArray4(value) {
9438
+ return Array.isArray(value) && value.every((item) => typeof item === "string");
9439
+ }
9440
+ function applyProjectOverride(merged, normalizedProjectConfig, globalConfig, key) {
9441
+ if (key in normalizedProjectConfig) {
9442
+ merged[key] = normalizedProjectConfig[key];
9443
+ return;
9444
+ }
9445
+ if (key in globalConfig) {
9446
+ merged[key] = globalConfig[key];
9234
9447
  }
9235
- if (subcommand === "diff") {
9236
- const { parsed, explicitAgainst } = parseEvalSubcommandOptions(args.slice(1), cwd);
9237
- if (!explicitAgainst) {
9238
- throw new Error("eval diff requires --against <baseline summary.json>");
9448
+ }
9449
+ function mergeUniqueStringArray(values) {
9450
+ return [...new Set(values.map((value) => String(value).trim()))];
9451
+ }
9452
+ function normalizeKnowledgeBasePath(value) {
9453
+ let normalized = path15.normalize(String(value).trim());
9454
+ const root = path15.parse(normalized).root;
9455
+ while (normalized.length > root.length && /[\\/]$/.test(normalized)) {
9456
+ normalized = normalized.slice(0, -1);
9457
+ }
9458
+ return normalized;
9459
+ }
9460
+ function mergeKnowledgeBasePaths(values) {
9461
+ return [...new Set(values.map((value) => normalizeKnowledgeBasePath(value)).filter((value) => value.length > 0))];
9462
+ }
9463
+ function validateConfigLayerShape(rawConfig, filePath) {
9464
+ if (!isRecord3(rawConfig)) {
9465
+ throw new Error(`Config file ${filePath} must contain a JSON object at the root.`);
9466
+ }
9467
+ if (rawConfig.knowledgeBases !== void 0 && !isStringArray4(rawConfig.knowledgeBases)) {
9468
+ throw new Error(`Config file ${filePath} field 'knowledgeBases' must be an array of strings.`);
9469
+ }
9470
+ if (rawConfig.additionalInclude !== void 0 && !isStringArray4(rawConfig.additionalInclude)) {
9471
+ throw new Error(`Config file ${filePath} field 'additionalInclude' must be an array of strings.`);
9472
+ }
9473
+ if (rawConfig.include !== void 0 && !isStringArray4(rawConfig.include)) {
9474
+ throw new Error(`Config file ${filePath} field 'include' must be an array of strings.`);
9475
+ }
9476
+ if (rawConfig.exclude !== void 0 && !isStringArray4(rawConfig.exclude)) {
9477
+ throw new Error(`Config file ${filePath} field 'exclude' must be an array of strings.`);
9478
+ }
9479
+ for (const section of ["customProvider", "indexing", "search", "debug", "reranker"]) {
9480
+ const value = rawConfig[section];
9481
+ if (value !== void 0 && !isRecord3(value)) {
9482
+ throw new Error(`Config file ${filePath} field '${section}' must be an object.`);
9239
9483
  }
9240
- if (!parsed.currentPath) {
9241
- throw new Error("eval diff requires --current <current summary.json>");
9484
+ }
9485
+ return rawConfig;
9486
+ }
9487
+ function loadJsonFile(filePath) {
9488
+ if (!existsSync9(filePath)) {
9489
+ return null;
9490
+ }
9491
+ try {
9492
+ const content = readFileSync9(filePath, "utf-8");
9493
+ return validateConfigLayerShape(JSON.parse(content), filePath);
9494
+ } catch (error) {
9495
+ if (error instanceof Error && error.message.startsWith("Config file ")) {
9496
+ throw error;
9242
9497
  }
9243
- parsed.againstPath = explicitAgainst;
9244
- const currentPath = parsed.currentPath;
9245
- if (!currentPath.endsWith(".json")) {
9246
- throw new Error("eval diff --current must point to a summary JSON file");
9498
+ const message = error instanceof Error ? error.message : String(error);
9499
+ throw new Error(`Failed to load config file ${filePath}: ${message}`);
9500
+ }
9501
+ return null;
9502
+ }
9503
+ function materializeLocalProjectConfig(projectRoot, config) {
9504
+ const localConfigPath = path15.join(projectRoot, ".opencode", "codebase-index.json");
9505
+ mkdirSync4(path15.dirname(localConfigPath), { recursive: true });
9506
+ writeFileSync4(localConfigPath, JSON.stringify(config, null, 2), "utf-8");
9507
+ return localConfigPath;
9508
+ }
9509
+ function loadProjectConfigLayer(projectRoot) {
9510
+ const projectConfigPath = resolveProjectConfigPath(projectRoot);
9511
+ const projectConfig = loadJsonFile(projectConfigPath);
9512
+ if (!projectConfig) {
9513
+ return {};
9514
+ }
9515
+ const normalizedConfig = { ...projectConfig };
9516
+ const projectConfigBaseDir = path15.dirname(path15.dirname(projectConfigPath));
9517
+ if (Array.isArray(normalizedConfig.knowledgeBases)) {
9518
+ normalizedConfig.knowledgeBases = resolveInheritedKnowledgeBaseEntries(
9519
+ normalizedConfig.knowledgeBases,
9520
+ projectConfigBaseDir,
9521
+ projectRoot
9522
+ );
9523
+ }
9524
+ return normalizedConfig;
9525
+ }
9526
+ function loadMergedConfig(projectRoot) {
9527
+ const globalConfigPath = os5.homedir() + "/.config/opencode/codebase-index.json";
9528
+ const projectConfigPath = resolveProjectConfigPath(projectRoot);
9529
+ let globalConfig = null;
9530
+ let globalConfigError = null;
9531
+ try {
9532
+ globalConfig = loadJsonFile(globalConfigPath);
9533
+ } catch (error) {
9534
+ globalConfigError = error instanceof Error ? error : new Error(String(error));
9535
+ }
9536
+ const projectConfig = loadJsonFile(projectConfigPath);
9537
+ const normalizedProjectConfig = loadProjectConfigLayer(projectRoot);
9538
+ if (globalConfigError) {
9539
+ if (!projectConfig) {
9540
+ throw globalConfigError;
9247
9541
  }
9248
- if (!parsed.againstPath.endsWith(".json")) {
9249
- throw new Error("eval diff --against must point to a summary JSON file");
9542
+ globalConfig = null;
9543
+ }
9544
+ if (!globalConfig && !projectConfig) {
9545
+ return {};
9546
+ }
9547
+ if (!projectConfig && globalConfig) {
9548
+ return globalConfig;
9549
+ }
9550
+ if (!globalConfig && projectConfig) {
9551
+ return normalizedProjectConfig;
9552
+ }
9553
+ if (!globalConfig || !projectConfig) {
9554
+ return globalConfig ?? normalizedProjectConfig;
9555
+ }
9556
+ const merged = { ...globalConfig };
9557
+ for (const key of PROJECT_OVERRIDE_KEYS) {
9558
+ applyProjectOverride(merged, normalizedProjectConfig, globalConfig, key);
9559
+ }
9560
+ if (projectConfig) {
9561
+ for (const key of Object.keys(projectConfig)) {
9562
+ if (PROJECT_OVERRIDE_KEYS.includes(key) || MERGE_ARRAY_KEYS.includes(key)) {
9563
+ continue;
9564
+ }
9565
+ merged[key] = normalizedProjectConfig[key];
9250
9566
  }
9251
- const currentSummary = loadSummary(path10.resolve(parsed.projectRoot, currentPath), {
9252
- allowLegacyDiversityMetrics: true
9253
- });
9254
- const baselineSummary = loadSummary(path10.resolve(parsed.projectRoot, parsed.againstPath), {
9255
- allowLegacyDiversityMetrics: true
9256
- });
9257
- const comparison = compareSummaries(
9258
- currentSummary,
9259
- baselineSummary,
9260
- path10.resolve(parsed.projectRoot, parsed.againstPath)
9261
- );
9262
- const outputDir = createRunDirectory(path10.resolve(parsed.projectRoot, parsed.outputRoot));
9263
- const summaryMd = createSummaryMarkdown(currentSummary, comparison);
9264
- writeJson(path10.join(outputDir, "compare.json"), comparison);
9265
- writeText(path10.join(outputDir, "summary.md"), summaryMd);
9266
- writeJson(path10.join(outputDir, "summary.json"), currentSummary);
9267
- console.log(`Eval diff complete. Artifacts: ${outputDir}`);
9268
- return 0;
9269
9567
  }
9270
- throw new Error(`Unknown eval subcommand: ${subcommand}`);
9568
+ const globalKbs = globalConfig && Array.isArray(globalConfig.knowledgeBases) ? globalConfig.knowledgeBases : [];
9569
+ const projectKbs = projectConfig ? Array.isArray(normalizedProjectConfig.knowledgeBases) ? normalizedProjectConfig.knowledgeBases : [] : [];
9570
+ const allKbs = [...globalKbs, ...projectKbs];
9571
+ merged.knowledgeBases = mergeKnowledgeBasePaths(allKbs);
9572
+ const globalAdditional = globalConfig && Array.isArray(globalConfig.additionalInclude) ? globalConfig.additionalInclude : [];
9573
+ const projectAdditional = projectConfig && Array.isArray(projectConfig.additionalInclude) ? projectConfig.additionalInclude : [];
9574
+ const allAdditional = [...globalAdditional, ...projectAdditional];
9575
+ merged.additionalInclude = mergeUniqueStringArray(allAdditional);
9576
+ return merged;
9271
9577
  }
9272
9578
 
9273
- // src/mcp-server.ts
9274
- import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
9275
- import { z } from "zod";
9276
- import * as path11 from "path";
9277
- import { existsSync as existsSync8 } from "fs";
9278
-
9279
9579
  // src/tools/utils.ts
9280
9580
  var MAX_CONTENT_LINES = 30;
9281
9581
  function truncateContent(content) {
@@ -9415,12 +9715,15 @@ function formatHealthCheck(result) {
9415
9715
  }
9416
9716
  return lines.join("\n");
9417
9717
  }
9718
+ function formatResultHeader(result, index) {
9719
+ return result.name ? `[${index + 1}] ${result.chunkType} "${result.name}" in ${result.filePath}:${result.startLine}-${result.endLine}` : `[${index + 1}] ${result.chunkType} in ${result.filePath}:${result.startLine}-${result.endLine}`;
9720
+ }
9418
9721
  function formatDefinitionLookup(results, query) {
9419
9722
  if (results.length === 0) {
9420
9723
  return `No definition found for "${query}". Try codebase_search for broader discovery, or verify the symbol name.`;
9421
9724
  }
9422
9725
  const formatted = results.map((r, idx) => {
9423
- const header = r.name ? `[${idx + 1}] ${r.chunkType} "${r.name}" in ${r.filePath}:${r.startLine}-${r.endLine}` : `[${idx + 1}] ${r.chunkType} in ${r.filePath}:${r.startLine}-${r.endLine}`;
9726
+ const header = formatResultHeader(r, idx);
9424
9727
  return `${header} (score: ${r.score.toFixed(2)})
9425
9728
  \`\`\`
9426
9729
  ${truncateContent(r.content)}
@@ -9429,7 +9732,7 @@ ${truncateContent(r.content)}
9429
9732
  return formatted.join("\n\n");
9430
9733
  }
9431
9734
 
9432
- // src/mcp-server.ts
9735
+ // src/mcp-server/shared.ts
9433
9736
  var MAX_CONTENT_LINES2 = 30;
9434
9737
  function truncateContent2(content) {
9435
9738
  const lines = content.split("\n");
@@ -9450,50 +9753,23 @@ var CHUNK_TYPE_ENUM = [
9450
9753
  "module",
9451
9754
  "other"
9452
9755
  ];
9453
- function createMcpServer(projectRoot, config) {
9454
- const server = new McpServer({
9455
- name: "opencode-codebase-index",
9456
- version: "0.5.1"
9457
- });
9458
- const runtimeConfig = config;
9459
- let indexer = new Indexer(projectRoot, runtimeConfig);
9460
- let initialized = false;
9461
- function refreshIndexerFromConfig() {
9462
- indexer = new Indexer(projectRoot, runtimeConfig);
9463
- initialized = false;
9464
- }
9465
- function shouldForceLocalizeProjectIndex() {
9466
- if (runtimeConfig.scope !== "project") {
9467
- return false;
9468
- }
9469
- const localIndexPath = path11.join(projectRoot, ".opencode", "index");
9470
- const mainRepoRoot = resolveWorktreeMainRepoRoot(projectRoot);
9471
- if (!mainRepoRoot) {
9472
- return false;
9473
- }
9474
- const inheritedIndexPath = path11.join(mainRepoRoot, ".opencode", "index");
9475
- return !existsSync8(localIndexPath) && existsSync8(inheritedIndexPath);
9476
- }
9477
- async function ensureInitialized() {
9478
- if (!initialized) {
9479
- await indexer.initialize();
9480
- initialized = true;
9481
- }
9482
- }
9756
+
9757
+ // src/mcp-server/register-tools.ts
9758
+ function registerMcpTools(server, runtime) {
9483
9759
  server.tool(
9484
9760
  "codebase_search",
9485
9761
  "Search codebase by MEANING, not keywords. Returns full code content. For just finding WHERE code is (saves ~90% tokens), use codebase_peek instead.",
9486
9762
  {
9487
- query: z.string().describe("Natural language description of what code you're looking for. Describe behavior, not syntax."),
9488
- limit: z.number().optional().default(5).describe("Maximum number of results to return"),
9489
- fileType: z.string().optional().describe("Filter by file extension (e.g., 'ts', 'py', 'rs')"),
9490
- directory: z.string().optional().describe("Filter by directory path (e.g., 'src/utils', 'lib')"),
9491
- chunkType: z.enum(CHUNK_TYPE_ENUM).optional().describe("Filter by code chunk type"),
9492
- contextLines: z.number().optional().describe("Number of extra lines to include before/after each match (default: 0)")
9763
+ query: z2.string().describe("Natural language description of what code you're looking for. Describe behavior, not syntax."),
9764
+ limit: z2.number().optional().default(5).describe("Maximum number of results to return"),
9765
+ fileType: z2.string().optional().describe("Filter by file extension (e.g., 'ts', 'py', 'rs')"),
9766
+ directory: z2.string().optional().describe("Filter by directory path (e.g., 'src/utils', 'lib')"),
9767
+ chunkType: z2.enum(CHUNK_TYPE_ENUM).optional().describe("Filter by code chunk type"),
9768
+ contextLines: z2.number().optional().describe("Number of extra lines to include before/after each match (default: 0)")
9493
9769
  },
9494
9770
  async (args) => {
9495
- await ensureInitialized();
9496
- const results = await indexer.search(args.query, args.limit ?? 5, {
9771
+ await runtime.ensureInitialized();
9772
+ const results = await runtime.getIndexer().search(args.query, args.limit ?? 5, {
9497
9773
  fileType: args.fileType,
9498
9774
  directory: args.directory,
9499
9775
  chunkType: args.chunkType,
@@ -9518,15 +9794,15 @@ ${formatted.join("\n\n")}` }] };
9518
9794
  "codebase_peek",
9519
9795
  "Quick lookup of code locations by meaning. Returns only metadata (file, line, name, type) WITHOUT code content. Saves ~90% tokens vs codebase_search.",
9520
9796
  {
9521
- query: z.string().describe("Natural language description of what code you're looking for."),
9522
- limit: z.number().optional().default(10).describe("Maximum number of results to return"),
9523
- fileType: z.string().optional().describe("Filter by file extension (e.g., 'ts', 'py', 'rs')"),
9524
- directory: z.string().optional().describe("Filter by directory path (e.g., 'src/utils', 'lib')"),
9525
- chunkType: z.enum(CHUNK_TYPE_ENUM).optional().describe("Filter by code chunk type")
9797
+ query: z2.string().describe("Natural language description of what code you're looking for."),
9798
+ limit: z2.number().optional().default(10).describe("Maximum number of results to return"),
9799
+ fileType: z2.string().optional().describe("Filter by file extension (e.g., 'ts', 'py', 'rs')"),
9800
+ directory: z2.string().optional().describe("Filter by directory path (e.g., 'src/utils', 'lib')"),
9801
+ chunkType: z2.enum(CHUNK_TYPE_ENUM).optional().describe("Filter by code chunk type")
9526
9802
  },
9527
9803
  async (args) => {
9528
- await ensureInitialized();
9529
- const results = await indexer.search(args.query, args.limit ?? 10, {
9804
+ await runtime.ensureInitialized();
9805
+ const results = await runtime.getIndexer().search(args.query, args.limit ?? 10, {
9530
9806
  fileType: args.fileType,
9531
9807
  directory: args.directory,
9532
9808
  chunkType: args.chunkType,
@@ -9551,29 +9827,29 @@ Use Read tool to examine specific files.` }] };
9551
9827
  "index_codebase",
9552
9828
  "Index the codebase for semantic search. Creates vector embeddings of code chunks. Incremental - only re-indexes changed files. Run before first codebase_search.",
9553
9829
  {
9554
- force: z.boolean().optional().default(false).describe("Force reindex even if already indexed"),
9555
- estimateOnly: z.boolean().optional().default(false).describe("Only show cost estimate without indexing"),
9556
- verbose: z.boolean().optional().default(false).describe("Show detailed info about skipped files and parsing failures")
9830
+ force: z2.boolean().optional().default(false).describe("Force reindex even if already indexed"),
9831
+ estimateOnly: z2.boolean().optional().default(false).describe("Only show cost estimate without indexing"),
9832
+ verbose: z2.boolean().optional().default(false).describe("Show detailed info about skipped files and parsing failures")
9557
9833
  },
9558
9834
  async (args) => {
9559
9835
  if (args.estimateOnly) {
9560
- await ensureInitialized();
9561
- const estimate = await indexer.estimateCost();
9836
+ await runtime.ensureInitialized();
9837
+ const estimate = await runtime.getIndexer().estimateCost();
9562
9838
  return { content: [{ type: "text", text: formatCostEstimate(estimate) }] };
9563
9839
  }
9564
9840
  if (args.force) {
9565
- if (shouldForceLocalizeProjectIndex()) {
9566
- materializeLocalProjectConfig(projectRoot, loadProjectConfigLayer(projectRoot));
9567
- refreshIndexerFromConfig();
9841
+ if (runtime.shouldForceLocalizeProjectIndex()) {
9842
+ materializeLocalProjectConfig(runtime.projectRoot, loadProjectConfigLayer(runtime.projectRoot));
9843
+ runtime.refreshIndexerFromConfig();
9568
9844
  }
9569
- await ensureInitialized();
9570
- await indexer.clearIndex();
9571
- refreshIndexerFromConfig();
9572
- await ensureInitialized();
9845
+ await runtime.ensureInitialized();
9846
+ await runtime.getIndexer().clearIndex();
9847
+ runtime.refreshIndexerFromConfig();
9848
+ await runtime.ensureInitialized();
9573
9849
  } else {
9574
- await ensureInitialized();
9850
+ await runtime.ensureInitialized();
9575
9851
  }
9576
- const stats = await indexer.index();
9852
+ const stats = await runtime.getIndexer().index();
9577
9853
  return { content: [{ type: "text", text: formatIndexStats(stats, args.verbose ?? false) }] };
9578
9854
  }
9579
9855
  );
@@ -9582,8 +9858,8 @@ Use Read tool to examine specific files.` }] };
9582
9858
  "Check the status of the codebase index. Shows whether the codebase is indexed, how many chunks are stored, and the embedding provider being used.",
9583
9859
  {},
9584
9860
  async () => {
9585
- await ensureInitialized();
9586
- const status = await indexer.getStatus();
9861
+ await runtime.ensureInitialized();
9862
+ const status = await runtime.getIndexer().getStatus();
9587
9863
  return { content: [{ type: "text", text: formatStatus(status) }] };
9588
9864
  }
9589
9865
  );
@@ -9592,8 +9868,8 @@ Use Read tool to examine specific files.` }] };
9592
9868
  "Check index health and remove stale entries from deleted files. Run this to clean up the index after files have been deleted.",
9593
9869
  {},
9594
9870
  async () => {
9595
- await ensureInitialized();
9596
- const result = await indexer.healthCheck();
9871
+ await runtime.ensureInitialized();
9872
+ const result = await runtime.getIndexer().healthCheck();
9597
9873
  return { content: [{ type: "text", text: formatHealthCheck(result) }] };
9598
9874
  }
9599
9875
  );
@@ -9602,8 +9878,8 @@ Use Read tool to examine specific files.` }] };
9602
9878
  "Get metrics and performance statistics for the codebase index. Requires debug.enabled=true and debug.metrics=true in config.",
9603
9879
  {},
9604
9880
  async () => {
9605
- await ensureInitialized();
9606
- const logger = indexer.getLogger();
9881
+ await runtime.ensureInitialized();
9882
+ const logger = runtime.getIndexer().getLogger();
9607
9883
  if (!logger.isEnabled()) {
9608
9884
  return { content: [{ type: "text", text: 'Debug mode is disabled. Enable it in your config:\n\n```json\n{\n "debug": {\n "enabled": true,\n "metrics": true\n }\n}\n```' }] };
9609
9885
  }
@@ -9617,13 +9893,13 @@ Use Read tool to examine specific files.` }] };
9617
9893
  "index_logs",
9618
9894
  "Get recent debug logs from the codebase indexer. Requires debug.enabled=true in config.",
9619
9895
  {
9620
- limit: z.number().optional().default(20).describe("Maximum number of log entries to return"),
9621
- category: z.enum(["search", "embedding", "cache", "gc", "branch", "general"]).optional().describe("Filter by log category"),
9622
- level: z.enum(["error", "warn", "info", "debug"]).optional().describe("Filter by minimum log level")
9896
+ limit: z2.number().optional().default(20).describe("Maximum number of log entries to return"),
9897
+ category: z2.enum(["search", "embedding", "cache", "gc", "branch", "general"]).optional().describe("Filter by log category"),
9898
+ level: z2.enum(["error", "warn", "info", "debug"]).optional().describe("Filter by minimum log level")
9623
9899
  },
9624
9900
  async (args) => {
9625
- await ensureInitialized();
9626
- const logger = indexer.getLogger();
9901
+ await runtime.ensureInitialized();
9902
+ const logger = runtime.getIndexer().getLogger();
9627
9903
  if (!logger.isEnabled()) {
9628
9904
  return { content: [{ type: "text", text: 'Debug mode is disabled. Enable it in your config:\n\n```json\n{\n "debug": {\n "enabled": true\n }\n}\n```' }] };
9629
9905
  }
@@ -9649,16 +9925,16 @@ Use Read tool to examine specific files.` }] };
9649
9925
  "find_similar",
9650
9926
  "Find code similar to a given snippet. Use for duplicate detection, pattern discovery, or refactoring prep.",
9651
9927
  {
9652
- code: z.string().describe("The code snippet to find similar code for"),
9653
- limit: z.number().optional().default(10).describe("Maximum number of results to return"),
9654
- fileType: z.string().optional().describe("Filter by file extension (e.g., 'ts', 'py', 'rs')"),
9655
- directory: z.string().optional().describe("Filter by directory path (e.g., 'src/utils', 'lib')"),
9656
- chunkType: z.enum(CHUNK_TYPE_ENUM).optional().describe("Filter by code chunk type"),
9657
- excludeFile: z.string().optional().describe("Exclude results from this file path")
9928
+ code: z2.string().describe("The code snippet to find similar code for"),
9929
+ limit: z2.number().optional().default(10).describe("Maximum number of results to return"),
9930
+ fileType: z2.string().optional().describe("Filter by file extension (e.g., 'ts', 'py', 'rs')"),
9931
+ directory: z2.string().optional().describe("Filter by directory path (e.g., 'src/utils', 'lib')"),
9932
+ chunkType: z2.enum(CHUNK_TYPE_ENUM).optional().describe("Filter by code chunk type"),
9933
+ excludeFile: z2.string().optional().describe("Exclude results from this file path")
9658
9934
  },
9659
9935
  async (args) => {
9660
- await ensureInitialized();
9661
- const results = await indexer.findSimilar(args.code, args.limit ?? 10, {
9936
+ await runtime.ensureInitialized();
9937
+ const results = await runtime.getIndexer().findSimilar(args.code, args.limit ?? 10, {
9662
9938
  fileType: args.fileType,
9663
9939
  directory: args.directory,
9664
9940
  chunkType: args.chunkType,
@@ -9683,14 +9959,14 @@ ${formatted.join("\n\n")}` }] };
9683
9959
  "implementation_lookup",
9684
9960
  "Jump to symbol definition. Find WHERE something is defined. Returns the authoritative source location(s). Prefers real implementation files over tests, docs, examples, and fixtures.",
9685
9961
  {
9686
- query: z.string().describe("Symbol name or natural language description (e.g., 'validateToken', 'where is the payment handler defined')"),
9687
- limit: z.number().optional().default(5).describe("Maximum number of results"),
9688
- fileType: z.string().optional().describe("Filter by file extension (e.g., 'ts', 'py')"),
9689
- directory: z.string().optional().describe("Filter by directory path (e.g., 'src/utils')")
9962
+ query: z2.string().describe("Symbol name or natural language description (e.g., 'validateToken', 'where is the payment handler defined')"),
9963
+ limit: z2.number().optional().default(5).describe("Maximum number of results"),
9964
+ fileType: z2.string().optional().describe("Filter by file extension (e.g., 'ts', 'py')"),
9965
+ directory: z2.string().optional().describe("Filter by directory path (e.g., 'src/utils')")
9690
9966
  },
9691
9967
  async (args) => {
9692
- await ensureInitialized();
9693
- const results = await indexer.search(args.query, args.limit ?? 5, {
9968
+ await runtime.ensureInitialized();
9969
+ const results = await runtime.getIndexer().search(args.query, args.limit ?? 5, {
9694
9970
  fileType: args.fileType,
9695
9971
  directory: args.directory,
9696
9972
  definitionIntent: true
@@ -9702,12 +9978,13 @@ ${formatted.join("\n\n")}` }] };
9702
9978
  "call_graph",
9703
9979
  "Query the call graph to find callers or callees of a function/method. Use to understand code flow and dependencies.",
9704
9980
  {
9705
- name: z.string().describe("Function or method name to query"),
9706
- direction: z.enum(["callers", "callees"]).default("callers").describe("Direction: 'callers' finds who calls this function, 'callees' finds what this function calls"),
9707
- symbolId: z.string().optional().describe("Symbol ID (required for 'callees' direction)")
9981
+ name: z2.string().describe("Function or method name to query"),
9982
+ direction: z2.enum(["callers", "callees"]).default("callers").describe("Direction: 'callers' finds who calls this function, 'callees' finds what this function calls"),
9983
+ symbolId: z2.string().optional().describe("Symbol ID (required for 'callees' direction)")
9708
9984
  },
9709
9985
  async (args) => {
9710
- await ensureInitialized();
9986
+ await runtime.ensureInitialized();
9987
+ const indexer = runtime.getIndexer();
9711
9988
  if (args.direction === "callees") {
9712
9989
  if (!args.symbolId) {
9713
9990
  return { content: [{ type: "text", text: "Error: 'symbolId' is required when direction is 'callees'." }] };
@@ -9735,91 +10012,46 @@ ${formatted2.join("\n")}` }] };
9735
10012
  ${formatted.join("\n")}` }] };
9736
10013
  }
9737
10014
  );
9738
- server.prompt(
9739
- "search",
9740
- "Search codebase by meaning using semantic search",
9741
- { query: z.string().describe("What to search for in the codebase") },
9742
- (args) => ({
9743
- messages: [{
9744
- role: "user",
9745
- content: {
9746
- type: "text",
9747
- text: `Search the codebase for: "${args.query}"
9748
-
9749
- Use the codebase_search tool with this query. If you need just locations first, use codebase_peek instead to save tokens.`
9750
- }
9751
- }]
9752
- })
9753
- );
9754
- server.prompt(
9755
- "find",
9756
- "Find code using hybrid approach (semantic + grep)",
9757
- { query: z.string().describe("What to find in the codebase") },
9758
- (args) => ({
9759
- messages: [{
9760
- role: "user",
9761
- content: {
9762
- type: "text",
9763
- text: `Find code related to: "${args.query}"
10015
+ }
9764
10016
 
9765
- Use a hybrid approach:
9766
- 1. First use codebase_peek to find semantic matches by meaning
9767
- 2. Then use grep for exact identifier matches
9768
- 3. Combine results for comprehensive coverage`
9769
- }
9770
- }]
9771
- })
9772
- );
9773
- server.prompt(
9774
- "index",
9775
- "Index the codebase for semantic search",
9776
- { options: z.string().optional().describe("Options: 'force' to rebuild, 'estimate' to check costs") },
9777
- (args) => {
9778
- const opts = args.options?.toLowerCase() ?? "";
9779
- let instruction = "Use the index_codebase tool to index the codebase for semantic search.";
9780
- if (opts.includes("force")) {
9781
- instruction = "Use the index_codebase tool with force=true to rebuild the entire index from scratch.";
9782
- } else if (opts.includes("estimate")) {
9783
- instruction = "Use the index_codebase tool with estimateOnly=true to check the cost estimate before indexing.";
9784
- }
9785
- return {
9786
- messages: [{
9787
- role: "user",
9788
- content: { type: "text", text: instruction }
9789
- }]
9790
- };
10017
+ // src/mcp-server.ts
10018
+ function createMcpServer(projectRoot, config) {
10019
+ const server = new McpServer({
10020
+ name: "opencode-codebase-index",
10021
+ version: "0.5.1"
10022
+ });
10023
+ let indexer = new Indexer(projectRoot, config);
10024
+ let initialized = false;
10025
+ function refreshIndexerFromConfig() {
10026
+ indexer = new Indexer(projectRoot, config);
10027
+ initialized = false;
10028
+ }
10029
+ function shouldForceLocalizeProjectIndex() {
10030
+ if (config.scope !== "project") {
10031
+ return false;
9791
10032
  }
9792
- );
9793
- server.prompt(
9794
- "status",
9795
- "Check if the codebase is indexed and ready",
9796
- {},
9797
- () => ({
9798
- messages: [{
9799
- role: "user",
9800
- content: {
9801
- type: "text",
9802
- text: "Use the index_status tool to check if the codebase index is ready and show its current state."
9803
- }
9804
- }]
9805
- })
9806
- );
9807
- server.prompt(
9808
- "definition",
9809
- "Find where a symbol is defined in the codebase",
9810
- { query: z.string().describe("Symbol name or description to find the definition of") },
9811
- (args) => ({
9812
- messages: [{
9813
- role: "user",
9814
- content: {
9815
- type: "text",
9816
- text: `Find the definition of: "${args.query}"
9817
-
9818
- Use the implementation_lookup tool to find where this symbol is defined. This prioritizes real implementation files over tests, docs, and examples. If no definition is found, fall back to codebase_search for broader discovery.`
9819
- }
9820
- }]
9821
- })
9822
- );
10033
+ const localIndexPath = path16.join(projectRoot, ".opencode", "index");
10034
+ const mainRepoRoot = resolveWorktreeMainRepoRoot(projectRoot);
10035
+ if (!mainRepoRoot) {
10036
+ return false;
10037
+ }
10038
+ const inheritedIndexPath = path16.join(mainRepoRoot, ".opencode", "index");
10039
+ return !existsSync10(localIndexPath) && existsSync10(inheritedIndexPath);
10040
+ }
10041
+ async function ensureInitialized() {
10042
+ if (!initialized) {
10043
+ await indexer.initialize();
10044
+ initialized = true;
10045
+ }
10046
+ }
10047
+ registerMcpTools(server, {
10048
+ projectRoot,
10049
+ ensureInitialized,
10050
+ getIndexer: () => indexer,
10051
+ refreshIndexerFromConfig,
10052
+ shouldForceLocalizeProjectIndex
10053
+ });
10054
+ registerMcpPrompts(server);
9823
10055
  return server;
9824
10056
  }
9825
10057
 
@@ -9829,9 +10061,9 @@ function parseArgs(argv) {
9829
10061
  let config;
9830
10062
  for (let i = 2; i < argv.length; i++) {
9831
10063
  if (argv[i] === "--project" && argv[i + 1]) {
9832
- project = path12.resolve(argv[++i]);
10064
+ project = path17.resolve(argv[++i]);
9833
10065
  } else if (argv[i] === "--config" && argv[i + 1]) {
9834
- config = path12.resolve(argv[++i]);
10066
+ config = path17.resolve(argv[++i]);
9835
10067
  }
9836
10068
  }
9837
10069
  return { project, config };