@nxuss/lemma 1.9.0 → 1.10.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.
Files changed (74) hide show
  1. package/README.md +24 -4
  2. package/dist/cjs/mcp/index.js +14 -0
  3. package/dist/cjs/mcp/index.js.map +1 -1
  4. package/dist/cjs/mcp/tool-profiles.d.ts.map +1 -1
  5. package/dist/cjs/mcp/tool-profiles.js +3 -0
  6. package/dist/cjs/mcp/tool-profiles.js.map +1 -1
  7. package/dist/cjs/mcp/tools.d.ts.map +1 -1
  8. package/dist/cjs/mcp/tools.js +290 -138
  9. package/dist/cjs/mcp/tools.js.map +1 -1
  10. package/dist/cjs/subconscious/TheBrainV2.d.ts +27 -1
  11. package/dist/cjs/subconscious/TheBrainV2.d.ts.map +1 -1
  12. package/dist/cjs/subconscious/TheBrainV2.js +53 -1
  13. package/dist/cjs/subconscious/TheBrainV2.js.map +1 -1
  14. package/dist/cjs/utils/AffectedTests.d.ts +54 -0
  15. package/dist/cjs/utils/AffectedTests.d.ts.map +1 -0
  16. package/dist/cjs/utils/AffectedTests.js +195 -0
  17. package/dist/cjs/utils/AffectedTests.js.map +1 -0
  18. package/dist/cjs/utils/DepGraph.d.ts +28 -0
  19. package/dist/cjs/utils/DepGraph.d.ts.map +1 -0
  20. package/dist/cjs/utils/DepGraph.js +210 -0
  21. package/dist/cjs/utils/DepGraph.js.map +1 -0
  22. package/dist/cjs/utils/LocalPrefilter.d.ts +61 -0
  23. package/dist/cjs/utils/LocalPrefilter.d.ts.map +1 -0
  24. package/dist/cjs/utils/LocalPrefilter.js +169 -0
  25. package/dist/cjs/utils/LocalPrefilter.js.map +1 -0
  26. package/dist/cjs/utils/OutputDelta.d.ts +60 -0
  27. package/dist/cjs/utils/OutputDelta.d.ts.map +1 -0
  28. package/dist/cjs/utils/OutputDelta.js +226 -0
  29. package/dist/cjs/utils/OutputDelta.js.map +1 -0
  30. package/dist/cjs/utils/OutputLedger.d.ts +60 -0
  31. package/dist/cjs/utils/OutputLedger.d.ts.map +1 -0
  32. package/dist/cjs/utils/OutputLedger.js +116 -0
  33. package/dist/cjs/utils/OutputLedger.js.map +1 -0
  34. package/dist/cjs/utils/ReadWorkspaceCache.d.ts +8 -2
  35. package/dist/cjs/utils/ReadWorkspaceCache.d.ts.map +1 -1
  36. package/dist/cjs/utils/ReadWorkspaceCache.js +58 -3
  37. package/dist/cjs/utils/ReadWorkspaceCache.js.map +1 -1
  38. package/dist/esm/mcp/index.js +14 -0
  39. package/dist/esm/mcp/index.js.map +1 -1
  40. package/dist/esm/mcp/tool-profiles.d.ts.map +1 -1
  41. package/dist/esm/mcp/tool-profiles.js +3 -0
  42. package/dist/esm/mcp/tool-profiles.js.map +1 -1
  43. package/dist/esm/mcp/tools.d.ts.map +1 -1
  44. package/dist/esm/mcp/tools.js +288 -136
  45. package/dist/esm/mcp/tools.js.map +1 -1
  46. package/dist/esm/subconscious/TheBrainV2.d.ts +27 -1
  47. package/dist/esm/subconscious/TheBrainV2.d.ts.map +1 -1
  48. package/dist/esm/subconscious/TheBrainV2.js +53 -1
  49. package/dist/esm/subconscious/TheBrainV2.js.map +1 -1
  50. package/dist/esm/utils/AffectedTests.d.ts +54 -0
  51. package/dist/esm/utils/AffectedTests.d.ts.map +1 -0
  52. package/dist/esm/utils/AffectedTests.js +186 -0
  53. package/dist/esm/utils/AffectedTests.js.map +1 -0
  54. package/dist/esm/utils/DepGraph.d.ts +28 -0
  55. package/dist/esm/utils/DepGraph.d.ts.map +1 -0
  56. package/dist/esm/utils/DepGraph.js +169 -0
  57. package/dist/esm/utils/DepGraph.js.map +1 -0
  58. package/dist/esm/utils/LocalPrefilter.d.ts +61 -0
  59. package/dist/esm/utils/LocalPrefilter.d.ts.map +1 -0
  60. package/dist/esm/utils/LocalPrefilter.js +159 -0
  61. package/dist/esm/utils/LocalPrefilter.js.map +1 -0
  62. package/dist/esm/utils/OutputDelta.d.ts +60 -0
  63. package/dist/esm/utils/OutputDelta.d.ts.map +1 -0
  64. package/dist/esm/utils/OutputDelta.js +221 -0
  65. package/dist/esm/utils/OutputDelta.js.map +1 -0
  66. package/dist/esm/utils/OutputLedger.d.ts +60 -0
  67. package/dist/esm/utils/OutputLedger.d.ts.map +1 -0
  68. package/dist/esm/utils/OutputLedger.js +107 -0
  69. package/dist/esm/utils/OutputLedger.js.map +1 -0
  70. package/dist/esm/utils/ReadWorkspaceCache.d.ts +8 -2
  71. package/dist/esm/utils/ReadWorkspaceCache.d.ts.map +1 -1
  72. package/dist/esm/utils/ReadWorkspaceCache.js +57 -3
  73. package/dist/esm/utils/ReadWorkspaceCache.js.map +1 -1
  74. package/package.json +4 -2
@@ -80,7 +80,12 @@ const TokenReceipt_1 = require("../utils/TokenReceipt");
80
80
  const CommandOutputDistiller_1 = require("../utils/CommandOutputDistiller");
81
81
  const PatchMatcher_1 = require("../utils/PatchMatcher");
82
82
  const WorkspaceSearch_1 = require("../utils/WorkspaceSearch");
83
+ const LocalPrefilter_1 = require("../utils/LocalPrefilter");
83
84
  const ReadWorkspaceCache_1 = require("../utils/ReadWorkspaceCache");
85
+ const OutputDelta_1 = require("../utils/OutputDelta");
86
+ const OutputLedger_1 = require("../utils/OutputLedger");
87
+ const DepGraph_1 = require("../utils/DepGraph");
88
+ const AffectedTests_1 = require("../utils/AffectedTests");
84
89
  const LineNumberMap_1 = require("../utils/LineNumberMap");
85
90
  const FileOutline_1 = require("../utils/FileOutline");
86
91
  const SearchCache_1 = require("../utils/SearchCache");
@@ -180,6 +185,30 @@ const toolDefinitions = [
180
185
  description: "Symbols (not whole files) this answer depends on. Freshness is then judged by that symbol's own source, so edits elsewhere in the same file don't stale this memory out.",
181
186
  },
182
187
  outcome: { type: "string", enum: ["confirmed", "failed"], description: "Default 'confirmed' (a verified working solution). Set 'failed' to record a dead end — an approach ruled out, not one to recommend." },
188
+ claims: {
189
+ type: "array",
190
+ items: {
191
+ type: "object",
192
+ properties: {
193
+ text: { type: "string", description: "One sub-statement from `response`, verifiable on its own (e.g. one sentence about one function)." },
194
+ filePaths: { type: "array", items: { type: "string" }, description: "Paths this specific claim depends on." },
195
+ symbols: {
196
+ type: "array",
197
+ items: {
198
+ type: "object",
199
+ properties: {
200
+ filePath: { type: "string" },
201
+ symbolName: { type: "string" },
202
+ },
203
+ required: ["filePath", "symbolName"],
204
+ },
205
+ description: "Symbols this specific claim depends on.",
206
+ },
207
+ },
208
+ required: ["text"],
209
+ },
210
+ description: "Split `response` into independently-verifiable sub-claims when it makes more than one assertion about different parts of the code. Each claim tracks its own filePaths/symbols, so one claim going stale (e.g. one function changed) doesn't discard the others that are still true. Skip this for a single-fact response — plain filePaths/symbols above already covers that case.",
211
+ },
183
212
  },
184
213
  required: ["query", "response"],
185
214
  },
@@ -208,7 +237,7 @@ const toolDefinitions = [
208
237
  },
209
238
  {
210
239
  name: "read_workspace_file",
211
- description: "Read a file inside the workspace. Compresses comments/whitespace and scrubs API keys. Every line carries its REAL line number in the original file (compression removes lines, it never renumbers them), so you can cite file:line and patch precisely without re-reading. The header lists which imports were compressed away. Use offset/limit to read a line range of a large file. If this exact view was already read this session and hasn't changed on disk, returns an UNCHANGED marker plus a symbol outline reuse your copy, or pass force:true if it's no longer in your context.",
240
+ description: "Read a file inside the workspace. Compresses comments/whitespace and scrubs API keys. Every line carries its REAL line number in the original file (compression removes lines, it never renumbers them), so you can cite file:line and patch precisely without re-reading. The header lists which imports were compressed away. Use offset/limit to read a line range of a large file. If this exact view was already read this session and hasn't changed on disk, returns an UNCHANGED marker plus a symbol outline. If it changed only slightly, returns a CHANGED marker with a unified diff to apply to the copy you already hold. Either way, pass force:true to get the full file when it is no longer in your context.",
212
241
  inputSchema: {
213
242
  type: "object",
214
243
  properties: {
@@ -750,6 +779,17 @@ const toolDefinitions = [
750
779
  required: ["filePath"],
751
780
  },
752
781
  },
782
+ {
783
+ name: "affected_tests",
784
+ description: "Given the current diff (or an explicit file list), returns only the test files that transitively import the changed code, plus the exact command to run just those. Uses the TypeScript import graph — zero LLM calls. Run this instead of the full suite after an edit: fewer minutes, and far less test output in context. Reports honestly when the full suite is the right answer (unbounded change, runtime-only coupling, most of the suite affected).",
785
+ inputSchema: {
786
+ type: "object",
787
+ properties: {
788
+ files: { type: "array", items: { type: "string" }, description: "Explicit changed files, relative to workspace root. Omit to read the diff from git (working tree + untracked)." },
789
+ baseRef: { type: "string", description: "Additional git ref to diff against, e.g. 'main'. The working tree is always included." },
790
+ },
791
+ },
792
+ },
753
793
  {
754
794
  name: "refactor",
755
795
  description: "Declarative multi-file codemod engine. Rename symbols across the entire workspace or move files updating all imports. Uses TypeScript Compiler API — zero LLM calls. Supports dry-run diff preview and post-refactor tsc verification.",
@@ -1182,6 +1222,7 @@ const toolHandlers = {
1182
1222
  pr_status: handlePRStatus,
1183
1223
  generate_pr_workflow: handleGeneratePRWorkflow,
1184
1224
  depgraph: handleDepgraph,
1225
+ affected_tests: handleAffectedTests,
1185
1226
  refactor: handleRefactor,
1186
1227
  smart_file_slice: handleSmartFileSlice,
1187
1228
  test_oracle: handleTestOracle,
@@ -1391,7 +1432,16 @@ async function handleSearchMemory(args) {
1391
1432
  }
1392
1433
  const fresh = results.filter((r) => r.fresh);
1393
1434
  const stale = results.filter((r) => !r.fresh);
1394
- const formatResult = (r, i) => `Result ${i + 1} (Similarity: ${(r.similarity * 100).toFixed(1)}%)${r.outcome === "failed" ? "\n⚠️ TAGGED AS A FAILED ATTEMPT — this was tried before and did NOT work. Do not repeat it; treat this as a warning, not a suggestion." : ""}\nPrompt: ${r.query.substring(0, 300)}...\nResponse: ${typeof r.response === "string" ? r.response : JSON.stringify(r.response, null, 2)}`;
1435
+ const formatClaims = (r) => {
1436
+ if (!Array.isArray(r.claims) || r.claims.length === 0)
1437
+ return "";
1438
+ const lines = r.claims.map((c) => c.fresh
1439
+ ? ` ✓ fresh: ${c.text}`
1440
+ : ` ✗ STALE (${(c.staleFiles || []).join(", ")}): ${c.text}`);
1441
+ const staleCount = r.claims.filter((c) => !c.fresh).length;
1442
+ return `\nClaims (${r.claims.length - staleCount}/${r.claims.length} still fresh — trust only the ✓ ones):\n${lines.join("\n")}`;
1443
+ };
1444
+ const formatResult = (r, i) => `Result ${i + 1} (Similarity: ${(r.similarity * 100).toFixed(1)}%)${r.outcome === "failed" ? "\n⚠️ TAGGED AS A FAILED ATTEMPT — this was tried before and did NOT work. Do not repeat it; treat this as a warning, not a suggestion." : ""}\nPrompt: ${r.query.substring(0, 300)}...\nResponse: ${typeof r.response === "string" ? r.response : JSON.stringify(r.response, null, 2)}${formatClaims(r)}`;
1395
1445
  if (fresh.length > 0) {
1396
1446
  // Only a fresh hit (or an untracked, purely conceptual entry) counts as a real
1397
1447
  // avoided re-investigation — this is the only case worth crediting as savings.
@@ -1436,11 +1486,14 @@ async function handleStoreMemory(args) {
1436
1486
  const symbols = Array.isArray(args?.symbols)
1437
1487
  ? args.symbols.filter((s) => s?.filePath && s?.symbolName)
1438
1488
  : undefined;
1489
+ const claims = Array.isArray(args?.claims)
1490
+ ? args.claims.filter((c) => c?.text)
1491
+ : undefined;
1439
1492
  if (!query || !responseText)
1440
1493
  throw new Error("Query and response are required");
1441
1494
  try {
1442
1495
  const brain = (0, TheBrainV2_1.getBrain)();
1443
- const storeRes = brain.store(query, responseText, provider, 0.92, filePaths, undefined, outcome, symbols);
1496
+ const storeRes = brain.store(query, responseText, provider, 0.92, filePaths, undefined, outcome, symbols, claims);
1444
1497
  // Storing a memory saves nothing — it only creates the chance of a saving later.
1445
1498
  // Crediting tokens here inflated the ledger on write and then credited the same
1446
1499
  // answer again on every read. The saving is booked by search_memory on a fresh hit.
@@ -1452,7 +1505,8 @@ async function handleStoreMemory(args) {
1452
1505
  const trackingNote = filePaths && filePaths.length > 0 ? ` Tracking freshness against ${filePaths.length} file(s) — this memory auto-invalidates if they change.` : "";
1453
1506
  const symbolNote = symbols && symbols.length > 0 ? ` Tracking freshness against ${symbols.length} symbol(s) specifically — unrelated edits elsewhere in those files won't stale this out.` : "";
1454
1507
  const outcomeNote = outcome === "failed" ? " Tagged as a FAILED attempt — future searches will surface it as a warning, not a suggestion." : "";
1455
- return { content: [{ type: "text", text: `Success: Memory stored. ${storeRes.reason}${trackingNote}${symbolNote}${outcomeNote}` }] };
1508
+ const claimsNote = claims && claims.length > 0 ? ` Split into ${claims.length} independently-verifiable claim(s) — a future search can trust the ones still fresh even if another goes stale.` : "";
1509
+ return { content: [{ type: "text", text: `Success: Memory stored. ${storeRes.reason}${trackingNote}${symbolNote}${outcomeNote}${claimsNote}` }] };
1456
1510
  }
1457
1511
  catch (e) {
1458
1512
  (0, utils_1.logError)("store_memory", e);
@@ -1582,7 +1636,42 @@ async function handleReadWorkspaceFile(args) {
1582
1636
  }
1583
1637
  if (numbers)
1584
1638
  content = (0, LineNumberMap_1.withLineNumbers)(content, numbers);
1585
- (0, ReadWorkspaceCache_1.recordSent)(resolved, view, dedup.hash);
1639
+ // The file changed, but usually only slightly — the common case in an edit loop is a
1640
+ // few lines moving in a file the model already holds in full. Re-sending everything
1641
+ // charges for the whole file (and keeps charging, since tool results stay in the
1642
+ // transcript). A diff against the exact text we last emitted covers the same ground
1643
+ // for a fraction of the tokens; `shouldEmitDelta` declines whenever it wouldn't.
1644
+ if (!force) {
1645
+ const previousEmitted = (0, ReadWorkspaceCache_1.getLastEmitted)(resolved, view);
1646
+ if (previousEmitted !== null) {
1647
+ const delta = (0, OutputDelta_1.shouldEmitDelta)(previousEmitted, content);
1648
+ if (delta.emit) {
1649
+ (0, reportSavings_1.reportSavings)({
1650
+ source: "contextSqueeze",
1651
+ charsBefore: content.length,
1652
+ charsAfter: delta.body.length,
1653
+ toolName: "read_workspace_file",
1654
+ filePath,
1655
+ });
1656
+ (0, ReadWorkspaceCache_1.recordSent)(resolved, view, dedup.hash, content);
1657
+ return {
1658
+ content: [
1659
+ {
1660
+ type: "text",
1661
+ text: [
1662
+ `CHANGED: ${filePath} — ${delta.hunkCount} hunk(s), +${delta.added}/-${delta.removed} since you last read it.`,
1663
+ `Apply this diff to the copy you already have. Line numbers are real positions in the file on disk.`,
1664
+ `If you no longer hold that copy, re-fetch with force:true.`,
1665
+ ``,
1666
+ delta.body,
1667
+ ].join("\n"),
1668
+ },
1669
+ ],
1670
+ };
1671
+ }
1672
+ }
1673
+ }
1674
+ (0, ReadWorkspaceCache_1.recordSent)(resolved, view, dedup.hash, content);
1586
1675
  return { content: [{ type: "text", text: rangeHeader + content }] };
1587
1676
  }
1588
1677
  catch (err) {
@@ -1801,9 +1890,35 @@ async function handleRunWorkspaceCommand(args) {
1801
1890
  handle: distilled.handle,
1802
1891
  });
1803
1892
  }
1893
+ // A debug loop runs the same command repeatedly and the output barely moves between
1894
+ // runs. The header is part of the compared text on purpose: a pass/fail flip must never
1895
+ // be reported as "identical", even in the rare case where the body alone matches.
1896
+ // The footer is deliberately excluded and always re-appended — it carries this run's
1897
+ // `output_region` handle, which is different every time and must stay usable.
1898
+ const body = `${header}\n\nOutput:\n${distilled.text}`;
1899
+ const footer = (0, CommandOutputDistiller_1.buildDistillFooter)(distilled);
1900
+ const key = (0, OutputLedger_1.ledgerKey)("run_workspace_command", { command, cwd: workspaceRoot, raw: args?.raw === true });
1901
+ const ledger = (0, OutputLedger_1.checkOutput)(key, body, {
1902
+ label: `\`${command}\``,
1903
+ rerunHint: "The command did run again — this reports its result, it does not skip execution.",
1904
+ });
1905
+ (0, OutputLedger_1.recordOutput)(key, body);
1906
+ if (ledger.replacement) {
1907
+ (0, reportSavings_1.reportSavings)({
1908
+ source: "contextSqueeze",
1909
+ charsBefore: body.length,
1910
+ charsAfter: ledger.replacement.length,
1911
+ toolName: "run_workspace_command",
1912
+ });
1913
+ (0, TokenReceipt_1.recordReceiptEvent)("tool_call", `run_workspace_command ${ledger.status} vs. earlier run`, {
1914
+ command,
1915
+ savedChars: ledger.savedChars,
1916
+ });
1917
+ return { content: [{ type: "text", text: ledger.replacement + footer }] };
1918
+ }
1804
1919
  return {
1805
1920
  content: [
1806
- { type: "text", text: `${header}\n\nOutput:\n${distilled.text}${(0, CommandOutputDistiller_1.buildDistillFooter)(distilled)}` },
1921
+ { type: "text", text: body + footer },
1807
1922
  ],
1808
1923
  };
1809
1924
  }
@@ -1894,6 +2009,67 @@ async function handleListWorkspaceDir(args) {
1894
2009
  return { content: [{ type: "text", text: `Error listing directory: ${err.message}` }] };
1895
2010
  }
1896
2011
  }
2012
+ /** Files shown after a successful local ranking; the rest are named, not silently dropped. */
2013
+ const PREFILTER_FILES_SHOWN = 12;
2014
+ /**
2015
+ * Rank matching files with the local model and show only the top ones.
2016
+ *
2017
+ * Ranking happens per file, not per match: the model is choosing which files are worth
2018
+ * reading, and ranking 200 individual lines would ask it a question it cannot answer as
2019
+ * well. On any failure — disabled, no Ollama, unparseable reply — this returns the input
2020
+ * untouched, which is the behaviour with the feature off.
2021
+ */
2022
+ async function prefilterSearchMatches(query, matches) {
2023
+ const byFile = new Map();
2024
+ for (const m of matches) {
2025
+ const list = byFile.get(m.filePath);
2026
+ if (list)
2027
+ list.push(m);
2028
+ else
2029
+ byFile.set(m.filePath, [m]);
2030
+ }
2031
+ const totalFiles = byFile.size;
2032
+ const untouched = { matches, totalFiles, note: "", applied: false };
2033
+ if (totalFiles <= PREFILTER_FILES_SHOWN)
2034
+ return untouched;
2035
+ try {
2036
+ const candidates = [...byFile.entries()].map(([filePath, fileMatches]) => ({
2037
+ id: filePath,
2038
+ summary: `${filePath} (${fileMatches.length} match${fileMatches.length === 1 ? "" : "es"}): ${fileMatches[0].text.trim().slice(0, 120)}`,
2039
+ }));
2040
+ const ranked = await (0, LocalPrefilter_1.rankCandidates)(query, candidates);
2041
+ if (!ranked)
2042
+ return untouched;
2043
+ const shown = ranked.order.slice(0, PREFILTER_FILES_SHOWN);
2044
+ const withheld = ranked.order.slice(PREFILTER_FILES_SHOWN);
2045
+ if (withheld.length === 0)
2046
+ return untouched;
2047
+ const shownSet = new Set(shown);
2048
+ const kept = matches.filter((m) => shownSet.has(m.filePath));
2049
+ const note = [
2050
+ ``,
2051
+ `── ${withheld.length} more file(s) matched but were ranked lower by a local model (${ranked.model}) running on this machine:`,
2052
+ ...withheld.slice(0, 25).map((f) => ` ${f}`),
2053
+ ...(withheld.length > 25 ? [` … and ${withheld.length - 25} more`] : []),
2054
+ `This ranking is a local model's judgement, not a filter on relevance — if what you need`,
2055
+ `is not above, re-run this search with maxResults raised or a narrower query to see the`,
2056
+ `full results, or read one of the files listed here directly.`,
2057
+ ].join("\n");
2058
+ (0, reportSavings_1.reportSavings)({
2059
+ source: "contextSqueeze",
2060
+ charsBefore: matches.reduce((n, m) => n + m.text.length, 0),
2061
+ charsAfter: kept.reduce((n, m) => n + m.text.length, 0),
2062
+ toolName: "search_workspace",
2063
+ query: query.substring(0, 100),
2064
+ });
2065
+ return { matches: kept, totalFiles, note, applied: true };
2066
+ }
2067
+ catch (err) {
2068
+ // The prefilter is an optimization. It must never be the reason a search returns less.
2069
+ (0, utils_1.logWarn)("search_workspace", "local prefilter failed, returning unranked results");
2070
+ return untouched;
2071
+ }
2072
+ }
1897
2073
  async function handleSearchWorkspace(args) {
1898
2074
  const query = args?.query;
1899
2075
  const extension = args?.extension;
@@ -1904,29 +2080,62 @@ async function handleSearchWorkspace(args) {
1904
2080
  try {
1905
2081
  const extensions = (0, WorkspaceSearch_1.parseExtensionFilter)(extension);
1906
2082
  const cached = (0, SearchCache_1.getCachedSearch)(workspaceRoot, query, extensions, maxResults);
2083
+ // `SearchCache` spares the filesystem walk, but it still hands back the full result
2084
+ // text — and re-emitting an identical wall of matches costs exactly as many tokens as
2085
+ // producing it did. The ledger below is what turns that into a saving, so both the
2086
+ // cached and freshly-computed paths converge on one `resultText` first.
2087
+ let resultText;
1907
2088
  if (cached.text !== null) {
1908
2089
  (0, reportSavings_1.reportSavings)({ source: "cache", tokens: Math.floor(cached.text.length / 4), toolName: "search_workspace", query: query.substring(0, 100) });
1909
- return { content: [{ type: "text", text: cached.text }] };
1910
- }
1911
- const { matches, truncated } = (0, WorkspaceSearch_1.searchWorkspace)(workspaceRoot, query, extensions, maxResults);
1912
- if (matches.length === 0) {
1913
- const emptyText = `No matches found in the workspace for "${query}".`;
1914
- (0, SearchCache_1.setCachedSearch)(query, extensions, maxResults, cached.fingerprint, emptyText);
1915
- return { content: [{ type: "text", text: emptyText }] };
2090
+ resultText = cached.text;
1916
2091
  }
1917
- const grouped = (0, WorkspaceSearch_1.groupSearchResults)(matches, truncated, undefined, workspaceRoot);
1918
- let formatted = grouped.formatted;
1919
- try {
1920
- const scrubber = new SemanticScrubber_1.SemanticScrubber();
1921
- const { maskedPrompt } = scrubber.mask(formatted);
1922
- formatted = maskedPrompt;
2092
+ else {
2093
+ const { matches, truncated } = (0, WorkspaceSearch_1.searchWorkspace)(workspaceRoot, query, extensions, maxResults);
2094
+ if (matches.length === 0) {
2095
+ resultText = `No matches found in the workspace for "${query}".`;
2096
+ (0, SearchCache_1.setCachedSearch)(query, extensions, maxResults, cached.fingerprint, resultText);
2097
+ }
2098
+ else {
2099
+ // Opt-in: a local model ranks the matching files so the expensive model only reads
2100
+ // the ones likely to matter. It only ever reorders and truncates with disclosure —
2101
+ // and returns null on any doubt, in which case `matches` is used untouched.
2102
+ const prefilter = await prefilterSearchMatches(query, matches);
2103
+ const grouped = (0, WorkspaceSearch_1.groupSearchResults)(prefilter.matches, truncated, undefined, workspaceRoot);
2104
+ let formatted = grouped.formatted + prefilter.note;
2105
+ try {
2106
+ const scrubber = new SemanticScrubber_1.SemanticScrubber();
2107
+ const { maskedPrompt } = scrubber.mask(formatted);
2108
+ formatted = maskedPrompt;
2109
+ }
2110
+ catch (err) {
2111
+ (0, utils_1.logWarn)("search_workspace", "SemanticScrubber failed, returning raw results");
2112
+ }
2113
+ const truncationNote = grouped.truncated ? ` (truncated at ${maxResults} matches — narrow your query or extension filter for the rest)` : "";
2114
+ // The header reports what the search actually found, not what survived the
2115
+ // prefilter — otherwise a ranked-down file would vanish from the count as well as
2116
+ // from the list, and nothing would tell the reader it ever existed.
2117
+ resultText = `Found ${matches.length} matches across ${prefilter.totalFiles} files${truncationNote}:\n\n${formatted}`;
2118
+ // A ranked-down result is never cached: it would then be replayed to a later call
2119
+ // whose local model was unavailable, silently serving a subset as if it were the
2120
+ // whole search. Repeats are handled by the output ledger below instead.
2121
+ if (!prefilter.applied) {
2122
+ (0, SearchCache_1.setCachedSearch)(query, extensions, maxResults, cached.fingerprint, resultText);
2123
+ }
2124
+ }
1923
2125
  }
1924
- catch (err) {
1925
- (0, utils_1.logWarn)("search_workspace", "SemanticScrubber failed, returning raw results");
2126
+ const key = (0, OutputLedger_1.ledgerKey)("search_workspace", { query, extension: extension ?? null, maxResults, cwd: workspaceRoot });
2127
+ const ledger = (0, OutputLedger_1.checkOutput)(key, resultText, { label: `search for "${query}"` });
2128
+ (0, OutputLedger_1.recordOutput)(key, resultText);
2129
+ if (ledger.replacement) {
2130
+ (0, reportSavings_1.reportSavings)({
2131
+ source: "contextSqueeze",
2132
+ charsBefore: resultText.length,
2133
+ charsAfter: ledger.replacement.length,
2134
+ toolName: "search_workspace",
2135
+ query: query.substring(0, 100),
2136
+ });
2137
+ return { content: [{ type: "text", text: ledger.replacement }] };
1926
2138
  }
1927
- const truncationNote = grouped.truncated ? ` (truncated at ${maxResults} matches — narrow your query or extension filter for the rest)` : "";
1928
- const resultText = `Found ${grouped.totalMatches} matches across ${grouped.filesMatched} files${truncationNote}:\n\n${formatted}`;
1929
- (0, SearchCache_1.setCachedSearch)(query, extensions, maxResults, cached.fingerprint, resultText);
1930
2139
  return {
1931
2140
  content: [
1932
2141
  {
@@ -3812,113 +4021,9 @@ function generateAzurePipelineYaml(threshold) {
3812
4021
  ` AZURE_DEVOPS_TOKEN: \$(AZURE_DEVOPS_TOKEN)`,
3813
4022
  ].join("\n");
3814
4023
  }
3815
- function buildDepGraphMap(workspaceRoot) {
3816
- const map = new Map();
3817
- const IGNORE_DG = new Set(["node_modules", ".git", "dist", "chroma_data", ".lemma", "build", "coverage", ".next"]);
3818
- function walkDG(dir) {
3819
- let entries = [];
3820
- try {
3821
- entries = fs_1.default.readdirSync(dir, { withFileTypes: true });
3822
- }
3823
- catch {
3824
- return;
3825
- }
3826
- for (const e of entries) {
3827
- if (IGNORE_DG.has(e.name))
3828
- continue;
3829
- const abs = path_1.default.join(dir, e.name);
3830
- if (e.isDirectory()) {
3831
- walkDG(abs);
3832
- }
3833
- else if (/\.(ts|tsx|js|jsx)$/.test(e.name) && !e.name.endsWith(".d.ts")) {
3834
- const rel = path_1.default.relative(workspaceRoot, abs).replace(/\\/g, "/");
3835
- map.set(rel, { rel, abs, imports: [], importedBy: [], externalImports: [], exports: [] });
3836
- }
3837
- }
3838
- }
3839
- walkDG(workspaceRoot);
3840
- for (const [, node] of map) {
3841
- try {
3842
- const src = fs_1.default.readFileSync(node.abs, "utf8");
3843
- const sf = ts.createSourceFile(node.abs, src, ts.ScriptTarget.Latest, true);
3844
- ts.forEachChild(sf, (n) => {
3845
- if (ts.isImportDeclaration(n) && ts.isStringLiteral(n.moduleSpecifier)) {
3846
- const spec = n.moduleSpecifier.text;
3847
- if (spec.startsWith(".")) {
3848
- const dir = path_1.default.dirname(node.abs);
3849
- let resolved = path_1.default.resolve(dir, spec);
3850
- for (const ext of [".ts", ".tsx", ".js", ".jsx", "/index.ts", "/index.tsx", "/index.js"]) {
3851
- if (fs_1.default.existsSync(resolved + ext)) {
3852
- resolved += ext;
3853
- break;
3854
- }
3855
- }
3856
- const rel = path_1.default.relative(workspaceRoot, resolved).replace(/\\/g, "/");
3857
- if (!node.imports.includes(rel))
3858
- node.imports.push(rel);
3859
- }
3860
- else {
3861
- const pkg = spec.split("/")[0];
3862
- if (!node.externalImports.includes(pkg))
3863
- node.externalImports.push(pkg);
3864
- }
3865
- }
3866
- const hasExportMod = (nd) => ts.canHaveModifiers(nd) &&
3867
- (ts.getModifiers(nd) ?? []).some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
3868
- if (hasExportMod(n)) {
3869
- if (ts.isFunctionDeclaration(n) && n.name)
3870
- node.exports.push(n.name.text);
3871
- else if (ts.isClassDeclaration(n) && n.name)
3872
- node.exports.push(n.name.text);
3873
- else if (ts.isVariableStatement(n)) {
3874
- for (const decl of n.declarationList.declarations) {
3875
- if (ts.isIdentifier(decl.name))
3876
- node.exports.push(decl.name.text);
3877
- }
3878
- }
3879
- else if (ts.isInterfaceDeclaration(n))
3880
- node.exports.push(n.name.text);
3881
- else if (ts.isTypeAliasDeclaration(n))
3882
- node.exports.push(n.name.text);
3883
- else if (ts.isEnumDeclaration(n))
3884
- node.exports.push(n.name.text);
3885
- }
3886
- if (ts.isExportDeclaration(n) && n.exportClause && ts.isNamedExports(n.exportClause)) {
3887
- for (const el of n.exportClause.elements)
3888
- node.exports.push(el.name.text);
3889
- }
3890
- });
3891
- }
3892
- catch { /* skip unparseable */ }
3893
- }
3894
- for (const [, node] of map) {
3895
- for (const imp of node.imports) {
3896
- const target = map.get(imp);
3897
- if (target && !target.importedBy.includes(node.rel))
3898
- target.importedBy.push(node.rel);
3899
- }
3900
- }
3901
- return map;
3902
- }
3903
- function bfsDepGraph(map, startRel, direction, maxDepth) {
3904
- const visited = new Map();
3905
- const queue = [{ rel: startRel, depth: 0 }];
3906
- while (queue.length > 0) {
3907
- const item = queue.shift();
3908
- if (visited.has(item.rel) || item.depth > maxDepth)
3909
- continue;
3910
- const node = map.get(item.rel);
3911
- if (!node)
3912
- continue;
3913
- visited.set(item.rel, { node, depth: item.depth });
3914
- if (item.depth < maxDepth) {
3915
- const neighbors = direction === "forward" ? node.imports : node.importedBy;
3916
- for (const n of neighbors)
3917
- queue.push({ rel: n, depth: item.depth + 1 });
3918
- }
3919
- }
3920
- return visited;
3921
- }
4024
+ // ═══════════════════════════════════════════════════════════════════════════════
4025
+ // ─── depgraph ─────────────────────────────────────────────────────────────────
4026
+ // ═══════════════════════════════════════════════════════════════════════════════
3922
4027
  function detectDepCycles(map, subset) {
3923
4028
  const cycles = [];
3924
4029
  const visited = new Set();
@@ -4091,13 +4196,13 @@ async function handleDepgraph(args) {
4091
4196
  try {
4092
4197
  const { resolved } = (0, utils_1.safeResolvePath)(workspaceRoot, filePath);
4093
4198
  const relTarget = path_1.default.relative(workspaceRoot, resolved).replace(/\\/g, "/");
4094
- const map = buildDepGraphMap(workspaceRoot);
4199
+ const map = (0, DepGraph_1.buildDepGraphMap)(workspaceRoot);
4095
4200
  const targetNode = map.get(relTarget);
4096
4201
  if (!targetNode) {
4097
4202
  return { content: [{ type: "text", text: `File not found in workspace graph: ${relTarget}\nCheck path is correct and has .ts/.tsx/.js/.jsx extension.` }] };
4098
4203
  }
4099
- const forwardVisited = bfsDepGraph(map, relTarget, "forward", depth);
4100
- const backwardVisited = showReverse ? bfsDepGraph(map, relTarget, "backward", depth) : new Map();
4204
+ const forwardVisited = (0, DepGraph_1.bfsDepGraph)(map, relTarget, "forward", depth);
4205
+ const backwardVisited = showReverse ? (0, DepGraph_1.bfsDepGraph)(map, relTarget, "backward", depth) : new Map();
4101
4206
  const allRelInScope = new Set([...forwardVisited.keys(), ...(showReverse ? backwardVisited.keys() : [])]);
4102
4207
  const cycles = detectCircular ? detectDepCycles(map, allRelInScope) : [];
4103
4208
  let output;
@@ -4123,13 +4228,46 @@ async function handleDepgraph(args) {
4123
4228
  output = formatDGText(targetNode, forwardVisited, backwardVisited, cycles, includeExternal, showReverse);
4124
4229
  }
4125
4230
  const stats = `\n\n📊 Stats: ${forwardVisited.size} nodos forward, ${backwardVisited.size} nodos reverse, depth=${depth}`;
4126
- return { content: [{ type: "text", text: output + (format === "json" ? "" : stats) }] };
4231
+ const finalText = output + (format === "json" ? "" : stats);
4232
+ const key = (0, OutputLedger_1.ledgerKey)("depgraph", { file: relTarget, depth, format, showReverse, includeExternal, detectCircular, cwd: workspaceRoot });
4233
+ const ledger = (0, OutputLedger_1.checkOutput)(key, finalText, { label: `depgraph for ${relTarget}` });
4234
+ (0, OutputLedger_1.recordOutput)(key, finalText);
4235
+ if (ledger.replacement) {
4236
+ (0, reportSavings_1.reportSavings)({ source: "contextSqueeze", charsBefore: finalText.length, charsAfter: ledger.replacement.length, toolName: "depgraph", filePath: relTarget });
4237
+ return { content: [{ type: "text", text: ledger.replacement }] };
4238
+ }
4239
+ return { content: [{ type: "text", text: finalText }] };
4127
4240
  }
4128
4241
  catch (err) {
4129
4242
  (0, utils_1.logError)("depgraph", err);
4130
4243
  return { content: [{ type: "text", text: `depgraph failed: ${err.message}` }] };
4131
4244
  }
4132
4245
  }
4246
+ async function handleAffectedTests(args) {
4247
+ const workspaceRoot = process.cwd();
4248
+ try {
4249
+ const files = Array.isArray(args?.files) ? args.files.filter((f) => typeof f === "string") : undefined;
4250
+ const result = (0, AffectedTests_1.findAffectedTests)({ workspaceRoot, files, baseRef: args?.baseRef });
4251
+ const text = (0, AffectedTests_1.formatAffectedTests)(result);
4252
+ // Worth recording even when it narrows nothing: the receipt is the record of what this
4253
+ // tool actually did, and "recommended the full suite" is as much a result as a subset.
4254
+ (0, TokenReceipt_1.recordReceiptEvent)("tool_call", "affected_tests", {
4255
+ changed: result.changed.length,
4256
+ affected: result.affected.length,
4257
+ totalTests: result.totalTests,
4258
+ narrowed: result.command !== null,
4259
+ });
4260
+ return { content: [{ type: "text", text }] };
4261
+ }
4262
+ catch (err) {
4263
+ (0, utils_1.logError)("affected_tests", err);
4264
+ // Failing here must never look like "nothing is affected" — that would talk a caller
4265
+ // out of running tests at all.
4266
+ return {
4267
+ content: [{ type: "text", text: `affected_tests failed: ${err.message}\nFall back to running the full suite.` }],
4268
+ };
4269
+ }
4270
+ }
4133
4271
  // ═══════════════════════════════════════════════════════════════════════════════
4134
4272
  // ─── refactor ─────────────────────────────────────────────────────────────────
4135
4273
  // ═══════════════════════════════════════════════════════════════════════════════
@@ -4627,10 +4765,13 @@ async function handleSmartFileSlice(args) {
4627
4765
  async function handleTestOracle(args) {
4628
4766
  const command = args.command || "npm test";
4629
4767
  const workspaceRoot = process.cwd();
4630
- // Run tests, check errors
4768
+ // A failing test suite is re-run more than any other command in a session, and the
4769
+ // failure list barely moves between runs. Both branches funnel into one `resultText` so
4770
+ // the ledger can answer a repeat with a diff instead of the same stack traces again.
4771
+ let resultText;
4631
4772
  try {
4632
4773
  const out = (0, child_process_1.execSync)(command, { cwd: workspaceRoot, encoding: "utf8", timeout: 30000 });
4633
- return { content: [{ type: "text", text: `All tests passed!\n\nOutput:\n${out.substring(0, 1000)}` }] };
4774
+ resultText = `All tests passed!\n\nOutput:\n${out.substring(0, 1000)}`;
4634
4775
  }
4635
4776
  catch (e) {
4636
4777
  const stdout = e.stdout || "";
@@ -4640,8 +4781,19 @@ async function handleTestOracle(args) {
4640
4781
  .split("\n")
4641
4782
  .filter((l) => l.includes("fail") || l.includes("Error") || l.includes("at ") || l.includes("✗") || l.includes("Stack"));
4642
4783
  const summary = failureLines.slice(0, 80).join("\n") || `Test execution failed:\n${stdout.substring(0, 800)}\n${stderr.substring(0, 800)}`;
4643
- return { content: [{ type: "text", text: `Tests Failed. Compressed failures:\n\n${summary}` }] };
4784
+ resultText = `Tests Failed. Compressed failures:\n\n${summary}`;
4785
+ }
4786
+ const key = (0, OutputLedger_1.ledgerKey)("test_oracle", { command, cwd: workspaceRoot });
4787
+ const ledger = (0, OutputLedger_1.checkOutput)(key, resultText, {
4788
+ label: `\`${command}\``,
4789
+ rerunHint: "The suite did run again — this reports its result, it does not skip execution.",
4790
+ });
4791
+ (0, OutputLedger_1.recordOutput)(key, resultText);
4792
+ if (ledger.replacement) {
4793
+ (0, reportSavings_1.reportSavings)({ source: "contextSqueeze", charsBefore: resultText.length, charsAfter: ledger.replacement.length, toolName: "test_oracle" });
4794
+ return { content: [{ type: "text", text: ledger.replacement }] };
4644
4795
  }
4796
+ return { content: [{ type: "text", text: resultText }] };
4645
4797
  }
4646
4798
  // ── Schema Extract ───────────────────────────────────────────────────
4647
4799
  async function handleSchemaExtract(args) {