@nxuss/lemma 1.9.1 → 1.11.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 (122) hide show
  1. package/README.md +24 -4
  2. package/dist/cjs/cli/lemma-proxy.d.ts.map +1 -1
  3. package/dist/cjs/cli/lemma-proxy.js +7 -2
  4. package/dist/cjs/cli/lemma-proxy.js.map +1 -1
  5. package/dist/cjs/mcp/tool-profiles.d.ts.map +1 -1
  6. package/dist/cjs/mcp/tool-profiles.js +43 -0
  7. package/dist/cjs/mcp/tool-profiles.js.map +1 -1
  8. package/dist/cjs/mcp/tools.d.ts.map +1 -1
  9. package/dist/cjs/mcp/tools.js +314 -146
  10. package/dist/cjs/mcp/tools.js.map +1 -1
  11. package/dist/cjs/subconscious/TheBrainV2.d.ts +27 -1
  12. package/dist/cjs/subconscious/TheBrainV2.d.ts.map +1 -1
  13. package/dist/cjs/subconscious/TheBrainV2.js +53 -1
  14. package/dist/cjs/subconscious/TheBrainV2.js.map +1 -1
  15. package/dist/cjs/utils/AffectedTests.d.ts +54 -0
  16. package/dist/cjs/utils/AffectedTests.d.ts.map +1 -0
  17. package/dist/cjs/utils/AffectedTests.js +195 -0
  18. package/dist/cjs/utils/AffectedTests.js.map +1 -0
  19. package/dist/cjs/utils/AstSymbolCache.d.ts +29 -0
  20. package/dist/cjs/utils/AstSymbolCache.d.ts.map +1 -0
  21. package/dist/cjs/utils/AstSymbolCache.js +66 -0
  22. package/dist/cjs/utils/AstSymbolCache.js.map +1 -0
  23. package/dist/cjs/utils/ContextSqueezer.d.ts +24 -4
  24. package/dist/cjs/utils/ContextSqueezer.d.ts.map +1 -1
  25. package/dist/cjs/utils/ContextSqueezer.js +32 -3
  26. package/dist/cjs/utils/ContextSqueezer.js.map +1 -1
  27. package/dist/cjs/utils/DepGraph.d.ts +28 -0
  28. package/dist/cjs/utils/DepGraph.d.ts.map +1 -0
  29. package/dist/cjs/utils/DepGraph.js +210 -0
  30. package/dist/cjs/utils/DepGraph.js.map +1 -0
  31. package/dist/cjs/utils/LocalPrefilter.d.ts +61 -0
  32. package/dist/cjs/utils/LocalPrefilter.d.ts.map +1 -0
  33. package/dist/cjs/utils/LocalPrefilter.js +169 -0
  34. package/dist/cjs/utils/LocalPrefilter.js.map +1 -0
  35. package/dist/cjs/utils/OutputDelta.d.ts +60 -0
  36. package/dist/cjs/utils/OutputDelta.d.ts.map +1 -0
  37. package/dist/cjs/utils/OutputDelta.js +226 -0
  38. package/dist/cjs/utils/OutputDelta.js.map +1 -0
  39. package/dist/cjs/utils/OutputLedger.d.ts +60 -0
  40. package/dist/cjs/utils/OutputLedger.d.ts.map +1 -0
  41. package/dist/cjs/utils/OutputLedger.js +134 -0
  42. package/dist/cjs/utils/OutputLedger.js.map +1 -0
  43. package/dist/cjs/utils/ReadWorkspaceCache.d.ts +8 -2
  44. package/dist/cjs/utils/ReadWorkspaceCache.d.ts.map +1 -1
  45. package/dist/cjs/utils/ReadWorkspaceCache.js +82 -4
  46. package/dist/cjs/utils/ReadWorkspaceCache.js.map +1 -1
  47. package/dist/cjs/utils/RoiLedger.d.ts +31 -0
  48. package/dist/cjs/utils/RoiLedger.d.ts.map +1 -0
  49. package/dist/cjs/utils/RoiLedger.js +113 -0
  50. package/dist/cjs/utils/RoiLedger.js.map +1 -0
  51. package/dist/cjs/utils/SharedLedgerStore.d.ts +26 -0
  52. package/dist/cjs/utils/SharedLedgerStore.d.ts.map +1 -0
  53. package/dist/cjs/utils/SharedLedgerStore.js +130 -0
  54. package/dist/cjs/utils/SharedLedgerStore.js.map +1 -0
  55. package/dist/cjs/utils/SpeculativeWarmer.d.ts +24 -0
  56. package/dist/cjs/utils/SpeculativeWarmer.d.ts.map +1 -0
  57. package/dist/cjs/utils/SpeculativeWarmer.js +73 -0
  58. package/dist/cjs/utils/SpeculativeWarmer.js.map +1 -0
  59. package/dist/cjs/utils/reportSavings.d.ts.map +1 -1
  60. package/dist/cjs/utils/reportSavings.js +11 -0
  61. package/dist/cjs/utils/reportSavings.js.map +1 -1
  62. package/dist/esm/cli/lemma-proxy.d.ts.map +1 -1
  63. package/dist/esm/cli/lemma-proxy.js +7 -2
  64. package/dist/esm/cli/lemma-proxy.js.map +1 -1
  65. package/dist/esm/mcp/tool-profiles.d.ts.map +1 -1
  66. package/dist/esm/mcp/tool-profiles.js +43 -0
  67. package/dist/esm/mcp/tool-profiles.js.map +1 -1
  68. package/dist/esm/mcp/tools.d.ts.map +1 -1
  69. package/dist/esm/mcp/tools.js +312 -144
  70. package/dist/esm/mcp/tools.js.map +1 -1
  71. package/dist/esm/subconscious/TheBrainV2.d.ts +27 -1
  72. package/dist/esm/subconscious/TheBrainV2.d.ts.map +1 -1
  73. package/dist/esm/subconscious/TheBrainV2.js +53 -1
  74. package/dist/esm/subconscious/TheBrainV2.js.map +1 -1
  75. package/dist/esm/utils/AffectedTests.d.ts +54 -0
  76. package/dist/esm/utils/AffectedTests.d.ts.map +1 -0
  77. package/dist/esm/utils/AffectedTests.js +186 -0
  78. package/dist/esm/utils/AffectedTests.js.map +1 -0
  79. package/dist/esm/utils/AstSymbolCache.d.ts +29 -0
  80. package/dist/esm/utils/AstSymbolCache.d.ts.map +1 -0
  81. package/dist/esm/utils/AstSymbolCache.js +58 -0
  82. package/dist/esm/utils/AstSymbolCache.js.map +1 -0
  83. package/dist/esm/utils/ContextSqueezer.d.ts +24 -4
  84. package/dist/esm/utils/ContextSqueezer.d.ts.map +1 -1
  85. package/dist/esm/utils/ContextSqueezer.js +32 -3
  86. package/dist/esm/utils/ContextSqueezer.js.map +1 -1
  87. package/dist/esm/utils/DepGraph.d.ts +28 -0
  88. package/dist/esm/utils/DepGraph.d.ts.map +1 -0
  89. package/dist/esm/utils/DepGraph.js +169 -0
  90. package/dist/esm/utils/DepGraph.js.map +1 -0
  91. package/dist/esm/utils/LocalPrefilter.d.ts +61 -0
  92. package/dist/esm/utils/LocalPrefilter.d.ts.map +1 -0
  93. package/dist/esm/utils/LocalPrefilter.js +159 -0
  94. package/dist/esm/utils/LocalPrefilter.js.map +1 -0
  95. package/dist/esm/utils/OutputDelta.d.ts +60 -0
  96. package/dist/esm/utils/OutputDelta.d.ts.map +1 -0
  97. package/dist/esm/utils/OutputDelta.js +221 -0
  98. package/dist/esm/utils/OutputDelta.js.map +1 -0
  99. package/dist/esm/utils/OutputLedger.d.ts +60 -0
  100. package/dist/esm/utils/OutputLedger.d.ts.map +1 -0
  101. package/dist/esm/utils/OutputLedger.js +125 -0
  102. package/dist/esm/utils/OutputLedger.js.map +1 -0
  103. package/dist/esm/utils/ReadWorkspaceCache.d.ts +8 -2
  104. package/dist/esm/utils/ReadWorkspaceCache.d.ts.map +1 -1
  105. package/dist/esm/utils/ReadWorkspaceCache.js +81 -4
  106. package/dist/esm/utils/ReadWorkspaceCache.js.map +1 -1
  107. package/dist/esm/utils/RoiLedger.d.ts +31 -0
  108. package/dist/esm/utils/RoiLedger.d.ts.map +1 -0
  109. package/dist/esm/utils/RoiLedger.js +104 -0
  110. package/dist/esm/utils/RoiLedger.js.map +1 -0
  111. package/dist/esm/utils/SharedLedgerStore.d.ts +26 -0
  112. package/dist/esm/utils/SharedLedgerStore.d.ts.map +1 -0
  113. package/dist/esm/utils/SharedLedgerStore.js +122 -0
  114. package/dist/esm/utils/SharedLedgerStore.js.map +1 -0
  115. package/dist/esm/utils/SpeculativeWarmer.d.ts +24 -0
  116. package/dist/esm/utils/SpeculativeWarmer.d.ts.map +1 -0
  117. package/dist/esm/utils/SpeculativeWarmer.js +66 -0
  118. package/dist/esm/utils/SpeculativeWarmer.js.map +1 -0
  119. package/dist/esm/utils/reportSavings.d.ts.map +1 -1
  120. package/dist/esm/utils/reportSavings.js +11 -0
  121. package/dist/esm/utils/reportSavings.js.map +1 -1
  122. package/package.json +1 -1
@@ -37,7 +37,14 @@ import { recordReceiptEvent, getReceiptSummary, getLedgerLength } from "../utils
37
37
  import { distillCommandOutput, buildDistillFooter, readRegion } from "../utils/CommandOutputDistiller.js";
38
38
  import { findMatch, reindentReplacement } from "../utils/PatchMatcher.js";
39
39
  import { searchWorkspace, groupSearchResults, parseExtensionFilter } from "../utils/WorkspaceSearch.js";
40
- import { checkAlreadySent, recordSent } from "../utils/ReadWorkspaceCache.js";
40
+ import { rankCandidates } from "../utils/LocalPrefilter.js";
41
+ import { checkAlreadySent, recordSent, getLastEmitted } from "../utils/ReadWorkspaceCache.js";
42
+ import { shouldEmitDelta } from "../utils/OutputDelta.js";
43
+ import { ledgerKey, checkOutput, recordOutput as recordLedgerOutput } from "../utils/OutputLedger.js";
44
+ import { buildDepGraphMap, bfsDepGraph } from "../utils/DepGraph.js";
45
+ import { getOrExtractSymbols } from "../utils/AstSymbolCache.js";
46
+ import { warmNeighbors, registerSymbolExtractor } from "../utils/SpeculativeWarmer.js";
47
+ import { findAffectedTests, formatAffectedTests } from "../utils/AffectedTests.js";
41
48
  import { mapSqueezedToOriginal, withLineNumbers, sequentialNumbers } from "../utils/LineNumberMap.js";
42
49
  import { fileOutline, formatOutline } from "../utils/FileOutline.js";
43
50
  import { getCachedSearch, setCachedSearch } from "../utils/SearchCache.js";
@@ -137,6 +144,30 @@ const toolDefinitions = [
137
144
  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.",
138
145
  },
139
146
  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." },
147
+ claims: {
148
+ type: "array",
149
+ items: {
150
+ type: "object",
151
+ properties: {
152
+ text: { type: "string", description: "One sub-statement from `response`, verifiable on its own (e.g. one sentence about one function)." },
153
+ filePaths: { type: "array", items: { type: "string" }, description: "Paths this specific claim depends on." },
154
+ symbols: {
155
+ type: "array",
156
+ items: {
157
+ type: "object",
158
+ properties: {
159
+ filePath: { type: "string" },
160
+ symbolName: { type: "string" },
161
+ },
162
+ required: ["filePath", "symbolName"],
163
+ },
164
+ description: "Symbols this specific claim depends on.",
165
+ },
166
+ },
167
+ required: ["text"],
168
+ },
169
+ 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.",
170
+ },
140
171
  },
141
172
  required: ["query", "response"],
142
173
  },
@@ -165,7 +196,7 @@ const toolDefinitions = [
165
196
  },
166
197
  {
167
198
  name: "read_workspace_file",
168
- 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.",
199
+ 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.",
169
200
  inputSchema: {
170
201
  type: "object",
171
202
  properties: {
@@ -373,7 +404,7 @@ const toolDefinitions = [
373
404
  },
374
405
  {
375
406
  name: "prune_conversation_history",
376
- description: "Compress conversation history by decimating old turns and compacting large code blocks in historical messages. Keeps recent context intact.",
407
+ description: "Compress conversation history by decimating old turns and compacting large code blocks in historical messages. Keeps recent context intact. Refuses to mutate anything by default because pruning invalidates the prompt-cache prefix, which is usually a net loss — pass force:true to actually apply it, or call without force to just get the estimated cache-invalidation cost back.",
377
408
  inputSchema: {
378
409
  type: "object",
379
410
  properties: {
@@ -382,6 +413,11 @@ const toolDefinitions = [
382
413
  items: { type: "object" },
383
414
  description: "Array of conversation messages with role + content",
384
415
  },
416
+ force: {
417
+ type: "boolean",
418
+ description: "Actually apply the prune despite the prompt-cache-invalidation cost. Default false — the call is a dry run that reports cacheInvalidationEstimate instead.",
419
+ default: false,
420
+ },
385
421
  },
386
422
  required: ["messages"],
387
423
  },
@@ -707,6 +743,17 @@ const toolDefinitions = [
707
743
  required: ["filePath"],
708
744
  },
709
745
  },
746
+ {
747
+ name: "affected_tests",
748
+ 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).",
749
+ inputSchema: {
750
+ type: "object",
751
+ properties: {
752
+ files: { type: "array", items: { type: "string" }, description: "Explicit changed files, relative to workspace root. Omit to read the diff from git (working tree + untracked)." },
753
+ baseRef: { type: "string", description: "Additional git ref to diff against, e.g. 'main'. The working tree is always included." },
754
+ },
755
+ },
756
+ },
710
757
  {
711
758
  name: "refactor",
712
759
  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.",
@@ -1139,6 +1186,7 @@ const toolHandlers = {
1139
1186
  pr_status: handlePRStatus,
1140
1187
  generate_pr_workflow: handleGeneratePRWorkflow,
1141
1188
  depgraph: handleDepgraph,
1189
+ affected_tests: handleAffectedTests,
1142
1190
  refactor: handleRefactor,
1143
1191
  smart_file_slice: handleSmartFileSlice,
1144
1192
  test_oracle: handleTestOracle,
@@ -1239,6 +1287,7 @@ function receiptLabelMeta(name, args) {
1239
1287
  return meta;
1240
1288
  }
1241
1289
  export function setupToolsHandlers(server, onToolCall) {
1290
+ registerSymbolExtractor(extractSymbolsWithTsCompiler);
1242
1291
  server.setRequestHandler(ListToolsRequestSchema, async () => ({
1243
1292
  tools: toolDefinitionsArray,
1244
1293
  }));
@@ -1348,7 +1397,16 @@ async function handleSearchMemory(args) {
1348
1397
  }
1349
1398
  const fresh = results.filter((r) => r.fresh);
1350
1399
  const stale = results.filter((r) => !r.fresh);
1351
- 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)}`;
1400
+ const formatClaims = (r) => {
1401
+ if (!Array.isArray(r.claims) || r.claims.length === 0)
1402
+ return "";
1403
+ const lines = r.claims.map((c) => c.fresh
1404
+ ? ` ✓ fresh: ${c.text}`
1405
+ : ` ✗ STALE (${(c.staleFiles || []).join(", ")}): ${c.text}`);
1406
+ const staleCount = r.claims.filter((c) => !c.fresh).length;
1407
+ return `\nClaims (${r.claims.length - staleCount}/${r.claims.length} still fresh — trust only the ✓ ones):\n${lines.join("\n")}`;
1408
+ };
1409
+ 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)}`;
1352
1410
  if (fresh.length > 0) {
1353
1411
  // Only a fresh hit (or an untracked, purely conceptual entry) counts as a real
1354
1412
  // avoided re-investigation — this is the only case worth crediting as savings.
@@ -1393,11 +1451,14 @@ async function handleStoreMemory(args) {
1393
1451
  const symbols = Array.isArray(args?.symbols)
1394
1452
  ? args.symbols.filter((s) => s?.filePath && s?.symbolName)
1395
1453
  : undefined;
1454
+ const claims = Array.isArray(args?.claims)
1455
+ ? args.claims.filter((c) => c?.text)
1456
+ : undefined;
1396
1457
  if (!query || !responseText)
1397
1458
  throw new Error("Query and response are required");
1398
1459
  try {
1399
1460
  const brain = getBrain();
1400
- const storeRes = brain.store(query, responseText, provider, 0.92, filePaths, undefined, outcome, symbols);
1461
+ const storeRes = brain.store(query, responseText, provider, 0.92, filePaths, undefined, outcome, symbols, claims);
1401
1462
  // Storing a memory saves nothing — it only creates the chance of a saving later.
1402
1463
  // Crediting tokens here inflated the ledger on write and then credited the same
1403
1464
  // answer again on every read. The saving is booked by search_memory on a fresh hit.
@@ -1409,7 +1470,8 @@ async function handleStoreMemory(args) {
1409
1470
  const trackingNote = filePaths && filePaths.length > 0 ? ` Tracking freshness against ${filePaths.length} file(s) — this memory auto-invalidates if they change.` : "";
1410
1471
  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.` : "";
1411
1472
  const outcomeNote = outcome === "failed" ? " Tagged as a FAILED attempt — future searches will surface it as a warning, not a suggestion." : "";
1412
- return { content: [{ type: "text", text: `Success: Memory stored. ${storeRes.reason}${trackingNote}${symbolNote}${outcomeNote}` }] };
1473
+ 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.` : "";
1474
+ return { content: [{ type: "text", text: `Success: Memory stored. ${storeRes.reason}${trackingNote}${symbolNote}${outcomeNote}${claimsNote}` }] };
1413
1475
  }
1414
1476
  catch (e) {
1415
1477
  logError("store_memory", e);
@@ -1539,7 +1601,44 @@ async function handleReadWorkspaceFile(args) {
1539
1601
  }
1540
1602
  if (numbers)
1541
1603
  content = withLineNumbers(content, numbers);
1542
- recordSent(resolved, view, dedup.hash);
1604
+ // The file changed, but usually only slightly — the common case in an edit loop is a
1605
+ // few lines moving in a file the model already holds in full. Re-sending everything
1606
+ // charges for the whole file (and keeps charging, since tool results stay in the
1607
+ // transcript). A diff against the exact text we last emitted covers the same ground
1608
+ // for a fraction of the tokens; `shouldEmitDelta` declines whenever it wouldn't.
1609
+ if (!force) {
1610
+ const previousEmitted = getLastEmitted(resolved, view);
1611
+ if (previousEmitted !== null) {
1612
+ const delta = shouldEmitDelta(previousEmitted, content);
1613
+ if (delta.emit) {
1614
+ reportSavings({
1615
+ source: "contextSqueeze",
1616
+ charsBefore: content.length,
1617
+ charsAfter: delta.body.length,
1618
+ toolName: "read_workspace_file",
1619
+ filePath,
1620
+ });
1621
+ recordSent(resolved, view, dedup.hash, content);
1622
+ warmNeighbors(workspaceRoot, resolved);
1623
+ return {
1624
+ content: [
1625
+ {
1626
+ type: "text",
1627
+ text: [
1628
+ `CHANGED: ${filePath} — ${delta.hunkCount} hunk(s), +${delta.added}/-${delta.removed} since you last read it.`,
1629
+ `Apply this diff to the copy you already have. Line numbers are real positions in the file on disk.`,
1630
+ `If you no longer hold that copy, re-fetch with force:true.`,
1631
+ ``,
1632
+ delta.body,
1633
+ ].join("\n"),
1634
+ },
1635
+ ],
1636
+ };
1637
+ }
1638
+ }
1639
+ }
1640
+ recordSent(resolved, view, dedup.hash, content);
1641
+ warmNeighbors(workspaceRoot, resolved);
1543
1642
  return { content: [{ type: "text", text: rangeHeader + content }] };
1544
1643
  }
1545
1644
  catch (err) {
@@ -1758,9 +1857,35 @@ async function handleRunWorkspaceCommand(args) {
1758
1857
  handle: distilled.handle,
1759
1858
  });
1760
1859
  }
1860
+ // A debug loop runs the same command repeatedly and the output barely moves between
1861
+ // runs. The header is part of the compared text on purpose: a pass/fail flip must never
1862
+ // be reported as "identical", even in the rare case where the body alone matches.
1863
+ // The footer is deliberately excluded and always re-appended — it carries this run's
1864
+ // `output_region` handle, which is different every time and must stay usable.
1865
+ const body = `${header}\n\nOutput:\n${distilled.text}`;
1866
+ const footer = buildDistillFooter(distilled);
1867
+ const key = ledgerKey("run_workspace_command", { command, cwd: workspaceRoot, raw: args?.raw === true });
1868
+ const ledger = checkOutput(key, body, {
1869
+ label: `\`${command}\``,
1870
+ rerunHint: "The command did run again — this reports its result, it does not skip execution.",
1871
+ });
1872
+ recordLedgerOutput(key, body);
1873
+ if (ledger.replacement) {
1874
+ reportSavings({
1875
+ source: "contextSqueeze",
1876
+ charsBefore: body.length,
1877
+ charsAfter: ledger.replacement.length,
1878
+ toolName: "run_workspace_command",
1879
+ });
1880
+ recordReceiptEvent("tool_call", `run_workspace_command ${ledger.status} vs. earlier run`, {
1881
+ command,
1882
+ savedChars: ledger.savedChars,
1883
+ });
1884
+ return { content: [{ type: "text", text: ledger.replacement + footer }] };
1885
+ }
1761
1886
  return {
1762
1887
  content: [
1763
- { type: "text", text: `${header}\n\nOutput:\n${distilled.text}${buildDistillFooter(distilled)}` },
1888
+ { type: "text", text: body + footer },
1764
1889
  ],
1765
1890
  };
1766
1891
  }
@@ -1851,6 +1976,67 @@ async function handleListWorkspaceDir(args) {
1851
1976
  return { content: [{ type: "text", text: `Error listing directory: ${err.message}` }] };
1852
1977
  }
1853
1978
  }
1979
+ /** Files shown after a successful local ranking; the rest are named, not silently dropped. */
1980
+ const PREFILTER_FILES_SHOWN = 12;
1981
+ /**
1982
+ * Rank matching files with the local model and show only the top ones.
1983
+ *
1984
+ * Ranking happens per file, not per match: the model is choosing which files are worth
1985
+ * reading, and ranking 200 individual lines would ask it a question it cannot answer as
1986
+ * well. On any failure — disabled, no Ollama, unparseable reply — this returns the input
1987
+ * untouched, which is the behaviour with the feature off.
1988
+ */
1989
+ async function prefilterSearchMatches(query, matches) {
1990
+ const byFile = new Map();
1991
+ for (const m of matches) {
1992
+ const list = byFile.get(m.filePath);
1993
+ if (list)
1994
+ list.push(m);
1995
+ else
1996
+ byFile.set(m.filePath, [m]);
1997
+ }
1998
+ const totalFiles = byFile.size;
1999
+ const untouched = { matches, totalFiles, note: "", applied: false };
2000
+ if (totalFiles <= PREFILTER_FILES_SHOWN)
2001
+ return untouched;
2002
+ try {
2003
+ const candidates = [...byFile.entries()].map(([filePath, fileMatches]) => ({
2004
+ id: filePath,
2005
+ summary: `${filePath} (${fileMatches.length} match${fileMatches.length === 1 ? "" : "es"}): ${fileMatches[0].text.trim().slice(0, 120)}`,
2006
+ }));
2007
+ const ranked = await rankCandidates(query, candidates);
2008
+ if (!ranked)
2009
+ return untouched;
2010
+ const shown = ranked.order.slice(0, PREFILTER_FILES_SHOWN);
2011
+ const withheld = ranked.order.slice(PREFILTER_FILES_SHOWN);
2012
+ if (withheld.length === 0)
2013
+ return untouched;
2014
+ const shownSet = new Set(shown);
2015
+ const kept = matches.filter((m) => shownSet.has(m.filePath));
2016
+ const note = [
2017
+ ``,
2018
+ `── ${withheld.length} more file(s) matched but were ranked lower by a local model (${ranked.model}) running on this machine:`,
2019
+ ...withheld.slice(0, 25).map((f) => ` ${f}`),
2020
+ ...(withheld.length > 25 ? [` … and ${withheld.length - 25} more`] : []),
2021
+ `This ranking is a local model's judgement, not a filter on relevance — if what you need`,
2022
+ `is not above, re-run this search with maxResults raised or a narrower query to see the`,
2023
+ `full results, or read one of the files listed here directly.`,
2024
+ ].join("\n");
2025
+ reportSavings({
2026
+ source: "contextSqueeze",
2027
+ charsBefore: matches.reduce((n, m) => n + m.text.length, 0),
2028
+ charsAfter: kept.reduce((n, m) => n + m.text.length, 0),
2029
+ toolName: "search_workspace",
2030
+ query: query.substring(0, 100),
2031
+ });
2032
+ return { matches: kept, totalFiles, note, applied: true };
2033
+ }
2034
+ catch (err) {
2035
+ // The prefilter is an optimization. It must never be the reason a search returns less.
2036
+ logWarn("search_workspace", "local prefilter failed, returning unranked results");
2037
+ return untouched;
2038
+ }
2039
+ }
1854
2040
  async function handleSearchWorkspace(args) {
1855
2041
  const query = args?.query;
1856
2042
  const extension = args?.extension;
@@ -1861,29 +2047,62 @@ async function handleSearchWorkspace(args) {
1861
2047
  try {
1862
2048
  const extensions = parseExtensionFilter(extension);
1863
2049
  const cached = getCachedSearch(workspaceRoot, query, extensions, maxResults);
2050
+ // `SearchCache` spares the filesystem walk, but it still hands back the full result
2051
+ // text — and re-emitting an identical wall of matches costs exactly as many tokens as
2052
+ // producing it did. The ledger below is what turns that into a saving, so both the
2053
+ // cached and freshly-computed paths converge on one `resultText` first.
2054
+ let resultText;
1864
2055
  if (cached.text !== null) {
1865
2056
  reportSavings({ source: "cache", tokens: Math.floor(cached.text.length / 4), toolName: "search_workspace", query: query.substring(0, 100) });
1866
- return { content: [{ type: "text", text: cached.text }] };
1867
- }
1868
- const { matches, truncated } = searchWorkspace(workspaceRoot, query, extensions, maxResults);
1869
- if (matches.length === 0) {
1870
- const emptyText = `No matches found in the workspace for "${query}".`;
1871
- setCachedSearch(query, extensions, maxResults, cached.fingerprint, emptyText);
1872
- return { content: [{ type: "text", text: emptyText }] };
2057
+ resultText = cached.text;
1873
2058
  }
1874
- const grouped = groupSearchResults(matches, truncated, undefined, workspaceRoot);
1875
- let formatted = grouped.formatted;
1876
- try {
1877
- const scrubber = new SemanticScrubber();
1878
- const { maskedPrompt } = scrubber.mask(formatted);
1879
- formatted = maskedPrompt;
2059
+ else {
2060
+ const { matches, truncated } = searchWorkspace(workspaceRoot, query, extensions, maxResults);
2061
+ if (matches.length === 0) {
2062
+ resultText = `No matches found in the workspace for "${query}".`;
2063
+ setCachedSearch(query, extensions, maxResults, cached.fingerprint, resultText);
2064
+ }
2065
+ else {
2066
+ // Opt-in: a local model ranks the matching files so the expensive model only reads
2067
+ // the ones likely to matter. It only ever reorders and truncates with disclosure —
2068
+ // and returns null on any doubt, in which case `matches` is used untouched.
2069
+ const prefilter = await prefilterSearchMatches(query, matches);
2070
+ const grouped = groupSearchResults(prefilter.matches, truncated, undefined, workspaceRoot);
2071
+ let formatted = grouped.formatted + prefilter.note;
2072
+ try {
2073
+ const scrubber = new SemanticScrubber();
2074
+ const { maskedPrompt } = scrubber.mask(formatted);
2075
+ formatted = maskedPrompt;
2076
+ }
2077
+ catch (err) {
2078
+ logWarn("search_workspace", "SemanticScrubber failed, returning raw results");
2079
+ }
2080
+ const truncationNote = grouped.truncated ? ` (truncated at ${maxResults} matches — narrow your query or extension filter for the rest)` : "";
2081
+ // The header reports what the search actually found, not what survived the
2082
+ // prefilter — otherwise a ranked-down file would vanish from the count as well as
2083
+ // from the list, and nothing would tell the reader it ever existed.
2084
+ resultText = `Found ${matches.length} matches across ${prefilter.totalFiles} files${truncationNote}:\n\n${formatted}`;
2085
+ // A ranked-down result is never cached: it would then be replayed to a later call
2086
+ // whose local model was unavailable, silently serving a subset as if it were the
2087
+ // whole search. Repeats are handled by the output ledger below instead.
2088
+ if (!prefilter.applied) {
2089
+ setCachedSearch(query, extensions, maxResults, cached.fingerprint, resultText);
2090
+ }
2091
+ }
1880
2092
  }
1881
- catch (err) {
1882
- logWarn("search_workspace", "SemanticScrubber failed, returning raw results");
2093
+ const key = ledgerKey("search_workspace", { query, extension: extension ?? null, maxResults, cwd: workspaceRoot });
2094
+ const ledger = checkOutput(key, resultText, { label: `search for "${query}"` });
2095
+ recordLedgerOutput(key, resultText);
2096
+ if (ledger.replacement) {
2097
+ reportSavings({
2098
+ source: "contextSqueeze",
2099
+ charsBefore: resultText.length,
2100
+ charsAfter: ledger.replacement.length,
2101
+ toolName: "search_workspace",
2102
+ query: query.substring(0, 100),
2103
+ });
2104
+ return { content: [{ type: "text", text: ledger.replacement }] };
1883
2105
  }
1884
- const truncationNote = grouped.truncated ? ` (truncated at ${maxResults} matches — narrow your query or extension filter for the rest)` : "";
1885
- const resultText = `Found ${grouped.totalMatches} matches across ${grouped.filesMatched} files${truncationNote}:\n\n${formatted}`;
1886
- setCachedSearch(query, extensions, maxResults, cached.fingerprint, resultText);
1887
2106
  return {
1888
2107
  content: [
1889
2108
  {
@@ -2157,7 +2376,7 @@ function walkDirForHologram(dir, rel, extensions, stats) {
2157
2376
  const ext = entry.name.split(".").pop() || "";
2158
2377
  if (extensions.includes(ext)) {
2159
2378
  stats.filesScanned++;
2160
- allSymbols = allSymbols.concat(extractSymbolsWithTsCompiler(fullPath, relPath, stats.parseErrors));
2379
+ allSymbols = allSymbols.concat(getOrExtractSymbols(fullPath, relPath, extractSymbolsWithTsCompiler, stats.parseErrors));
2161
2380
  }
2162
2381
  }
2163
2382
  }
@@ -2428,13 +2647,16 @@ async function handlePruneConversationHistory(args) {
2428
2647
  const messages = args?.messages;
2429
2648
  if (!messages)
2430
2649
  throw new Error("messages is required");
2650
+ const force = !!args?.force;
2431
2651
  try {
2432
- const result = pruneHistoryMessages(messages);
2433
- reportSavings({
2434
- source: "historyPrune",
2435
- tokens: result.tokensSavedEstimate,
2436
- toolName: "prune_conversation_history",
2437
- });
2652
+ const result = pruneHistoryMessages(messages, { force });
2653
+ if (result.applied) {
2654
+ reportSavings({
2655
+ source: "historyPrune",
2656
+ tokens: result.tokensSavedEstimate,
2657
+ toolName: "prune_conversation_history",
2658
+ });
2659
+ }
2438
2660
  return {
2439
2661
  content: [
2440
2662
  {
@@ -2442,6 +2664,9 @@ async function handlePruneConversationHistory(args) {
2442
2664
  text: JSON.stringify({
2443
2665
  messages: result.messages,
2444
2666
  tokensSavedEstimate: result.tokensSavedEstimate,
2667
+ cacheInvalidationEstimate: result.cacheInvalidationEstimate,
2668
+ applied: result.applied,
2669
+ ...(result.reason ? { reason: result.reason } : {}),
2445
2670
  }, null, 2),
2446
2671
  },
2447
2672
  ],
@@ -3769,113 +3994,9 @@ function generateAzurePipelineYaml(threshold) {
3769
3994
  ` AZURE_DEVOPS_TOKEN: \$(AZURE_DEVOPS_TOKEN)`,
3770
3995
  ].join("\n");
3771
3996
  }
3772
- function buildDepGraphMap(workspaceRoot) {
3773
- const map = new Map();
3774
- const IGNORE_DG = new Set(["node_modules", ".git", "dist", "chroma_data", ".lemma", "build", "coverage", ".next"]);
3775
- function walkDG(dir) {
3776
- let entries = [];
3777
- try {
3778
- entries = fs.readdirSync(dir, { withFileTypes: true });
3779
- }
3780
- catch {
3781
- return;
3782
- }
3783
- for (const e of entries) {
3784
- if (IGNORE_DG.has(e.name))
3785
- continue;
3786
- const abs = path.join(dir, e.name);
3787
- if (e.isDirectory()) {
3788
- walkDG(abs);
3789
- }
3790
- else if (/\.(ts|tsx|js|jsx)$/.test(e.name) && !e.name.endsWith(".d.ts")) {
3791
- const rel = path.relative(workspaceRoot, abs).replace(/\\/g, "/");
3792
- map.set(rel, { rel, abs, imports: [], importedBy: [], externalImports: [], exports: [] });
3793
- }
3794
- }
3795
- }
3796
- walkDG(workspaceRoot);
3797
- for (const [, node] of map) {
3798
- try {
3799
- const src = fs.readFileSync(node.abs, "utf8");
3800
- const sf = ts.createSourceFile(node.abs, src, ts.ScriptTarget.Latest, true);
3801
- ts.forEachChild(sf, (n) => {
3802
- if (ts.isImportDeclaration(n) && ts.isStringLiteral(n.moduleSpecifier)) {
3803
- const spec = n.moduleSpecifier.text;
3804
- if (spec.startsWith(".")) {
3805
- const dir = path.dirname(node.abs);
3806
- let resolved = path.resolve(dir, spec);
3807
- for (const ext of [".ts", ".tsx", ".js", ".jsx", "/index.ts", "/index.tsx", "/index.js"]) {
3808
- if (fs.existsSync(resolved + ext)) {
3809
- resolved += ext;
3810
- break;
3811
- }
3812
- }
3813
- const rel = path.relative(workspaceRoot, resolved).replace(/\\/g, "/");
3814
- if (!node.imports.includes(rel))
3815
- node.imports.push(rel);
3816
- }
3817
- else {
3818
- const pkg = spec.split("/")[0];
3819
- if (!node.externalImports.includes(pkg))
3820
- node.externalImports.push(pkg);
3821
- }
3822
- }
3823
- const hasExportMod = (nd) => ts.canHaveModifiers(nd) &&
3824
- (ts.getModifiers(nd) ?? []).some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
3825
- if (hasExportMod(n)) {
3826
- if (ts.isFunctionDeclaration(n) && n.name)
3827
- node.exports.push(n.name.text);
3828
- else if (ts.isClassDeclaration(n) && n.name)
3829
- node.exports.push(n.name.text);
3830
- else if (ts.isVariableStatement(n)) {
3831
- for (const decl of n.declarationList.declarations) {
3832
- if (ts.isIdentifier(decl.name))
3833
- node.exports.push(decl.name.text);
3834
- }
3835
- }
3836
- else if (ts.isInterfaceDeclaration(n))
3837
- node.exports.push(n.name.text);
3838
- else if (ts.isTypeAliasDeclaration(n))
3839
- node.exports.push(n.name.text);
3840
- else if (ts.isEnumDeclaration(n))
3841
- node.exports.push(n.name.text);
3842
- }
3843
- if (ts.isExportDeclaration(n) && n.exportClause && ts.isNamedExports(n.exportClause)) {
3844
- for (const el of n.exportClause.elements)
3845
- node.exports.push(el.name.text);
3846
- }
3847
- });
3848
- }
3849
- catch { /* skip unparseable */ }
3850
- }
3851
- for (const [, node] of map) {
3852
- for (const imp of node.imports) {
3853
- const target = map.get(imp);
3854
- if (target && !target.importedBy.includes(node.rel))
3855
- target.importedBy.push(node.rel);
3856
- }
3857
- }
3858
- return map;
3859
- }
3860
- function bfsDepGraph(map, startRel, direction, maxDepth) {
3861
- const visited = new Map();
3862
- const queue = [{ rel: startRel, depth: 0 }];
3863
- while (queue.length > 0) {
3864
- const item = queue.shift();
3865
- if (visited.has(item.rel) || item.depth > maxDepth)
3866
- continue;
3867
- const node = map.get(item.rel);
3868
- if (!node)
3869
- continue;
3870
- visited.set(item.rel, { node, depth: item.depth });
3871
- if (item.depth < maxDepth) {
3872
- const neighbors = direction === "forward" ? node.imports : node.importedBy;
3873
- for (const n of neighbors)
3874
- queue.push({ rel: n, depth: item.depth + 1 });
3875
- }
3876
- }
3877
- return visited;
3878
- }
3997
+ // ═══════════════════════════════════════════════════════════════════════════════
3998
+ // ─── depgraph ─────────────────────────────────────────────────────────────────
3999
+ // ═══════════════════════════════════════════════════════════════════════════════
3879
4000
  function detectDepCycles(map, subset) {
3880
4001
  const cycles = [];
3881
4002
  const visited = new Set();
@@ -4080,13 +4201,46 @@ async function handleDepgraph(args) {
4080
4201
  output = formatDGText(targetNode, forwardVisited, backwardVisited, cycles, includeExternal, showReverse);
4081
4202
  }
4082
4203
  const stats = `\n\n📊 Stats: ${forwardVisited.size} nodos forward, ${backwardVisited.size} nodos reverse, depth=${depth}`;
4083
- return { content: [{ type: "text", text: output + (format === "json" ? "" : stats) }] };
4204
+ const finalText = output + (format === "json" ? "" : stats);
4205
+ const key = ledgerKey("depgraph", { file: relTarget, depth, format, showReverse, includeExternal, detectCircular, cwd: workspaceRoot });
4206
+ const ledger = checkOutput(key, finalText, { label: `depgraph for ${relTarget}` });
4207
+ recordLedgerOutput(key, finalText);
4208
+ if (ledger.replacement) {
4209
+ reportSavings({ source: "contextSqueeze", charsBefore: finalText.length, charsAfter: ledger.replacement.length, toolName: "depgraph", filePath: relTarget });
4210
+ return { content: [{ type: "text", text: ledger.replacement }] };
4211
+ }
4212
+ return { content: [{ type: "text", text: finalText }] };
4084
4213
  }
4085
4214
  catch (err) {
4086
4215
  logError("depgraph", err);
4087
4216
  return { content: [{ type: "text", text: `depgraph failed: ${err.message}` }] };
4088
4217
  }
4089
4218
  }
4219
+ async function handleAffectedTests(args) {
4220
+ const workspaceRoot = process.cwd();
4221
+ try {
4222
+ const files = Array.isArray(args?.files) ? args.files.filter((f) => typeof f === "string") : undefined;
4223
+ const result = findAffectedTests({ workspaceRoot, files, baseRef: args?.baseRef });
4224
+ const text = formatAffectedTests(result);
4225
+ // Worth recording even when it narrows nothing: the receipt is the record of what this
4226
+ // tool actually did, and "recommended the full suite" is as much a result as a subset.
4227
+ recordReceiptEvent("tool_call", "affected_tests", {
4228
+ changed: result.changed.length,
4229
+ affected: result.affected.length,
4230
+ totalTests: result.totalTests,
4231
+ narrowed: result.command !== null,
4232
+ });
4233
+ return { content: [{ type: "text", text }] };
4234
+ }
4235
+ catch (err) {
4236
+ logError("affected_tests", err);
4237
+ // Failing here must never look like "nothing is affected" — that would talk a caller
4238
+ // out of running tests at all.
4239
+ return {
4240
+ content: [{ type: "text", text: `affected_tests failed: ${err.message}\nFall back to running the full suite.` }],
4241
+ };
4242
+ }
4243
+ }
4090
4244
  // ═══════════════════════════════════════════════════════════════════════════════
4091
4245
  // ─── refactor ─────────────────────────────────────────────────────────────────
4092
4246
  // ═══════════════════════════════════════════════════════════════════════════════
@@ -4584,10 +4738,13 @@ async function handleSmartFileSlice(args) {
4584
4738
  async function handleTestOracle(args) {
4585
4739
  const command = args.command || "npm test";
4586
4740
  const workspaceRoot = process.cwd();
4587
- // Run tests, check errors
4741
+ // A failing test suite is re-run more than any other command in a session, and the
4742
+ // failure list barely moves between runs. Both branches funnel into one `resultText` so
4743
+ // the ledger can answer a repeat with a diff instead of the same stack traces again.
4744
+ let resultText;
4588
4745
  try {
4589
4746
  const out = execSync(command, { cwd: workspaceRoot, encoding: "utf8", timeout: 30000 });
4590
- return { content: [{ type: "text", text: `All tests passed!\n\nOutput:\n${out.substring(0, 1000)}` }] };
4747
+ resultText = `All tests passed!\n\nOutput:\n${out.substring(0, 1000)}`;
4591
4748
  }
4592
4749
  catch (e) {
4593
4750
  const stdout = e.stdout || "";
@@ -4597,8 +4754,19 @@ async function handleTestOracle(args) {
4597
4754
  .split("\n")
4598
4755
  .filter((l) => l.includes("fail") || l.includes("Error") || l.includes("at ") || l.includes("✗") || l.includes("Stack"));
4599
4756
  const summary = failureLines.slice(0, 80).join("\n") || `Test execution failed:\n${stdout.substring(0, 800)}\n${stderr.substring(0, 800)}`;
4600
- return { content: [{ type: "text", text: `Tests Failed. Compressed failures:\n\n${summary}` }] };
4757
+ resultText = `Tests Failed. Compressed failures:\n\n${summary}`;
4758
+ }
4759
+ const key = ledgerKey("test_oracle", { command, cwd: workspaceRoot });
4760
+ const ledger = checkOutput(key, resultText, {
4761
+ label: `\`${command}\``,
4762
+ rerunHint: "The suite did run again — this reports its result, it does not skip execution.",
4763
+ });
4764
+ recordLedgerOutput(key, resultText);
4765
+ if (ledger.replacement) {
4766
+ reportSavings({ source: "contextSqueeze", charsBefore: resultText.length, charsAfter: ledger.replacement.length, toolName: "test_oracle" });
4767
+ return { content: [{ type: "text", text: ledger.replacement }] };
4601
4768
  }
4769
+ return { content: [{ type: "text", text: resultText }] };
4602
4770
  }
4603
4771
  // ── Schema Extract ───────────────────────────────────────────────────
4604
4772
  async function handleSchemaExtract(args) {