@nxuss/lemma 1.3.3 → 1.5.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 (73) hide show
  1. package/README.md +33 -3
  2. package/bin/init.js +1 -1
  3. package/dist/cjs/mcp/prompts.d.ts +1 -1
  4. package/dist/cjs/mcp/prompts.d.ts.map +1 -1
  5. package/dist/cjs/mcp/prompts.js +4 -1
  6. package/dist/cjs/mcp/prompts.js.map +1 -1
  7. package/dist/cjs/mcp/tools.d.ts.map +1 -1
  8. package/dist/cjs/mcp/tools.js +208 -75
  9. package/dist/cjs/mcp/tools.js.map +1 -1
  10. package/dist/cjs/subconscious/TheBrainV2.d.ts +4 -1
  11. package/dist/cjs/subconscious/TheBrainV2.d.ts.map +1 -1
  12. package/dist/cjs/subconscious/TheBrainV2.js +49 -9
  13. package/dist/cjs/subconscious/TheBrainV2.js.map +1 -1
  14. package/dist/cjs/utils/CommandOutputDistiller.d.ts +75 -0
  15. package/dist/cjs/utils/CommandOutputDistiller.d.ts.map +1 -0
  16. package/dist/cjs/utils/CommandOutputDistiller.js +598 -0
  17. package/dist/cjs/utils/CommandOutputDistiller.js.map +1 -0
  18. package/dist/cjs/utils/PatchMatcher.d.ts +17 -0
  19. package/dist/cjs/utils/PatchMatcher.d.ts.map +1 -0
  20. package/dist/cjs/utils/PatchMatcher.js +210 -0
  21. package/dist/cjs/utils/PatchMatcher.js.map +1 -0
  22. package/dist/cjs/utils/ReadWorkspaceCache.d.ts +9 -0
  23. package/dist/cjs/utils/ReadWorkspaceCache.d.ts.map +1 -0
  24. package/dist/cjs/utils/ReadWorkspaceCache.js +31 -0
  25. package/dist/cjs/utils/ReadWorkspaceCache.js.map +1 -0
  26. package/dist/cjs/utils/SearchCache.d.ts +7 -0
  27. package/dist/cjs/utils/SearchCache.d.ts.map +1 -0
  28. package/dist/cjs/utils/SearchCache.js +75 -0
  29. package/dist/cjs/utils/SearchCache.js.map +1 -0
  30. package/dist/cjs/utils/StructuralSearch.d.ts +18 -0
  31. package/dist/cjs/utils/StructuralSearch.d.ts.map +1 -0
  32. package/dist/cjs/utils/StructuralSearch.js +190 -0
  33. package/dist/cjs/utils/StructuralSearch.js.map +1 -0
  34. package/dist/cjs/utils/WorkspaceSearch.d.ts +20 -0
  35. package/dist/cjs/utils/WorkspaceSearch.d.ts.map +1 -0
  36. package/dist/cjs/utils/WorkspaceSearch.js +0 -0
  37. package/dist/cjs/utils/WorkspaceSearch.js.map +1 -0
  38. package/dist/esm/mcp/prompts.d.ts +1 -1
  39. package/dist/esm/mcp/prompts.d.ts.map +1 -1
  40. package/dist/esm/mcp/prompts.js +4 -1
  41. package/dist/esm/mcp/prompts.js.map +1 -1
  42. package/dist/esm/mcp/tools.d.ts.map +1 -1
  43. package/dist/esm/mcp/tools.js +209 -76
  44. package/dist/esm/mcp/tools.js.map +1 -1
  45. package/dist/esm/subconscious/TheBrainV2.d.ts +4 -1
  46. package/dist/esm/subconscious/TheBrainV2.d.ts.map +1 -1
  47. package/dist/esm/subconscious/TheBrainV2.js +49 -9
  48. package/dist/esm/subconscious/TheBrainV2.js.map +1 -1
  49. package/dist/esm/utils/CommandOutputDistiller.d.ts +75 -0
  50. package/dist/esm/utils/CommandOutputDistiller.d.ts.map +1 -0
  51. package/dist/esm/utils/CommandOutputDistiller.js +588 -0
  52. package/dist/esm/utils/CommandOutputDistiller.js.map +1 -0
  53. package/dist/esm/utils/PatchMatcher.d.ts +17 -0
  54. package/dist/esm/utils/PatchMatcher.d.ts.map +1 -0
  55. package/dist/esm/utils/PatchMatcher.js +205 -0
  56. package/dist/esm/utils/PatchMatcher.js.map +1 -0
  57. package/dist/esm/utils/ReadWorkspaceCache.d.ts +9 -0
  58. package/dist/esm/utils/ReadWorkspaceCache.d.ts.map +1 -0
  59. package/dist/esm/utils/ReadWorkspaceCache.js +24 -0
  60. package/dist/esm/utils/ReadWorkspaceCache.js.map +1 -0
  61. package/dist/esm/utils/SearchCache.d.ts +7 -0
  62. package/dist/esm/utils/SearchCache.d.ts.map +1 -0
  63. package/dist/esm/utils/SearchCache.js +67 -0
  64. package/dist/esm/utils/SearchCache.js.map +1 -0
  65. package/dist/esm/utils/StructuralSearch.d.ts +18 -0
  66. package/dist/esm/utils/StructuralSearch.d.ts.map +1 -0
  67. package/dist/esm/utils/StructuralSearch.js +150 -0
  68. package/dist/esm/utils/StructuralSearch.js.map +1 -0
  69. package/dist/esm/utils/WorkspaceSearch.d.ts +20 -0
  70. package/dist/esm/utils/WorkspaceSearch.d.ts.map +1 -0
  71. package/dist/esm/utils/WorkspaceSearch.js +0 -0
  72. package/dist/esm/utils/WorkspaceSearch.js.map +1 -0
  73. package/package.json +1 -1
@@ -1,5 +1,5 @@
1
1
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2
- export declare const LEMMA_SYSTEM_PROMPT = "You are connected to Lemma \u2014 an intelligent local-first AI gateway.\nLemma gives you a set of tools you MUST use proactively and automatically. NEVER ask the user\nto call a tool manually. YOU invoke the tools. The user should experience zero friction.\n\n## MANDATORY AUTOMATIC BEHAVIORS\n\n### 1. Before reading ANY file \u2192 call get_ast_hologram first\nWhen you need to understand the structure of a codebase or navigate files, ALWAYS call\n`get_ast_hologram` first (with the relevant dirPath, e.g. \"src\"). Use the returned symbol\nmap to pinpoint exactly which files to read. NEVER blindly read every file one by one.\n\n### 2. Before answering ANY technical question \u2192 call query_hybrid_consensus first\nBEFORE making any reasoning or cloud LLM call, ALWAYS call `query_hybrid_consensus` with\nthe user's question. If similarity \u2265 80%, return the cached answer immediately \u2014 no cloud\ncall needed. Only proceed with your own reasoning if the Brain returns a MISS.\n\n### 3. When you start working on ANY file \u2192 call get_telepathic_hints automatically\nWhenever the user mentions a file or you are about to read/edit a file, IMMEDIATELY call\n`get_telepathic_hints` with that file's path. Surface the results to the user as relevant\ncontext BEFORE doing any work. Do not ask permission \u2014 just do it.\n\n### 4. Before applying ANY code patch \u2192 call validate_patch_sandbox first\nNEVER call `write_workspace_file` or `apply_workspace_patch` without FIRST calling\n`validate_patch_sandbox` with the proposed content. Only proceed with the write if the\nverdict is \"\u2705 SAFE TO APPLY\". If it fails, fix the issues and re-validate.\n\n### 5. After solving any technical problem \u2192 call store_memory automatically\nAfter resolving a bug, implementing a feature, or answering a significant technical question,\nALWAYS call `store_memory` to persist the solution. Use a descriptive query key so it can\nbe retrieved in future sessions across ALL projects.\n\n### 6. Workspace navigation \u2192 always use Lemma tools, NEVER guess file contents\n- Use `list_workspace_dir` to navigate structure\n- Use `read_workspace_file` (auto-compresses + scrubs secrets) instead of asking the user\n- Use `search_workspace` for text search across files\n- Use `run_workspace_command` for builds, tests, linters\n\n### 7. Privacy is automatic\n`read_workspace_file` and `search_workspace` automatically scrub API keys and PII.\nYou never need to manually sanitize file content.\n\n## LEMMA RESOURCES (auto-subscribe when available)\n- `lemma://runtime/context` \u2014 live crash logs. Read this FIRST when the user reports a bug.\n- `lemma://multiverse/timeline` \u2014 AST history. Use when the user says \"it was working before\".\n- `lemma://project/onboarding` \u2014 architecture map. Read at session start in new repos.\n- `lemma://stats/usage` \u2014 token savings report.\n\nRemember: the user hired YOU to be autonomous. Act like it.";
2
+ export declare const LEMMA_SYSTEM_PROMPT = "You are connected to Lemma \u2014 an intelligent local-first AI gateway.\nLemma gives you a set of tools you MUST use proactively and automatically. NEVER ask the user\nto call a tool manually. YOU invoke the tools. The user should experience zero friction.\n\n## MANDATORY AUTOMATIC BEHAVIORS\n\n### 1. Before reading ANY file \u2192 call get_ast_hologram first\nWhen you need to understand the structure of a codebase or navigate files, ALWAYS call\n`get_ast_hologram` first (with the relevant dirPath, e.g. \"src\"). Use the returned symbol\nmap to pinpoint exactly which files to read. NEVER blindly read every file one by one.\n\n### 2. Before answering ANY technical question \u2192 call query_hybrid_consensus first\nBEFORE making any reasoning or cloud LLM call, ALWAYS call `query_hybrid_consensus` with\nthe user's question. If similarity \u2265 80%, return the cached answer immediately \u2014 no cloud\ncall needed. Only proceed with your own reasoning if the Brain returns a MISS.\n\n### 3. When you start working on ANY file \u2192 call get_telepathic_hints automatically\nWhenever the user mentions a file or you are about to read/edit a file, IMMEDIATELY call\n`get_telepathic_hints` with that file's path. Surface the results to the user as relevant\ncontext BEFORE doing any work. Do not ask permission \u2014 just do it.\n\n### 4. Before applying ANY code patch \u2192 call validate_patch_sandbox first\nNEVER call `write_workspace_file` or `apply_workspace_patch` without FIRST calling\n`validate_patch_sandbox` with the proposed content. Only proceed with the write if the\nverdict is \"\u2705 SAFE TO APPLY\". If it fails, fix the issues and re-validate.\n\n### 5. After solving any technical problem \u2192 call store_memory automatically\nAfter resolving a bug, implementing a feature, or answering a significant technical question,\nALWAYS call `store_memory` to persist the solution. Use a descriptive query key so it can\nbe retrieved in future sessions across ALL projects.\n\n### 6. Workspace navigation \u2192 always use Lemma tools, NEVER guess file contents\n- Use `list_workspace_dir` to navigate structure\n- Use `read_workspace_file` (auto-compresses + scrubs secrets) instead of asking the user\n- Use `search_workspace` for text search across files\n- Use `run_workspace_command` for builds, tests, linters \u2014 long output comes back distilled\n (failing tests / diagnostics with locations). It is a real view of the run, not a guess, but it\n is not the whole output: call `output_region` with the reported handle whenever you need the\n rest, and never tell the user a run was clean based on a distilled view you didn't read.\n\n### 7. Privacy is automatic\n`read_workspace_file` and `search_workspace` automatically scrub API keys and PII.\nYou never need to manually sanitize file content.\n\n## LEMMA RESOURCES (auto-subscribe when available)\n- `lemma://runtime/context` \u2014 live crash logs. Read this FIRST when the user reports a bug.\n- `lemma://multiverse/timeline` \u2014 AST history. Use when the user says \"it was working before\".\n- `lemma://project/onboarding` \u2014 architecture map. Read at session start in new repos.\n- `lemma://stats/usage` \u2014 token savings report.\n\nRemember: the user hired YOU to be autonomous. Act like it.";
3
3
  export declare const LEMMA_TURBOMODE_PROMPT = "You are operating in LEMMA TURBOMODE. Your goal: multiply the user's LLM membership 50x by spending 50x fewer tokens per task. Every token counts. Every cache hit is a membership extension.\n\n## MANDATORY TURBOMODE RULES \u2014 VIOLATE THESE AND YOU WASTE THE USER'S MONEY\n\n### 0. Before ANYTHING \u2192 check token_budget\nEvery 5 turns, call `token_budget` to check your consumption. If multiplier drops below 10x, optimize harder.\n\n### 1. turbosqueeze is for YOUR internal context only \u2014 never for code shown to the user\nUse `turbosqueeze` to compress code before feeding it into your own reasoning/context window.\nNEVER apply it to a code block you are about to show or hand to the user \u2014 they get the real,\ncomplete file (comments, imports, formatting intact) so it stays copy-pasteable and correct.\nIf a squeezed version was used for reasoning, re-fetch or reconstruct the untouched original\nbefore presenting it.\n\n### 2. smarter_cache (threshold 0.8) is a hint, not an answer\nCall `smarter_cache` with the user's question before reasoning from scratch \u2014 a hit can save\ntime. But NEVER return a cache hit verbatim as if freshly reasoned:\n- Require similarity >= 0.8 (raised from 0.75 \u2014 0.75 was matching questions that weren't\n actually the same question).\n- Always tell the user the answer came from cache (e.g. \"from a cached earlier answer:\").\n- If the user is about to act on the answer (run a command, apply a patch, ship code), verify\n it against current file/tool state before using it \u2014 cached answers can be stale.\n- On a MISS, or on any doubt, reason normally.\n\n### 3. Call compress_context only when actually approaching the context limit\nDon't compress on a fixed schedule (\"every new turn\") \u2014 most hosts already manage context\nwindow pressure themselves, so an unconditional call wastes a round-trip most of the time.\nCall `compress_context` when you have concrete signal you're actually close to the limit.\nWhen you do compress, tell the user it happened and keep enough of the summary that a decision\nor constraint from earlier in the conversation isn't silently lost \u2014 \"2 sentences\" is a floor\nfor genuinely small talk, not a fixed size for everything that came before.\n\n### 4. Before reading files \u2192 call get_ast_hologram\nNEVER read files blindly. Always call `get_ast_hologram` first to locate the exact symbols you need. Then read only those specific files. Each unnecessary file read wastes 1000+ tokens.\n\n### 4b. Before re-reading a file \u2192 call file_fingerprint\nBefore calling read_workspace_file, check if the file hash has changed since last read. If status is 'unchanged', skip the read entirely. Only read when hash differs.\n\n### 4c. Before deep file analysis \u2192 use import_tree_context + type_coverage_report\nInstead of reading full files to understand architecture, call import_tree_context (import graph) and type_coverage_report (type safety). Both are zero-token tools.\n\n### 4d. Before investigating code ownership \u2192 call git_blame_heat\nFor questions about who wrote code, why it's risky, or change history, call git_blame_heat instead of reasoning. Zero LLM cost.\n\n### 5. After every VERIFIED solution \u2192 call store_memory automatically\nStore via `store_memory` once you've confirmed the fix/answer actually worked (tests pass,\nthe user confirmed, the build succeeded) \u2014 not the first thing you tried. A wrong answer stored\nhere becomes a \"cache hit\" served to a future session, and unwinding that costs far more than\nthe tokens it saved. Verified answers are still free to cache aggressively.\n\n### 6. Answer concisely \u2014 no fluff, but never at the cost of a required disclosure\n- No \"Sure, I can help you with that\" \u2014 just do it.\n- No unnecessary explanation of what you did \u2014 unless asked.\n- No markdown formatting in internal responses.\n- No repeating the user's question back to them.\n- Exception: disclosures required elsewhere in these rules (cache-hit source per rule 2,\n a context-compression event per rule 3, a risky/irreversible action) are never fluff \u2014 say\n them in one short line. Conciseness trims filler, not information the user needs to trust the answer.\n\n### 7. Use batch_tool_calls aggressively\nAny set of independent operations (read 3 files, search 2 patterns, check cache for 2 questions) \u2014 use `batch_tool_calls` to run them in parallel. Single round-trip > multiple sequential calls.\n\n### 8. Before multi-file operations \u2192 use auto_context_bundle or bulk_file_digest\nInstead of reading files one by one, use `auto_context_bundle` (one file, everything included) or `bulk_file_digest` (multiple files, one call). Both save 75-80% round-trips.\n\n### 9. Before outputting long responses \u2192 check repeat_guard\nCall `repeat_guard` with your draft output to avoid repeating yourself. Saves 10-15% of output tokens.\n\n### 9b. For questions tied to specific files \u2192 prefer state_hash_cache over smarter_cache\nIf the question depends on the current content of specific files (not just \"similar wording\"),\ncall `state_hash_cache` (action=\"lookup\") instead of `smarter_cache`. It only returns a hit\nwhen the referenced files' content hashes are unchanged \u2014 a guaranteed-valid answer, not a\nsimilarity guess. After answering, call it again with action=\"store\" to cache it. Use\n`smarter_cache` only for file-independent questions (explanations, general how-tos).\n\n### 9c. Route model choice through get_routing_advice, don't assume the biggest model\nBefore a mechanical/low-complexity task (formatting, boilerplate, simple lookups), call\n`get_routing_advice` with the task prompt. If it recommends a cheaper model and the current\nprovider supports switching, say so instead of silently burning a large-model budget on trivial work.\n\n### 9d. Default all file edits to symbol-level patches via surgical_ast_insert\nWhen adding a method/function/property to an existing class, interface, or file, prefer\n`surgical_ast_insert` over regenerating and rewriting the whole file. Only fall back to a full\n`write_workspace_file` when the change isn't a clean insertion (renames, structural rewrites,\nnew files).\n\n### 9e. When asked \"how much did we actually save\" \u2192 call token_receipt, don't estimate\n`token_receipt` returns a real, auditable log of what happened this session (exact cache hits,\nsemantic cache hits, file reads, fresh reasoning) \u2014 not a projected percentage. Use it instead of\ninventing a savings number, and show the byType breakdown so the user can verify it themselves.\n\n### 10. When resuming work \u2192 load conversation_checkpoint\nAt session start or when user says \"continuemos\", call `conversation_checkpoint` with action \"load\" to restore context without re-explaining.\n\n### 11. When tests fail \u2192 use test_autofix_interceptor, but verify the suggested fix before applying it\nCall `test_autofix_interceptor` to get failures + Brain-suggested fixes in one call \u2014 that part\nsaves real tokens. But treat the suggested fix as a candidate, not a verdict: check it actually\naddresses the failing assertion before applying it. Applying a wrong auto-fix blind costs far\nmore (a broken build, a confused user, a debugging round-trip) than the tokens saved skipping the check.\n\n### 12. For common prompt patterns \u2192 use prompt_pattern_cache\nBefore generating repetitive prompts (\"explain this\", \"review this\", \"write tests\"), check `prompt_pattern_cache` action \"find\" for cached templates.\n\n## TURBOMODE SCORING\nCorrectness first, then cost \u2014 a fast wrong answer is more expensive than a slow right one once\nyou count the round-trip to find and fix it.\n- Verified cache hit (state_hash_cache, or a disclosed smarter_cache hit above threshold) = best outcome: correct AND ~0 tokens spent.\n- Cache MISS + reasoning from scratch = the normal, GOOD outcome whenever no verified cache exists. Reasoning is not a failure mode \u2014 it's what you do when you don't already have a trustworthy answer.\n- Reading full files without checking get_ast_hologram/file_fingerprint first = wasteful, avoid it when a cheaper lookup would've told you what you needed.\n- Any answer delivered without required disclosure (rule 6's exception), or a cached/auto-fixed result used without verification = FAIL, regardless of tokens saved \u2014 it can cost the user far more than tokens once it's wrong.\n\nRemember: the goal is spending fewer tokens on the SAME quality of answer, not a lower-quality answer for fewer tokens.";
4
4
  export declare function setupPromptsHandlers(server: Server): void;
5
5
  //# sourceMappingURL=prompts.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"prompts.d.ts","sourceRoot":"","sources":["../../../src/mcp/prompts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAGnE,eAAO,MAAM,mBAAmB,m6FA+C4B,CAAC;AAE7D,eAAO,MAAM,sBAAsB,27QA+GqF,CAAC;AAEzH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAiDzD"}
1
+ {"version":3,"file":"prompts.d.ts","sourceRoot":"","sources":["../../../src/mcp/prompts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAGnE,eAAO,MAAM,mBAAmB,0uGAkD4B,CAAC;AAE7D,eAAO,MAAM,sBAAsB,27QA+GqF,CAAC;AAEzH,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAiDzD"}
@@ -34,7 +34,10 @@ be retrieved in future sessions across ALL projects.
34
34
  - Use \`list_workspace_dir\` to navigate structure
35
35
  - Use \`read_workspace_file\` (auto-compresses + scrubs secrets) instead of asking the user
36
36
  - Use \`search_workspace\` for text search across files
37
- - Use \`run_workspace_command\` for builds, tests, linters
37
+ - Use \`run_workspace_command\` for builds, tests, linters — long output comes back distilled
38
+ (failing tests / diagnostics with locations). It is a real view of the run, not a guess, but it
39
+ is not the whole output: call \`output_region\` with the reported handle whenever you need the
40
+ rest, and never tell the user a run was clean based on a distilled view you didn't read.
38
41
 
39
42
  ### 7. Privacy is automatic
40
43
  \`read_workspace_file\` and \`search_workspace\` automatically scrub API keys and PII.
@@ -1 +1 @@
1
- {"version":3,"file":"prompts.js","sourceRoot":"","sources":["../../../src/mcp/prompts.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,oCAAoC,CAAC;AAEtG,MAAM,CAAC,MAAM,mBAAmB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4DA+CyB,CAAC;AAE7D,MAAM,CAAC,MAAM,sBAAsB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wHA+GkF,CAAC;AAEzH,MAAM,UAAU,oBAAoB,CAAC,MAAc;IACjD,MAAM,CAAC,iBAAiB,CAAC,wBAAwB,EAAE,KAAK,IAAI,EAAE;QAC5D,OAAO;YACL,OAAO,EAAE;gBACP;oBACE,IAAI,EAAE,iBAAiB;oBACvB,WAAW,EAAE,+JAA+J;oBAC5K,SAAS,EAAE,EAAE;iBACd;gBACD;oBACE,IAAI,EAAE,iBAAiB;oBACvB,WAAW,EAAE,wOAAwO;oBACrP,SAAS,EAAE,EAAE;iBACd;aACF;SACF,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,iBAAiB,CAAC,sBAAsB,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;QACjE,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;YAC9C,OAAO;gBACL,WAAW,EAAE,qCAAqC;gBAClD,QAAQ,EAAE;oBACR;wBACE,IAAI,EAAE,MAAM;wBACZ,OAAO,EAAE;4BACP,IAAI,EAAE,MAAM;4BACZ,IAAI,EAAE,mBAAmB;yBAC1B;qBACF;iBACF;aACF,CAAC;QACJ,CAAC;QACD,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;YAC9C,OAAO;gBACL,WAAW,EAAE,8CAA8C;gBAC3D,QAAQ,EAAE;oBACR;wBACE,IAAI,EAAE,MAAM;wBACZ,OAAO,EAAE;4BACP,IAAI,EAAE,MAAM;4BACZ,IAAI,EAAE,sBAAsB;yBAC7B;qBACF;iBACF;aACF,CAAC;QACJ,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,mBAAmB,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;AACL,CAAC"}
1
+ {"version":3,"file":"prompts.js","sourceRoot":"","sources":["../../../src/mcp/prompts.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,oCAAoC,CAAC;AAEtG,MAAM,CAAC,MAAM,mBAAmB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4DAkDyB,CAAC;AAE7D,MAAM,CAAC,MAAM,sBAAsB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wHA+GkF,CAAC;AAEzH,MAAM,UAAU,oBAAoB,CAAC,MAAc;IACjD,MAAM,CAAC,iBAAiB,CAAC,wBAAwB,EAAE,KAAK,IAAI,EAAE;QAC5D,OAAO;YACL,OAAO,EAAE;gBACP;oBACE,IAAI,EAAE,iBAAiB;oBACvB,WAAW,EAAE,+JAA+J;oBAC5K,SAAS,EAAE,EAAE;iBACd;gBACD;oBACE,IAAI,EAAE,iBAAiB;oBACvB,WAAW,EAAE,wOAAwO;oBACrP,SAAS,EAAE,EAAE;iBACd;aACF;SACF,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,MAAM,CAAC,iBAAiB,CAAC,sBAAsB,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;QACjE,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;YAC9C,OAAO;gBACL,WAAW,EAAE,qCAAqC;gBAClD,QAAQ,EAAE;oBACR;wBACE,IAAI,EAAE,MAAM;wBACZ,OAAO,EAAE;4BACP,IAAI,EAAE,MAAM;4BACZ,IAAI,EAAE,mBAAmB;yBAC1B;qBACF;iBACF;aACF,CAAC;QACJ,CAAC;QACD,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,iBAAiB,EAAE,CAAC;YAC9C,OAAO;gBACL,WAAW,EAAE,8CAA8C;gBAC3D,QAAQ,EAAE;oBACR;wBACE,IAAI,EAAE,MAAM;wBACZ,OAAO,EAAE;4BACP,IAAI,EAAE,MAAM;4BACZ,IAAI,EAAE,sBAAsB;yBAC7B;qBACF;iBACF;aACF,CAAC;QACJ,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,mBAAmB,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../../src/mcp/tools.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAwiCnE,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,MAAM,EAAE,IAAI,GAAG,OAAO,GAAG,KAAK,GAAG,MAAM,CAAC;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,GAC1C,IAAI,CA2DN"}
1
+ {"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../../src/mcp/tools.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAukCnE,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,MAAM,EAAE,IAAI,GAAG,OAAO,GAAG,KAAK,GAAG,MAAM,CAAC;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,GAC1C,IAAI,CA2DN"}
@@ -1,7 +1,7 @@
1
1
  import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
2
2
  import fs from "fs";
3
3
  import path from "path";
4
- import { execSync } from "child_process";
4
+ import { execSync, spawnSync } from "child_process";
5
5
  import os from "os";
6
6
  import axios from "axios";
7
7
  import * as ts from "typescript";
@@ -33,6 +33,11 @@ import { surgicalASTInsert } from "../utils/SurgicalASTInsert";
33
33
  import { getInfraToolDefinitions, getInfraToolHandlers, INFRA_TOOL_NAMES } from "../infra/mcp-tools";
34
34
  import { lookupStateHash, storeStateHash } from "../utils/StateHashCache";
35
35
  import { recordReceiptEvent, getReceiptSummary } from "../utils/TokenReceipt";
36
+ import { distillCommandOutput, buildDistillFooter, readRegion } from "../utils/CommandOutputDistiller";
37
+ import { findMatch, reindentReplacement } from "../utils/PatchMatcher";
38
+ import { searchWorkspace, groupSearchResults, parseExtensionFilter } from "../utils/WorkspaceSearch";
39
+ import { checkAlreadySent, recordSent } from "../utils/ReadWorkspaceCache";
40
+ import { getCachedSearch, setCachedSearch } from "../utils/SearchCache";
36
41
  // ── Pro License Gate ──────────────────────────────────────────────────────────
37
42
  const LICENSE_FILE = path.join(os.homedir(), '.lemma-cache', 'license.json');
38
43
  let _proStatusCache = null;
@@ -89,7 +94,7 @@ const toolDefinitions = [
89
94
  },
90
95
  {
91
96
  name: "search_memory",
92
- description: "Search Lemma's semantic memory (The Brain) before answering technical questions. Retrieves past solutions, fixes, and context from ALL your projects globally. If similarity > 50%, use this answer directly.",
97
+ description: "Search Lemma's semantic memory (The Brain) before investigating something from scratch — retrieves past solutions, fixes, and context from ALL your projects globally. Results are split into 'fresh' (safe to reuse — either untracked general knowledge, or every file it depended on still hashes the same) and 'stale' (a similar question was answered before, but a tracked file changed since — re-verify against current state before reusing). Never treat a stale result as current.",
93
98
  inputSchema: {
94
99
  type: "object",
95
100
  properties: {
@@ -101,13 +106,14 @@ const toolDefinitions = [
101
106
  },
102
107
  {
103
108
  name: "store_memory",
104
- description: "Persist a technical solution, bug fix, architecture decision, or key fact into Lemma's Brain.",
109
+ description: "Persist a technical solution, bug fix, architecture decision, or key fact into Lemma's Brain — so future questions on the same topic (even phrased differently) don't require re-investigating the repo. Pass filePaths for anything derived from specific files (e.g. 'where is X implemented', 'how does Y work') so the memory auto-invalidates the moment those files change, instead of risking a stale answer being reused.",
105
110
  inputSchema: {
106
111
  type: "object",
107
112
  properties: {
108
113
  query: { type: "string", description: "The natural language query key" },
109
114
  response: { type: "string", description: "The complete technical content to memorize" },
110
115
  provider: { type: "string", description: "Optional model provider name", default: "generic" },
116
+ filePaths: { type: "array", items: { type: "string" }, description: "Paths (relative to project root) this answer depends on. If any changes later, this memory is marked stale instead of being silently reused." },
111
117
  },
112
118
  required: ["query", "response"],
113
119
  },
@@ -136,12 +142,13 @@ const toolDefinitions = [
136
142
  },
137
143
  {
138
144
  name: "read_workspace_file",
139
- description: "Read a file inside the workspace. Automatically compresses comments/whitespace and scrubs API keys.",
145
+ description: "Read a file inside the workspace. Automatically compresses comments/whitespace and scrubs API keys. If this exact file (same compact mode) was already read this session and hasn't changed on disk, returns a short UNCHANGED marker instead of resending the full content — reuse your existing copy. Pass force:true to always get the full content.",
140
146
  inputSchema: {
141
147
  type: "object",
142
148
  properties: {
143
149
  filePath: { type: "string", description: "Path relative to the project root" },
144
150
  compact: { type: "boolean", description: "Whether to compress comments and whitespace", default: true },
151
+ force: { type: "boolean", description: "Bypass the unchanged-file short-circuit and always return full content", default: false },
145
152
  },
146
153
  required: ["filePath"],
147
154
  },
@@ -172,7 +179,7 @@ const toolDefinitions = [
172
179
  },
173
180
  {
174
181
  name: "apply_workspace_patch",
175
- description: "Apply a smart search-and-replace patch to an existing file. Safe against duplicate matches.",
182
+ description: "Apply a smart search-and-replace patch to an existing file. Falls back through whitespace-normalized, indent-anchored, and fuzzy matching if the exact block isn't found — so a searchContent built from a compacted read still applies. Safe against duplicate matches. On total failure, the error includes a diff against the closest candidate block.",
176
183
  inputSchema: {
177
184
  type: "object",
178
185
  properties: {
@@ -185,15 +192,35 @@ const toolDefinitions = [
185
192
  },
186
193
  {
187
194
  name: "run_workspace_command",
188
- description: "Execute a bash command in the workspace root. Maximum execution timeout of 15 seconds.",
195
+ description: "Execute a bash command in the workspace root (15s timeout). Captures both stdout and stderr. Long test/build/lint output is distilled deterministically — for jest, vitest and tsc it returns the failing tests or diagnostics with their locations and reasons, dropping code frames and node_modules stack frames; anything else falls back to head/tail plus error-matching lines. The complete output is always stored first and the reply carries a handle: nothing is lost, use output_region to retrieve any part verbatim. Pass raw:true to skip distillation.",
189
196
  inputSchema: {
190
197
  type: "object",
191
198
  properties: {
192
199
  command: { type: "string", description: "The bash command to run" },
200
+ raw: {
201
+ type: "boolean",
202
+ description: "Return the complete untouched output instead of the distilled view",
203
+ default: false,
204
+ },
193
205
  },
194
206
  required: ["command"],
195
207
  },
196
208
  },
209
+ {
210
+ name: "output_region",
211
+ description: "Retrieve any part of a command output previously stored by run_workspace_command, verbatim. This is what makes distillation lossless — use it when the distilled view isn't enough. Pass all:true for the whole output, section for one named block (a failing test title or a file path, as listed in the distilled view), or startLine/endLine for a range.",
212
+ inputSchema: {
213
+ type: "object",
214
+ properties: {
215
+ handle: { type: "string", description: "The handle reported by run_workspace_command" },
216
+ section: { type: "string", description: "Name of a section to retrieve (failing test title or file path)" },
217
+ startLine: { type: "number", description: "First line to return (1-indexed, inclusive)" },
218
+ endLine: { type: "number", description: "Last line to return (1-indexed, inclusive)" },
219
+ all: { type: "boolean", description: "Return the complete stored output", default: false },
220
+ },
221
+ required: ["handle"],
222
+ },
223
+ },
197
224
  {
198
225
  name: "list_workspace_dir",
199
226
  description: "List files and subdirectories recursively to navigate the repository structure.",
@@ -207,12 +234,13 @@ const toolDefinitions = [
207
234
  },
208
235
  {
209
236
  name: "search_workspace",
210
- description: "Perform a fast local text search (grep) across all files in the project workspace.",
237
+ description: "Perform a fast local text search (grep) across all files in the project workspace. Respects .gitignore, skips binaries and files over 500KB, groups results by file (capped per file), and stops at maxResults. For code files (.ts/.tsx/.js/.jsx), matches inside the same function/class/method are collapsed into one entry with its signature and line range instead of raw duplicate lines. Repeating an identical query while the workspace is unchanged returns the cached result instantly instead of re-scanning.",
211
238
  inputSchema: {
212
239
  type: "object",
213
240
  properties: {
214
241
  query: { type: "string", description: "The keyword or text pattern to search for" },
215
- extension: { type: "string", description: "Optional file extension filter (e.g., 'ts', 'json')" },
242
+ extension: { type: "string", description: "Optional file extension filter, comma-separated for multiple (e.g., 'ts,tsx')" },
243
+ maxResults: { type: "number", description: "Maximum total matches to return before truncating", default: 200 },
216
244
  },
217
245
  required: ["query"],
218
246
  },
@@ -975,6 +1003,7 @@ const toolHandlers = {
975
1003
  create_workspace_file: handleCreateWorkspaceFile,
976
1004
  apply_workspace_patch: handleApplyWorkspacePatch,
977
1005
  run_workspace_command: handleRunWorkspaceCommand,
1006
+ output_region: handleOutputRegion,
978
1007
  list_workspace_dir: handleListWorkspaceDir,
979
1008
  search_workspace: handleSearchWorkspace,
980
1009
  squeeze_prompt: handleSqueezePrompt,
@@ -1113,12 +1142,27 @@ async function handleSearchMemory(args) {
1113
1142
  if (results.length === 0) {
1114
1143
  return { content: [{ type: "text", text: "No relevant memories found in Lemma's Brain." }] };
1115
1144
  }
1116
- const formatted = results
1117
- .map((r, i) => {
1118
- return `Result ${i + 1} (Similarity: ${(r.similarity * 100).toFixed(1)}%)\nPrompt: ${r.query.substring(0, 300)}...\nResponse: ${typeof r.response === "string" ? r.response : JSON.stringify(r.response, null, 2)}`;
1119
- })
1120
- .join("\n\n---\n\n");
1121
- return { content: [{ type: "text", text: `Lemma found ${results.length} memories:\n\n${formatted}` }] };
1145
+ const fresh = results.filter((r) => r.fresh);
1146
+ const stale = results.filter((r) => !r.fresh);
1147
+ const formatResult = (r, i) => `Result ${i + 1} (Similarity: ${(r.similarity * 100).toFixed(1)}%)\nPrompt: ${r.query.substring(0, 300)}...\nResponse: ${typeof r.response === "string" ? r.response : JSON.stringify(r.response, null, 2)}`;
1148
+ if (fresh.length > 0) {
1149
+ // Only a fresh hit (or an untracked, purely conceptual entry) counts as a real
1150
+ // avoided re-investigation this is the only case worth crediting as savings.
1151
+ const bestFresh = fresh[0];
1152
+ const tokensSaved = Math.max(100, Math.floor(String(bestFresh.response).length / 4));
1153
+ reportSavings({ source: "cache", tokens: tokensSaved, toolName: "search_memory", query: query.substring(0, 100) });
1154
+ }
1155
+ const parts = [];
1156
+ if (fresh.length > 0) {
1157
+ parts.push(`Lemma found ${fresh.length} fresh ${fresh.length === 1 ? "memory" : "memories"} (from cache — say so if you use this):\n\n${fresh.map(formatResult).join("\n\n---\n\n")}`);
1158
+ }
1159
+ if (stale.length > 0) {
1160
+ const staleText = stale
1161
+ .map((r, i) => `${formatResult(r, i)}\nSTALE — changed since stored: ${r.staleFiles.join(", ")}`)
1162
+ .join("\n\n---\n\n");
1163
+ parts.push(`${stale.length} more ${stale.length === 1 ? "memory is" : "memories are"} similar but STALE (underlying file(s) changed) — do not reuse verbatim, re-verify against current file state:\n\n${staleText}`);
1164
+ }
1165
+ return { content: [{ type: "text", text: parts.join("\n\n===\n\n") }] };
1122
1166
  }
1123
1167
  catch (e) {
1124
1168
  logError("search_memory", e);
@@ -1129,11 +1173,12 @@ async function handleStoreMemory(args) {
1129
1173
  const query = args?.query;
1130
1174
  const responseText = args?.response;
1131
1175
  const provider = args?.provider || "generic";
1176
+ const filePaths = Array.isArray(args?.filePaths) ? args.filePaths : undefined;
1132
1177
  if (!query || !responseText)
1133
1178
  throw new Error("Query and response are required");
1134
1179
  try {
1135
1180
  const brain = getBrain();
1136
- const storeRes = brain.store(query, responseText, provider);
1181
+ const storeRes = brain.store(query, responseText, provider, 0.92, filePaths);
1137
1182
  const tokensSaved = Math.max(100, Math.floor(responseText.length / 4));
1138
1183
  reportSavings({
1139
1184
  source: "cache",
@@ -1141,7 +1186,8 @@ async function handleStoreMemory(args) {
1141
1186
  toolName: "store_memory",
1142
1187
  query: query.substring(0, 100),
1143
1188
  });
1144
- return { content: [{ type: "text", text: `Success: Memory stored. ${storeRes.reason}` }] };
1189
+ const trackingNote = filePaths && filePaths.length > 0 ? ` Tracking freshness against ${filePaths.length} file(s) — this memory auto-invalidates if they change.` : "";
1190
+ return { content: [{ type: "text", text: `Success: Memory stored. ${storeRes.reason}${trackingNote}` }] };
1145
1191
  }
1146
1192
  catch (e) {
1147
1193
  logError("store_memory", e);
@@ -1186,6 +1232,7 @@ async function handleAutoHeal(args) {
1186
1232
  async function handleReadWorkspaceFile(args) {
1187
1233
  const filePath = args?.filePath;
1188
1234
  const compact = args?.compact !== false;
1235
+ const force = !!args?.force;
1189
1236
  if (!filePath)
1190
1237
  throw new Error("filePath is required");
1191
1238
  const workspaceRoot = process.cwd();
@@ -1193,6 +1240,24 @@ async function handleReadWorkspaceFile(args) {
1193
1240
  const { resolved } = safeResolvePath(workspaceRoot, filePath);
1194
1241
  let content = fs.readFileSync(resolved, "utf8");
1195
1242
  const originalSize = content.length;
1243
+ const dedup = checkAlreadySent(resolved, compact, content);
1244
+ if (!force && dedup.unchanged) {
1245
+ reportSavings({
1246
+ source: "contextSqueeze",
1247
+ charsBefore: originalSize,
1248
+ charsAfter: 0,
1249
+ toolName: "read_workspace_file",
1250
+ filePath,
1251
+ });
1252
+ return {
1253
+ content: [
1254
+ {
1255
+ type: "text",
1256
+ text: `UNCHANGED: ${filePath} is identical to what you already read this session (hash ${dedup.hash.slice(0, 12)}) — reuse your existing copy. Pass force:true to re-fetch anyway.`,
1257
+ },
1258
+ ],
1259
+ };
1260
+ }
1196
1261
  if (compact) {
1197
1262
  content = squeezeCode(content);
1198
1263
  if (content.length < originalSize) {
@@ -1213,6 +1278,7 @@ async function handleReadWorkspaceFile(args) {
1213
1278
  catch (err) {
1214
1279
  logWarn("read_workspace_file", "SemanticScrubber failed, returning raw content");
1215
1280
  }
1281
+ recordSent(resolved, compact, dedup.hash);
1216
1282
  return { content: [{ type: "text", text: content }] };
1217
1283
  }
1218
1284
  catch (err) {
@@ -1279,30 +1345,44 @@ async function handleApplyWorkspacePatch(args) {
1279
1345
  return { content: [{ type: "text", text: `Error: File ${filePath} does not exist.` }] };
1280
1346
  }
1281
1347
  const originalContent = fs.readFileSync(resolved, "utf8");
1282
- const index = originalContent.indexOf(searchContent);
1283
- if (index === -1) {
1284
- return {
1285
- content: [
1286
- {
1287
- type: "text",
1288
- text: `Error: Target search content not found in file. Make sure the search content matches exactly, including indentation, tabs, spaces, and newlines.`,
1289
- },
1290
- ],
1291
- };
1292
- }
1293
- if (originalContent.indexOf(searchContent, index + 1) !== -1) {
1348
+ const matchResult = findMatch(originalContent, searchContent);
1349
+ if (!Array.isArray(matchResult)) {
1350
+ if (matchResult.reason === "ambiguous") {
1351
+ return {
1352
+ content: [
1353
+ {
1354
+ type: "text",
1355
+ text: `Error: Multiple matches found for the search content. Please provide a larger search block with unique surrounding context lines to make it unambiguous.`,
1356
+ },
1357
+ ],
1358
+ };
1359
+ }
1360
+ if (matchResult.candidateDiff) {
1361
+ return {
1362
+ content: [
1363
+ {
1364
+ type: "text",
1365
+ text: `Error: No match found for the search content. Closest candidate starts at line ${matchResult.candidateStartLine} (- = your searchContent, + = actual file content):\n\n${matchResult.candidateDiff}`,
1366
+ },
1367
+ ],
1368
+ };
1369
+ }
1294
1370
  return {
1295
1371
  content: [
1296
1372
  {
1297
1373
  type: "text",
1298
- text: `Error: Multiple matches found for the search content. Please provide a larger search block with unique surrounding context lines to make it unambiguous.`,
1374
+ text: `Error: Target search content not found in file, and no similar block was found to diff against.`,
1299
1375
  },
1300
1376
  ],
1301
1377
  };
1302
1378
  }
1303
- const updatedContent = originalContent.replace(searchContent, replaceContent);
1304
- fs.writeFileSync(resolved, updatedContent, "utf8");
1305
- return { content: [{ type: "text", text: `Success: Patch successfully applied to ${filePath}` }] };
1379
+ const match = matchResult[0];
1380
+ const contentLines = originalContent.split("\n");
1381
+ const finalReplace = match.strategy === "exact" ? replaceContent : reindentReplacement(replaceContent, searchContent, contentLines[match.startLine]);
1382
+ const updatedLines = [...contentLines.slice(0, match.startLine), ...finalReplace.split("\n"), ...contentLines.slice(match.endLine + 1)];
1383
+ fs.writeFileSync(resolved, updatedLines.join("\n"), "utf8");
1384
+ const note = match.strategy === "exact" ? "" : ` (matched via ${match.strategy}, score ${match.score.toFixed(2)})`;
1385
+ return { content: [{ type: "text", text: `Success: Patch successfully applied to ${filePath}${note}` }] };
1306
1386
  }
1307
1387
  catch (err) {
1308
1388
  logError("apply_workspace_patch", err);
@@ -1338,25 +1418,95 @@ async function handleRunWorkspaceCommand(args) {
1338
1418
  // ── End allowlist check ────────────────────────────────────────────────
1339
1419
  const workspaceRoot = process.cwd();
1340
1420
  try {
1341
- const output = execSync(command, {
1421
+ // spawnSync rather than execSync: execSync leaves stderr attached to the parent unless
1422
+ // stdio is overridden, which silently dropped the output of every tool that reports on
1423
+ // stderr (jest and tsc both do) on a successful run. It also gives us stdout, stderr and
1424
+ // the exit code through one code path instead of a throw-on-failure split.
1425
+ const result = spawnSync(command, {
1342
1426
  cwd: workspaceRoot,
1427
+ shell: true,
1343
1428
  encoding: "utf8",
1344
1429
  timeout: 15000,
1430
+ maxBuffer: 20 * 1024 * 1024,
1345
1431
  env: { ...process.env },
1346
1432
  });
1347
- return { content: [{ type: "text", text: `Command completed successfully.\n\nOutput:\n${output}` }] };
1433
+ if (result.error) {
1434
+ const timedOut = result.error.code === "ETIMEDOUT";
1435
+ return {
1436
+ content: [
1437
+ {
1438
+ type: "text",
1439
+ text: timedOut
1440
+ ? `Command timed out after 15s: ${command}`
1441
+ : `Command could not be executed: ${result.error.message}`,
1442
+ },
1443
+ ],
1444
+ };
1445
+ }
1446
+ const stdout = result.stdout || "";
1447
+ const stderr = result.stderr || "";
1448
+ const combined = [stdout, stderr].filter((s) => s.trim().length > 0).join("\n");
1449
+ const status = result.status ?? 0;
1450
+ const header = status === 0 ? "Command completed successfully." : `Command failed with exit code ${status}.`;
1451
+ if (!combined.trim()) {
1452
+ return { content: [{ type: "text", text: `${header}\n\n(no output)` }] };
1453
+ }
1454
+ const distilled = distillCommandOutput(combined, { command, raw: args?.raw === true });
1455
+ if (distilled.handle) {
1456
+ recordReceiptEvent("tool_call", `run_workspace_command distilled (${distilled.format})`, {
1457
+ command,
1458
+ originalChars: distilled.originalChars,
1459
+ distilledChars: distilled.distilledChars,
1460
+ handle: distilled.handle,
1461
+ });
1462
+ }
1463
+ return {
1464
+ content: [
1465
+ { type: "text", text: `${header}\n\nOutput:\n${distilled.text}${buildDistillFooter(distilled)}` },
1466
+ ],
1467
+ };
1348
1468
  }
1349
1469
  catch (err) {
1350
1470
  return {
1351
1471
  content: [
1352
1472
  {
1353
1473
  type: "text",
1354
- text: `Command failed with exit code ${err.status || "unknown"}.\n\nStdout:\n${err.stdout || ""}\n\nStderr:\n${err.stderr || err.message || ""}`,
1474
+ text: `Command could not be executed: ${err?.message || String(err)}`,
1355
1475
  },
1356
1476
  ],
1357
1477
  };
1358
1478
  }
1359
1479
  }
1480
+ async function handleOutputRegion(args) {
1481
+ const handle = args?.handle;
1482
+ if (!handle)
1483
+ throw new Error("handle is required");
1484
+ const region = readRegion({
1485
+ handle,
1486
+ section: args?.section,
1487
+ startLine: args?.startLine,
1488
+ endLine: args?.endLine,
1489
+ all: args?.all === true,
1490
+ });
1491
+ if (!region.found) {
1492
+ const parts = [region.error ?? `Could not read region for handle "${handle}".`];
1493
+ if (region.availableSections?.length) {
1494
+ parts.push("", "Available sections:", ...region.availableSections.map((s) => ` ${s}`));
1495
+ }
1496
+ return { content: [{ type: "text", text: parts.join("\n") }] };
1497
+ }
1498
+ const scope = region.startLine === 1 && region.endLine === region.totalLines
1499
+ ? `complete output (${region.totalLines} lines)`
1500
+ : `lines ${region.startLine}-${region.endLine} of ${region.totalLines}`;
1501
+ return {
1502
+ content: [
1503
+ {
1504
+ type: "text",
1505
+ text: `Raw output for \`${region.command ?? "?"}\` — ${scope}, verbatim:\n\n${region.text}`,
1506
+ },
1507
+ ],
1508
+ };
1509
+ }
1360
1510
  function listDirRecursive(currentDir, relativePath, depth, maxDepth) {
1361
1511
  if (depth > maxDepth)
1362
1512
  return [];
@@ -1403,53 +1553,28 @@ async function handleListWorkspaceDir(args) {
1403
1553
  return { content: [{ type: "text", text: `Error listing directory: ${err.message}` }] };
1404
1554
  }
1405
1555
  }
1406
- function searchDirRecursive(currentDir, relativePath, query, extFilter) {
1407
- try {
1408
- const entries = fs.readdirSync(currentDir, { withFileTypes: true });
1409
- let result = [];
1410
- for (const entry of entries) {
1411
- if (["node_modules", ".git", "dist", "chroma_data", ".lemma"].includes(entry.name))
1412
- continue;
1413
- const rel = relativePath ? path.join(relativePath, entry.name) : entry.name;
1414
- const full = path.join(currentDir, entry.name);
1415
- if (entry.isDirectory()) {
1416
- result.push(...searchDirRecursive(full, rel, query, extFilter));
1417
- }
1418
- else {
1419
- if (extFilter && !entry.name.endsWith(`.${extFilter}`))
1420
- continue;
1421
- try {
1422
- const content = fs.readFileSync(full, "utf8");
1423
- if (content.toLowerCase().includes(query.toLowerCase())) {
1424
- const lines = content.split("\n");
1425
- lines.forEach((lineText, idx) => {
1426
- if (lineText.toLowerCase().includes(query.toLowerCase())) {
1427
- result.push({ filePath: rel, line: idx + 1, text: lineText.trim() });
1428
- }
1429
- });
1430
- }
1431
- }
1432
- catch { }
1433
- }
1434
- }
1435
- return result;
1436
- }
1437
- catch {
1438
- return [];
1439
- }
1440
- }
1441
1556
  async function handleSearchWorkspace(args) {
1442
1557
  const query = args?.query;
1443
1558
  const extension = args?.extension;
1559
+ const maxResults = args?.maxResults || 200;
1444
1560
  if (!query)
1445
1561
  throw new Error("query is required");
1446
1562
  const workspaceRoot = process.cwd();
1447
1563
  try {
1448
- const results = searchDirRecursive(workspaceRoot, "", query, extension);
1449
- if (results.length === 0) {
1450
- return { content: [{ type: "text", text: `No matches found in the workspace for "${query}".` }] };
1451
- }
1452
- let formatted = results.map((r) => `[${r.filePath}:${r.line}] ${r.text}`).join("\n");
1564
+ const extensions = parseExtensionFilter(extension);
1565
+ const cached = getCachedSearch(workspaceRoot, query, extensions, maxResults);
1566
+ if (cached.text !== null) {
1567
+ reportSavings({ source: "cache", tokens: Math.floor(cached.text.length / 4), toolName: "search_workspace", query: query.substring(0, 100) });
1568
+ return { content: [{ type: "text", text: cached.text }] };
1569
+ }
1570
+ const { matches, truncated } = searchWorkspace(workspaceRoot, query, extensions, maxResults);
1571
+ if (matches.length === 0) {
1572
+ const emptyText = `No matches found in the workspace for "${query}".`;
1573
+ setCachedSearch(query, extensions, maxResults, cached.fingerprint, emptyText);
1574
+ return { content: [{ type: "text", text: emptyText }] };
1575
+ }
1576
+ const grouped = groupSearchResults(matches, truncated, undefined, workspaceRoot);
1577
+ let formatted = grouped.formatted;
1453
1578
  try {
1454
1579
  const scrubber = new SemanticScrubber();
1455
1580
  const { maskedPrompt } = scrubber.mask(formatted);
@@ -1458,8 +1583,16 @@ async function handleSearchWorkspace(args) {
1458
1583
  catch (err) {
1459
1584
  logWarn("search_workspace", "SemanticScrubber failed, returning raw results");
1460
1585
  }
1586
+ const truncationNote = grouped.truncated ? ` (truncated at ${maxResults} matches — narrow your query or extension filter for the rest)` : "";
1587
+ const resultText = `Found ${grouped.totalMatches} matches across ${grouped.filesMatched} files${truncationNote}:\n\n${formatted}`;
1588
+ setCachedSearch(query, extensions, maxResults, cached.fingerprint, resultText);
1461
1589
  return {
1462
- content: [{ type: "text", text: `Found ${results.length} matches:\n\n${formatted}` }],
1590
+ content: [
1591
+ {
1592
+ type: "text",
1593
+ text: resultText,
1594
+ },
1595
+ ],
1463
1596
  };
1464
1597
  }
1465
1598
  catch (err) {