opencode-codebase-index 0.16.0 → 0.17.1

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.
@@ -4955,7 +4955,12 @@ function sameReclaimOwner(left, right) {
4955
4955
  }
4956
4956
  function publishJsonDirectory(finalPath, value) {
4957
4957
  const candidatePath = `${finalPath}.candidate.${process.pid}.${(0, import_crypto.randomUUID)()}`;
4958
- (0, import_fs7.mkdirSync)(candidatePath, { mode: 448 });
4958
+ try {
4959
+ (0, import_fs7.mkdirSync)(candidatePath, { mode: 448 });
4960
+ } catch (error) {
4961
+ if (getErrorCode(error) === "ENOENT") return false;
4962
+ throw error;
4963
+ }
4959
4964
  try {
4960
4965
  (0, import_fs7.writeFileSync)(path12.join(candidatePath, OWNER_FILE_NAME), JSON.stringify(value), {
4961
4966
  encoding: "utf-8",
@@ -4968,8 +4973,12 @@ function publishJsonDirectory(finalPath, value) {
4968
4973
  return true;
4969
4974
  } catch (error) {
4970
4975
  if ((0, import_fs7.existsSync)(finalPath)) return false;
4976
+ if (getErrorCode(error) === "ENOENT") return false;
4971
4977
  throw error;
4972
4978
  }
4979
+ } catch (error) {
4980
+ if (getErrorCode(error) === "ENOENT") return false;
4981
+ throw error;
4973
4982
  } finally {
4974
4983
  if ((0, import_fs7.existsSync)(candidatePath)) (0, import_fs7.rmSync)(candidatePath, { recursive: true, force: true });
4975
4984
  }
@@ -6538,6 +6547,27 @@ function mergeTieredResults(symbolLane, hybridLane, limit) {
6538
6547
  }
6539
6548
  return out;
6540
6549
  }
6550
+ function selectChunksWithFileCoverage(chunks, limit) {
6551
+ if (limit <= 0 || chunks.length === 0) {
6552
+ return [];
6553
+ }
6554
+ if (chunks.length <= limit) {
6555
+ return chunks;
6556
+ }
6557
+ if (limit === 1) {
6558
+ return [chunks[Math.floor((chunks.length - 1) / 2)]];
6559
+ }
6560
+ const selected = [];
6561
+ for (let index = 0; index < limit; index++) {
6562
+ const sourceIndex = Math.round(index * (chunks.length - 1) / (limit - 1));
6563
+ selected.push(chunks[sourceIndex]);
6564
+ }
6565
+ return selected;
6566
+ }
6567
+ function selectIndexableChunks(chunks, limit, semanticOnly) {
6568
+ const indexableChunks = semanticOnly ? chunks.filter((chunk) => chunk.chunkType !== "other") : chunks;
6569
+ return selectChunksWithFileCoverage(indexableChunks, limit);
6570
+ }
6541
6571
  function matchesSearchFilters(candidate, options, minScore) {
6542
6572
  if (candidate.score < minScore) return false;
6543
6573
  if (options?.fileType) {
@@ -8276,7 +8306,6 @@ var Indexer = class {
8276
8306
  const relativePath = path13.relative(this.projectRoot, parsed.path);
8277
8307
  stats.parseFailures.push(relativePath);
8278
8308
  }
8279
- let fileChunkCount = 0;
8280
8309
  let chunksToProcess = parsed.chunks;
8281
8310
  if (this.config.indexing.fallbackToTextOnMaxChunks && chunksToProcess.length > this.config.indexing.maxChunksPerFile) {
8282
8311
  const changedFile = changedFiles.find((f) => f.path === parsed.path);
@@ -8285,13 +8314,12 @@ var Indexer = class {
8285
8314
  chunksToProcess = textChunks;
8286
8315
  }
8287
8316
  }
8317
+ chunksToProcess = selectIndexableChunks(
8318
+ chunksToProcess,
8319
+ this.config.indexing.maxChunksPerFile,
8320
+ this.config.indexing.semanticOnly
8321
+ );
8288
8322
  for (const chunk of chunksToProcess) {
8289
- if (fileChunkCount >= this.config.indexing.maxChunksPerFile) {
8290
- break;
8291
- }
8292
- if (this.config.indexing.semanticOnly && chunk.chunkType === "other") {
8293
- continue;
8294
- }
8295
8323
  const id = generateChunkId(parsed.path, chunk);
8296
8324
  const contentHash = generateChunkHash(chunk);
8297
8325
  const existingContentHash = existingChunks.get(id);
@@ -8315,7 +8343,6 @@ var Indexer = class {
8315
8343
  blameSummary: blameMetadata.blameSummary
8316
8344
  });
8317
8345
  if (existingContentHash === contentHash) {
8318
- fileChunkCount++;
8319
8346
  continue;
8320
8347
  }
8321
8348
  const texts = createEmbeddingTexts(chunk, parsed.path, getSafeEmbeddingChunkTokenLimit(configuredProviderInfo)).map((text3) => ({
@@ -8340,7 +8367,6 @@ var Indexer = class {
8340
8367
  contentHash,
8341
8368
  metadata
8342
8369
  });
8343
- fileChunkCount++;
8344
8370
  }
8345
8371
  }
8346
8372
  const retryableFailedChunks = this.collectRetryableFailedChunks(
@@ -9931,6 +9957,159 @@ function findKnowledgeBasePathIndex(knowledgeBases, inputPath, projectRoot3) {
9931
9957
  }
9932
9958
 
9933
9959
  // src/tools/utils.ts
9960
+ var import_tiktoken = require("tiktoken");
9961
+ var MIN_CONTEXT_PACK_TOKEN_BUDGET = 128;
9962
+ var MAX_CONTEXT_PACK_TOKEN_BUDGET = 4e3;
9963
+ var DEFAULT_CONTEXT_PACK_TOKEN_BUDGET = 1200;
9964
+ var CONTEXT_TOKENIZER = (0, import_tiktoken.get_encoding)("cl100k_base");
9965
+ function clampContextPackTokenBudget(tokenBudget) {
9966
+ if (tokenBudget === void 0 || !Number.isFinite(tokenBudget)) {
9967
+ return DEFAULT_CONTEXT_PACK_TOKEN_BUDGET;
9968
+ }
9969
+ return Math.min(
9970
+ MAX_CONTEXT_PACK_TOKEN_BUDGET,
9971
+ Math.max(MIN_CONTEXT_PACK_TOKEN_BUDGET, Math.floor(tokenBudget))
9972
+ );
9973
+ }
9974
+ function countContextTokens(text3) {
9975
+ return CONTEXT_TOKENIZER.encode(text3).length;
9976
+ }
9977
+ function fitTextToContextBudget(text3, tokenBudget) {
9978
+ const normalizedBudget = clampContextPackTokenBudget(tokenBudget);
9979
+ const tokenEstimate = countContextTokens(text3);
9980
+ if (tokenEstimate <= normalizedBudget) {
9981
+ return {
9982
+ text: text3,
9983
+ tokenBudget: normalizedBudget,
9984
+ tokenEstimate,
9985
+ truncated: false
9986
+ };
9987
+ }
9988
+ const suffix = "\n...[truncated to context token budget]";
9989
+ const codePoints = Array.from(text3);
9990
+ let low = 0;
9991
+ let high = codePoints.length;
9992
+ while (low < high) {
9993
+ const middle = Math.ceil((low + high) / 2);
9994
+ const candidate = `${codePoints.slice(0, middle).join("").trimEnd()}${suffix}`;
9995
+ if (countContextTokens(candidate) <= normalizedBudget) low = middle;
9996
+ else high = middle - 1;
9997
+ }
9998
+ const fitted = `${codePoints.slice(0, low).join("").trimEnd()}${suffix}`;
9999
+ return {
10000
+ text: fitted,
10001
+ tokenBudget: normalizedBudget,
10002
+ tokenEstimate: countContextTokens(fitted),
10003
+ truncated: true
10004
+ };
10005
+ }
10006
+ function normalizedLineRange(result) {
10007
+ return result.startLine <= result.endLine ? { start: result.startLine, end: result.endLine } : { start: result.endLine, end: result.startLine };
10008
+ }
10009
+ function rankContextCandidates(results) {
10010
+ return results.map((result, originalIndex) => ({ result, originalIndex })).sort((left, right) => right.result.score - left.result.score || left.originalIndex - right.originalIndex);
10011
+ }
10012
+ function deduplicateContextCandidates(candidates) {
10013
+ const acceptedByFile = /* @__PURE__ */ new Map();
10014
+ const deduplicated = [];
10015
+ for (const { result } of candidates) {
10016
+ const range = normalizedLineRange(result);
10017
+ const accepted = acceptedByFile.get(result.filePath) ?? [];
10018
+ if (accepted.some((item) => item.start <= range.end && range.start <= item.end)) {
10019
+ continue;
10020
+ }
10021
+ accepted.push(range);
10022
+ acceptedByFile.set(result.filePath, accepted);
10023
+ deduplicated.push(result);
10024
+ }
10025
+ return deduplicated;
10026
+ }
10027
+ function diversifyContextCandidates(results) {
10028
+ const byFile = /* @__PURE__ */ new Map();
10029
+ for (const result of results) {
10030
+ const bucket = byFile.get(result.filePath) ?? [];
10031
+ bucket.push(result);
10032
+ byFile.set(result.filePath, bucket);
10033
+ }
10034
+ const files = [...byFile.keys()];
10035
+ const diversified = [];
10036
+ for (let depth = 0; diversified.length < results.length; depth += 1) {
10037
+ for (const file of files) {
10038
+ const result = byFile.get(file)?.[depth];
10039
+ if (result) diversified.push(result);
10040
+ }
10041
+ }
10042
+ return diversified;
10043
+ }
10044
+ function compactEvidenceValue(value, maxChars) {
10045
+ const characters = [...value];
10046
+ if (characters.length <= maxChars) return value;
10047
+ return `\u2026${characters.slice(-(maxChars - 1)).join("")}`;
10048
+ }
10049
+ function formatContextEvidence(result, index) {
10050
+ const symbol = result.name ? ` ${JSON.stringify(compactEvidenceValue(result.name, 80))}` : "";
10051
+ const path17 = compactEvidenceValue(result.filePath, 120);
10052
+ return `[${index}] ${result.chunkType}${symbol} in ${path17}:${result.startLine}-${result.endLine} (score ${result.score.toFixed(2)})`;
10053
+ }
10054
+ function formatContextPack(heading, selected, candidateCount, duplicateCount, limitOmittedCount, budgetOmittedCount) {
10055
+ const lines = selected.map((result, index) => formatContextEvidence(result, index + 1));
10056
+ const notes = [];
10057
+ if (duplicateCount > 0) notes.push(`${duplicateCount} overlapping duplicate${duplicateCount === 1 ? "" : "s"} removed`);
10058
+ if (limitOmittedCount > 0) notes.push(`${limitOmittedCount} additional result${limitOmittedCount === 1 ? "" : "s"} excluded by result limit`);
10059
+ if (budgetOmittedCount > 0) notes.push(`${budgetOmittedCount} additional result${budgetOmittedCount === 1 ? "" : "s"} omitted by token budget`);
10060
+ const footer = notes.length > 0 ? `Selected ${selected.length} of ${candidateCount} candidates; ${notes.join("; ")}.` : `Selected ${selected.length} of ${candidateCount} candidates.`;
10061
+ return `${heading}
10062
+
10063
+ ${lines.join("\n")}
10064
+
10065
+ ${footer}`;
10066
+ }
10067
+ function buildContextPack(results, options = {}) {
10068
+ const requestedTokenBudget = options.tokenBudget ?? DEFAULT_CONTEXT_PACK_TOKEN_BUDGET;
10069
+ const tokenBudget = clampContextPackTokenBudget(options.tokenBudget);
10070
+ const heading = compactEvidenceValue(options.heading?.trim() || "Codebase evidence", 160);
10071
+ const maxResults = Math.max(0, Math.floor(options.maxResults ?? results.length));
10072
+ const candidateCount = results.length;
10073
+ const deduplicated = deduplicateContextCandidates(rankContextCandidates(results));
10074
+ const diversified = diversifyContextCandidates(deduplicated);
10075
+ const duplicateCount = candidateCount - deduplicated.length;
10076
+ const selectable = diversified.slice(0, maxResults);
10077
+ const limitOmittedCount = deduplicated.length - selectable.length;
10078
+ let selected = [];
10079
+ let text3 = formatContextPack(heading, selected, candidateCount, duplicateCount, limitOmittedCount, selectable.length);
10080
+ for (let count = 1; count <= selectable.length; count += 1) {
10081
+ const candidateSelection = selectable.slice(0, count);
10082
+ const budgetOmittedCount2 = selectable.length - candidateSelection.length;
10083
+ const candidateText = formatContextPack(
10084
+ heading,
10085
+ candidateSelection,
10086
+ candidateCount,
10087
+ duplicateCount,
10088
+ limitOmittedCount,
10089
+ budgetOmittedCount2
10090
+ );
10091
+ if (countContextTokens(candidateText) > tokenBudget) break;
10092
+ selected = candidateSelection;
10093
+ text3 = candidateText;
10094
+ }
10095
+ const fitted = fitTextToContextBudget(text3, tokenBudget);
10096
+ const budgetOmittedCount = selectable.length - selected.length;
10097
+ const omittedCount = candidateCount - selected.length;
10098
+ return {
10099
+ requestedTokenBudget,
10100
+ tokenBudget,
10101
+ text: fitted.text,
10102
+ tokenEstimate: fitted.tokenEstimate,
10103
+ results: selected,
10104
+ candidateCount,
10105
+ deduplicatedCount: deduplicated.length,
10106
+ selectedCount: selected.length,
10107
+ omittedCount,
10108
+ duplicateCount,
10109
+ limitOmittedCount,
10110
+ budgetOmittedCount
10111
+ };
10112
+ }
9934
10113
  var MAX_CONTENT_LINES = 30;
9935
10114
  function truncateContent(content) {
9936
10115
  const lines = content.split("\n");
@@ -10075,17 +10254,6 @@ function calculatePercentage(progress) {
10075
10254
  if (progress.phase === "storing") return 95;
10076
10255
  return 0;
10077
10256
  }
10078
- function formatCodebasePeek(results) {
10079
- if (results.length === 0) {
10080
- return "No matching code found. Try a different query or run index_codebase first.";
10081
- }
10082
- const formatted = results.map((r, idx) => {
10083
- const location = `${r.filePath}:${r.startLine}-${r.endLine}`;
10084
- const name = r.name ? `"${r.name}"` : "(anonymous)";
10085
- return `[${idx + 1}] ${r.chunkType} ${name} at ${location} (score: ${r.score.toFixed(2)})${formatBlame(r)}`;
10086
- });
10087
- return formatted.join("\n");
10088
- }
10089
10257
  function formatHealthCheck(result) {
10090
10258
  if (result.resetCorruptedIndex) {
10091
10259
  return result.warning ?? "Detected a corrupted local index and reset it. Run index_codebase to rebuild search data.";
@@ -10599,6 +10767,253 @@ Run /index to rebuild the index without the removed knowledge base.`;
10599
10767
  return result;
10600
10768
  }
10601
10769
 
10770
+ // src/tools/symbol-inference.ts
10771
+ var IDENTIFIER_RE = /[A-Za-z_$][A-Za-z0-9_$]*/g;
10772
+ var QUOTED_BACKTICK_RE = /`([^`]+)`/g;
10773
+ var QUOTED_SINGLE_RE = /'([^'\\]+)'/g;
10774
+ var QUOTED_DOUBLE_RE = /"([^"]+)"/g;
10775
+ var SYMBOL_LIKE_RE = /^(?:[A-Za-z_$][A-Za-z0-9_$]*)$/;
10776
+ var CAMEL_CASE_RE = /^[a-z_][A-Za-z0-9_$]*[A-Z][A-Za-z0-9_$]*$/;
10777
+ var PASCAL_CASE_RE = /^[A-Z][A-Za-z0-9_$]*$/;
10778
+ var SNAKE_CASE_RE = /^[a-z][a-z0-9_]*_[a-z0-9_]+$/;
10779
+ var DEFINITION_INTENT_RE = /\b(where|defined|definition|define|declaration|symbol|function|method|class|interface|type)\b/i;
10780
+ var STOP_WORDS = /* @__PURE__ */ new Set([
10781
+ "a",
10782
+ "an",
10783
+ "and",
10784
+ "are",
10785
+ "at",
10786
+ "for",
10787
+ "find",
10788
+ "how",
10789
+ "i",
10790
+ "in",
10791
+ "is",
10792
+ "it",
10793
+ "of",
10794
+ "on",
10795
+ "that",
10796
+ "the",
10797
+ "definition",
10798
+ "show",
10799
+ "to",
10800
+ "where",
10801
+ "which",
10802
+ "what",
10803
+ "you",
10804
+ "your",
10805
+ "with"
10806
+ ]);
10807
+ function stripCallSuffix(token) {
10808
+ return token.replace(/\(\s*\)$/, "");
10809
+ }
10810
+ function isLikelySymbolName(token) {
10811
+ if (!SYMBOL_LIKE_RE.test(token)) {
10812
+ return false;
10813
+ }
10814
+ if (STOP_WORDS.has(token.toLowerCase())) {
10815
+ return false;
10816
+ }
10817
+ return CAMEL_CASE_RE.test(token) || PASCAL_CASE_RE.test(token) || SNAKE_CASE_RE.test(token);
10818
+ }
10819
+ function extractQuotedIdentifiers(query) {
10820
+ const identifiers = /* @__PURE__ */ new Set();
10821
+ for (const match of query.matchAll(QUOTED_BACKTICK_RE)) {
10822
+ const candidate = stripCallSuffix(match[1].trim());
10823
+ if (candidate && isLikelySymbolName(candidate)) {
10824
+ identifiers.add(candidate);
10825
+ }
10826
+ }
10827
+ for (const match of query.matchAll(QUOTED_SINGLE_RE)) {
10828
+ const candidate = stripCallSuffix(match[1].trim());
10829
+ if (candidate && isLikelySymbolName(candidate)) {
10830
+ identifiers.add(candidate);
10831
+ }
10832
+ }
10833
+ for (const match of query.matchAll(QUOTED_DOUBLE_RE)) {
10834
+ const candidate = stripCallSuffix(match[1].trim());
10835
+ if (candidate && isLikelySymbolName(candidate)) {
10836
+ identifiers.add(candidate);
10837
+ }
10838
+ }
10839
+ return [...identifiers];
10840
+ }
10841
+ function extractBareIdentifiers(query) {
10842
+ const unquoted = query.replace(QUOTED_BACKTICK_RE, " ").replace(QUOTED_SINGLE_RE, " ").replace(QUOTED_DOUBLE_RE, " ");
10843
+ const identifiers = /* @__PURE__ */ new Set();
10844
+ for (const match of unquoted.matchAll(IDENTIFIER_RE)) {
10845
+ const candidate = stripCallSuffix(match[0]);
10846
+ if (isLikelySymbolName(candidate)) {
10847
+ identifiers.add(candidate);
10848
+ }
10849
+ }
10850
+ return [...identifiers];
10851
+ }
10852
+ function isSingleMeaningfulToken(query, symbol) {
10853
+ const tokens = query.replace(/[`'"()]/g, " ").split(/[^A-Za-z0-9_$]+/).map((token) => token.trim().toLowerCase()).filter((token) => token.length > 0).filter((token) => !STOP_WORDS.has(token));
10854
+ return tokens.length === 1 && tokens[0] === symbol.toLowerCase();
10855
+ }
10856
+ function inferExactSymbolFromQuery(query) {
10857
+ const quoted = extractQuotedIdentifiers(query);
10858
+ if (quoted.length === 1) {
10859
+ return quoted[0];
10860
+ }
10861
+ if (quoted.length > 1) {
10862
+ return void 0;
10863
+ }
10864
+ const candidates = extractBareIdentifiers(query);
10865
+ if (candidates.length !== 1) {
10866
+ return void 0;
10867
+ }
10868
+ const candidate = candidates[0];
10869
+ if (DEFINITION_INTENT_RE.test(query)) {
10870
+ return candidate;
10871
+ }
10872
+ if (isSingleMeaningfulToken(query, candidate)) {
10873
+ return candidate;
10874
+ }
10875
+ return void 0;
10876
+ }
10877
+
10878
+ // src/tools/context.ts
10879
+ var MIN_CONTEXT_RESULT_LIMIT = 1;
10880
+ var MAX_CONTEXT_RESULT_LIMIT = 100;
10881
+ var MIN_CONTEXT_PATH_DEPTH = 1;
10882
+ var MAX_CONTEXT_PATH_DEPTH = 100;
10883
+ function locations(results) {
10884
+ return results.map((result) => ({
10885
+ filePath: result.filePath,
10886
+ startLine: result.startLine,
10887
+ endLine: result.endLine,
10888
+ score: result.score,
10889
+ chunkType: result.chunkType,
10890
+ name: result.name
10891
+ }));
10892
+ }
10893
+ function packedResult(route, routedQuery, pack) {
10894
+ return {
10895
+ text: pack.text,
10896
+ details: {
10897
+ route,
10898
+ routedQuery,
10899
+ tokenBudget: pack.tokenBudget,
10900
+ tokenEstimate: pack.tokenEstimate,
10901
+ candidateCount: pack.candidateCount,
10902
+ deduplicatedCount: pack.deduplicatedCount,
10903
+ selectedCount: pack.selectedCount,
10904
+ omittedCount: pack.omittedCount,
10905
+ duplicateCount: pack.duplicateCount,
10906
+ limitOmittedCount: pack.limitOmittedCount,
10907
+ budgetOmittedCount: pack.budgetOmittedCount,
10908
+ results: locations(pack.results)
10909
+ }
10910
+ };
10911
+ }
10912
+ async function resolveSearchContext(input, operations) {
10913
+ const explicitSymbol = input.symbol ?? void 0;
10914
+ const limit = input.limit ?? 10;
10915
+ const tokenBudget = input.tokenBudget ?? void 0;
10916
+ const lookupSymbol = explicitSymbol ?? inferExactSymbolFromQuery(input.query);
10917
+ if (lookupSymbol) {
10918
+ const definitions = await operations.lookup(lookupSymbol, MAX_CONTEXT_RESULT_LIMIT);
10919
+ if (definitions.length > 0) {
10920
+ return packedResult("definition", lookupSymbol, buildContextPack(definitions, {
10921
+ tokenBudget,
10922
+ maxResults: limit,
10923
+ heading: `Definition evidence for ${JSON.stringify(lookupSymbol)}`
10924
+ }));
10925
+ }
10926
+ if (explicitSymbol) {
10927
+ const fitted = fitTextToContextBudget(
10928
+ formatDefinitionLookup(definitions, lookupSymbol),
10929
+ tokenBudget
10930
+ );
10931
+ return { text: fitted.text };
10932
+ }
10933
+ }
10934
+ const results = await operations.search(input.query, MAX_CONTEXT_RESULT_LIMIT);
10935
+ if (results.length === 0) {
10936
+ const fitted = fitTextToContextBudget(
10937
+ "No matching code found. Try a different query or run index_codebase first.",
10938
+ tokenBudget
10939
+ );
10940
+ return { text: fitted.text };
10941
+ }
10942
+ return packedResult("conceptual", input.query, buildContextPack(results, {
10943
+ tokenBudget,
10944
+ maxResults: limit,
10945
+ heading: `Codebase evidence for ${JSON.stringify(input.query)}`
10946
+ }));
10947
+ }
10948
+ function fittedDetails(route, fitted) {
10949
+ return {
10950
+ route,
10951
+ tokenBudget: fitted.tokenBudget,
10952
+ tokenEstimate: fitted.tokenEstimate,
10953
+ truncated: fitted.truncated
10954
+ };
10955
+ }
10956
+ async function resolveCodebaseContext(projectRoot3, host, input) {
10957
+ const from = input.from ?? void 0;
10958
+ const to = input.to ?? void 0;
10959
+ const symbol = input.symbol ?? void 0;
10960
+ const limit = input.limit ?? 10;
10961
+ const maxDepth = input.maxDepth ?? 10;
10962
+ const fileType = input.fileType ?? void 0;
10963
+ const directory = input.directory ?? void 0;
10964
+ const tokenBudget = input.tokenBudget ?? void 0;
10965
+ if (from && to) {
10966
+ const path17 = await getCallGraphPath(projectRoot3, host, from, to, maxDepth);
10967
+ if (path17.length > 0) {
10968
+ const fitted2 = fitTextToContextBudget(
10969
+ formatCallGraphPath(from, to, path17),
10970
+ tokenBudget
10971
+ );
10972
+ return {
10973
+ text: fitted2.text,
10974
+ details: fittedDetails("path", fitted2)
10975
+ };
10976
+ }
10977
+ const { callers } = await getCallGraphData(projectRoot3, host, {
10978
+ name: to,
10979
+ direction: "callers"
10980
+ });
10981
+ const directEdge = callers.find((edge) => edge.fromSymbolName === from);
10982
+ if (directEdge) {
10983
+ const location = directEdge.fromSymbolFilePath ? ` at ${directEdge.fromSymbolFilePath}:${directEdge.line}` : "";
10984
+ const fitted2 = fitTextToContextBudget(
10985
+ `Direct path: ${from} --${directEdge.callType}--> ${to}${location} (edge is ${directEdge.isResolved ? "resolved" : "unresolved"}).`,
10986
+ tokenBudget
10987
+ );
10988
+ return {
10989
+ text: fitted2.text,
10990
+ details: fittedDetails("direct-edge", fitted2)
10991
+ };
10992
+ }
10993
+ const fitted = fitTextToContextBudget(
10994
+ formatCallGraphPath(from, to, path17),
10995
+ tokenBudget
10996
+ );
10997
+ return {
10998
+ text: fitted.text,
10999
+ details: fittedDetails("path", fitted)
11000
+ };
11001
+ }
11002
+ return resolveSearchContext({ query: input.query, symbol, limit, tokenBudget }, {
11003
+ lookup: (lookupSymbol, retrievalLimit) => implementationLookup(projectRoot3, host, lookupSymbol, {
11004
+ limit: retrievalLimit,
11005
+ fileType,
11006
+ directory
11007
+ }),
11008
+ search: (query, retrievalLimit) => searchCodebase(projectRoot3, host, query, {
11009
+ limit: retrievalLimit,
11010
+ fileType,
11011
+ directory,
11012
+ metadataOnly: true
11013
+ })
11014
+ });
11015
+ }
11016
+
10602
11017
  // src/pi-call-graph.ts
10603
11018
  var import_typebox = require("typebox");
10604
11019
  var HOST = "pi";
@@ -10675,58 +11090,33 @@ function codebaseIndexPiExtension(pi) {
10675
11090
  pi.registerTool({
10676
11091
  name: "codebase_context",
10677
11092
  label: "Codebase Context",
10678
- description: "PREFERRED FIRST TOOL for any repository question. Check index_status when freshness is unknown, then use this tool for low-token location discovery and dependency flow.",
11093
+ description: "PREFERRED FIRST TOOL for any repository question. Returns a deduplicated, file-diverse evidence pack within tokenBudget. Check index_status when freshness is unknown, then use this tool for low-token location discovery and dependency flow.",
10679
11094
  parameters: import_typebox2.Type.Object({
10680
11095
  query: import_typebox2.Type.String({ description: "Natural language description of what code you're trying to locate" }),
10681
- from: import_typebox2.Type.Optional(import_typebox2.Type.String({ description: "Source symbol when asking for a dependency path." })),
10682
- to: import_typebox2.Type.Optional(import_typebox2.Type.String({ description: "Target symbol when asking for a dependency path." })),
10683
- symbol: import_typebox2.Type.Optional(import_typebox2.Type.String({ description: "Exact symbol name for an authoritative definition lookup." })),
10684
- limit: import_typebox2.Type.Optional(import_typebox2.Type.Number({ description: "Maximum results (default: 10)" })),
10685
- maxDepth: import_typebox2.Type.Optional(import_typebox2.Type.Number({ description: "Maximum call-graph traversal depth for from/to paths" })),
10686
- fileType: import_typebox2.Type.Optional(import_typebox2.Type.String({ description: "Filter by file extension, e.g., ts, py, rs" })),
10687
- directory: import_typebox2.Type.Optional(import_typebox2.Type.String({ description: "Filter by directory path" }))
11096
+ from: import_typebox2.Type.Optional(import_typebox2.Type.Union([import_typebox2.Type.String(), import_typebox2.Type.Null()], { description: "Source symbol when asking for a dependency path." })),
11097
+ to: import_typebox2.Type.Optional(import_typebox2.Type.Union([import_typebox2.Type.String(), import_typebox2.Type.Null()], { description: "Target symbol when asking for a dependency path." })),
11098
+ symbol: import_typebox2.Type.Optional(import_typebox2.Type.Union([import_typebox2.Type.String(), import_typebox2.Type.Null()], { description: "Exact symbol name for an authoritative definition lookup." })),
11099
+ limit: import_typebox2.Type.Optional(import_typebox2.Type.Union([
11100
+ import_typebox2.Type.Integer({ minimum: MIN_CONTEXT_RESULT_LIMIT, maximum: MAX_CONTEXT_RESULT_LIMIT }),
11101
+ import_typebox2.Type.Null()
11102
+ ], { default: 10, description: `Maximum results (${MIN_CONTEXT_RESULT_LIMIT}-${MAX_CONTEXT_RESULT_LIMIT})` })),
11103
+ maxDepth: import_typebox2.Type.Optional(import_typebox2.Type.Union([
11104
+ import_typebox2.Type.Integer({ minimum: MIN_CONTEXT_PATH_DEPTH, maximum: MAX_CONTEXT_PATH_DEPTH }),
11105
+ import_typebox2.Type.Null()
11106
+ ], { default: 10, description: `Maximum call-graph traversal depth (${MIN_CONTEXT_PATH_DEPTH}-${MAX_CONTEXT_PATH_DEPTH})` })),
11107
+ fileType: import_typebox2.Type.Optional(import_typebox2.Type.Union([import_typebox2.Type.String(), import_typebox2.Type.Null()], { description: "Filter by file extension, e.g., ts, py, rs" })),
11108
+ directory: import_typebox2.Type.Optional(import_typebox2.Type.Union([import_typebox2.Type.String(), import_typebox2.Type.Null()], { description: "Filter by directory path" })),
11109
+ tokenBudget: import_typebox2.Type.Optional(import_typebox2.Type.Union([
11110
+ import_typebox2.Type.Integer({ minimum: MIN_CONTEXT_PACK_TOKEN_BUDGET, maximum: MAX_CONTEXT_PACK_TOKEN_BUDGET }),
11111
+ import_typebox2.Type.Null()
11112
+ ], {
11113
+ default: DEFAULT_CONTEXT_PACK_TOKEN_BUDGET,
11114
+ description: `Maximum response tokens (${MIN_CONTEXT_PACK_TOKEN_BUDGET}-${MAX_CONTEXT_PACK_TOKEN_BUDGET})`
11115
+ }))
10688
11116
  }),
10689
11117
  async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
10690
- const root = projectRoot2(ctx);
10691
- if (params.from && params.to) {
10692
- const path17 = await getCallGraphPath(root, HOST2, params.from, params.to, params.maxDepth);
10693
- if (path17.length > 0) {
10694
- return text2(formatCallGraphPath(params.from, params.to, path17), path17);
10695
- }
10696
- const { callers } = await getCallGraphData(root, HOST2, {
10697
- name: params.to,
10698
- direction: "callers"
10699
- });
10700
- const directEdge = callers.find((edge) => edge.fromSymbolName === params.from);
10701
- if (directEdge) {
10702
- const location = directEdge.fromSymbolFilePath ? ` at ${directEdge.fromSymbolFilePath}:${directEdge.line}` : "";
10703
- return text2(
10704
- `Direct path: ${params.from} --${directEdge.callType}--> ${params.to}${location} (edge is ${directEdge.isResolved ? "resolved" : "unresolved"}).`,
10705
- path17
10706
- );
10707
- }
10708
- return text2(formatCallGraphPath(params.from, params.to, path17), path17);
10709
- }
10710
- if (params.symbol) {
10711
- const results2 = await implementationLookup(root, HOST2, params.symbol, {
10712
- limit: params.limit ?? 10,
10713
- fileType: params.fileType,
10714
- directory: params.directory
10715
- });
10716
- return text2(formatDefinitionLookup(results2, params.symbol), results2);
10717
- }
10718
- const results = await searchCodebase(root, HOST2, params.query, {
10719
- limit: params.limit ?? 10,
10720
- fileType: params.fileType,
10721
- directory: params.directory,
10722
- metadataOnly: true
10723
- });
10724
- if (results.length === 0) {
10725
- return text2("No matching code found. Try a different query or run index_status/index_codebase first.");
10726
- }
10727
- return text2(`Found ${results.length} locations for "${params.query}":
10728
-
10729
- ${formatCodebasePeek(results)}`, results);
11118
+ const result = await resolveCodebaseContext(projectRoot2(ctx), HOST2, params);
11119
+ return text2(result.text, result.details);
10730
11120
  }
10731
11121
  });
10732
11122
  pi.registerTool({