@nxuss/lemma 1.5.0 → 1.7.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 (94) hide show
  1. package/README.md +182 -6
  2. package/bin/checkpoint.js +49 -0
  3. package/bin/init.js +21 -2
  4. package/dist/cjs/cli/lemma-proxy.d.ts.map +1 -1
  5. package/dist/cjs/cli/lemma-proxy.js +50 -6
  6. package/dist/cjs/cli/lemma-proxy.js.map +1 -1
  7. package/dist/cjs/mcp/index.js +23 -0
  8. package/dist/cjs/mcp/index.js.map +1 -1
  9. package/dist/cjs/mcp/prompts.d.ts +1 -2
  10. package/dist/cjs/mcp/prompts.d.ts.map +1 -1
  11. package/dist/cjs/mcp/prompts.js +34 -129
  12. package/dist/cjs/mcp/prompts.js.map +1 -1
  13. package/dist/cjs/mcp/resources.d.ts.map +1 -1
  14. package/dist/cjs/mcp/resources.js +4 -4
  15. package/dist/cjs/mcp/resources.js.map +1 -1
  16. package/dist/cjs/mcp/tool-profiles.d.ts +55 -0
  17. package/dist/cjs/mcp/tool-profiles.d.ts.map +1 -0
  18. package/dist/cjs/mcp/tool-profiles.js +204 -0
  19. package/dist/cjs/mcp/tool-profiles.js.map +1 -0
  20. package/dist/cjs/mcp/tools.d.ts +14 -0
  21. package/dist/cjs/mcp/tools.d.ts.map +1 -1
  22. package/dist/cjs/mcp/tools.js +190 -27
  23. package/dist/cjs/mcp/tools.js.map +1 -1
  24. package/dist/cjs/subconscious/TheBrainV2.d.ts +36 -2
  25. package/dist/cjs/subconscious/TheBrainV2.d.ts.map +1 -1
  26. package/dist/cjs/subconscious/TheBrainV2.js +104 -23
  27. package/dist/cjs/subconscious/TheBrainV2.js.map +1 -1
  28. package/dist/cjs/utils/AutoContextBundle.d.ts.map +1 -1
  29. package/dist/cjs/utils/AutoContextBundle.js +17 -20
  30. package/dist/cjs/utils/AutoContextBundle.js.map +1 -1
  31. package/dist/cjs/utils/ContextSqueezer.d.ts.map +1 -1
  32. package/dist/cjs/utils/ContextSqueezer.js +5 -3
  33. package/dist/cjs/utils/ContextSqueezer.js.map +1 -1
  34. package/dist/cjs/utils/ConversationCheckpoint.d.ts +13 -0
  35. package/dist/cjs/utils/ConversationCheckpoint.d.ts.map +1 -1
  36. package/dist/cjs/utils/ConversationCheckpoint.js +58 -0
  37. package/dist/cjs/utils/ConversationCheckpoint.js.map +1 -1
  38. package/dist/cjs/utils/SavingsLedger.d.ts +38 -1
  39. package/dist/cjs/utils/SavingsLedger.d.ts.map +1 -1
  40. package/dist/cjs/utils/SavingsLedger.js +89 -13
  41. package/dist/cjs/utils/SavingsLedger.js.map +1 -1
  42. package/dist/cjs/utils/TestAutofixInterceptor.d.ts.map +1 -1
  43. package/dist/cjs/utils/TestAutofixInterceptor.js +15 -26
  44. package/dist/cjs/utils/TestAutofixInterceptor.js.map +1 -1
  45. package/dist/cjs/utils/reportSavings.d.ts +15 -0
  46. package/dist/cjs/utils/reportSavings.d.ts.map +1 -1
  47. package/dist/cjs/utils/reportSavings.js +13 -1
  48. package/dist/cjs/utils/reportSavings.js.map +1 -1
  49. package/dist/esm/cli/lemma-proxy.d.ts.map +1 -1
  50. package/dist/esm/cli/lemma-proxy.js +50 -6
  51. package/dist/esm/cli/lemma-proxy.js.map +1 -1
  52. package/dist/esm/mcp/index.js +24 -1
  53. package/dist/esm/mcp/index.js.map +1 -1
  54. package/dist/esm/mcp/prompts.d.ts +1 -2
  55. package/dist/esm/mcp/prompts.d.ts.map +1 -1
  56. package/dist/esm/mcp/prompts.js +33 -128
  57. package/dist/esm/mcp/prompts.js.map +1 -1
  58. package/dist/esm/mcp/resources.d.ts.map +1 -1
  59. package/dist/esm/mcp/resources.js +4 -4
  60. package/dist/esm/mcp/resources.js.map +1 -1
  61. package/dist/esm/mcp/tool-profiles.d.ts +55 -0
  62. package/dist/esm/mcp/tool-profiles.d.ts.map +1 -0
  63. package/dist/esm/mcp/tool-profiles.js +196 -0
  64. package/dist/esm/mcp/tool-profiles.js.map +1 -0
  65. package/dist/esm/mcp/tools.d.ts +14 -0
  66. package/dist/esm/mcp/tools.d.ts.map +1 -1
  67. package/dist/esm/mcp/tools.js +188 -29
  68. package/dist/esm/mcp/tools.js.map +1 -1
  69. package/dist/esm/subconscious/TheBrainV2.d.ts +36 -2
  70. package/dist/esm/subconscious/TheBrainV2.d.ts.map +1 -1
  71. package/dist/esm/subconscious/TheBrainV2.js +103 -23
  72. package/dist/esm/subconscious/TheBrainV2.js.map +1 -1
  73. package/dist/esm/utils/AutoContextBundle.d.ts.map +1 -1
  74. package/dist/esm/utils/AutoContextBundle.js +17 -20
  75. package/dist/esm/utils/AutoContextBundle.js.map +1 -1
  76. package/dist/esm/utils/ContextSqueezer.d.ts.map +1 -1
  77. package/dist/esm/utils/ContextSqueezer.js +5 -3
  78. package/dist/esm/utils/ContextSqueezer.js.map +1 -1
  79. package/dist/esm/utils/ConversationCheckpoint.d.ts +13 -0
  80. package/dist/esm/utils/ConversationCheckpoint.d.ts.map +1 -1
  81. package/dist/esm/utils/ConversationCheckpoint.js +57 -0
  82. package/dist/esm/utils/ConversationCheckpoint.js.map +1 -1
  83. package/dist/esm/utils/SavingsLedger.d.ts +38 -1
  84. package/dist/esm/utils/SavingsLedger.d.ts.map +1 -1
  85. package/dist/esm/utils/SavingsLedger.js +89 -13
  86. package/dist/esm/utils/SavingsLedger.js.map +1 -1
  87. package/dist/esm/utils/TestAutofixInterceptor.d.ts.map +1 -1
  88. package/dist/esm/utils/TestAutofixInterceptor.js +15 -26
  89. package/dist/esm/utils/TestAutofixInterceptor.js.map +1 -1
  90. package/dist/esm/utils/reportSavings.d.ts +15 -0
  91. package/dist/esm/utils/reportSavings.d.ts.map +1 -1
  92. package/dist/esm/utils/reportSavings.js +12 -1
  93. package/dist/esm/utils/reportSavings.js.map +1 -1
  94. package/package.json +4 -2
@@ -0,0 +1,204 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.REVIEW_TOOL_NAMES = exports.CORE_TOOL_NAMES = void 0;
7
+ exports.resolveToolSurface = resolveToolSurface;
8
+ exports.buildToolboxCatalog = buildToolboxCatalog;
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const path_1 = __importDefault(require("path"));
11
+ /**
12
+ * Tools that pay for their schema in an ordinary coding session: reading, searching,
13
+ * patching, AST context, exact-hash caching, and the ledger. Everything else is
14
+ * situational and reachable through `lemma_toolbox`.
15
+ */
16
+ exports.CORE_TOOL_NAMES = [
17
+ // Workspace read & navigation
18
+ "read_workspace_file",
19
+ "list_workspace_dir",
20
+ "search_workspace",
21
+ "smart_file_slice",
22
+ "run_workspace_command",
23
+ "output_region",
24
+ // Write path — patch, then validate before it lands
25
+ "apply_workspace_patch",
26
+ "write_workspace_file",
27
+ "create_workspace_file",
28
+ "surgical_ast_insert",
29
+ "validate_patch_sandbox",
30
+ // Structure without reading whole files
31
+ "get_ast_hologram",
32
+ "get_symbol_surgical_context",
33
+ "get_project_onboarding",
34
+ // Avoid re-deriving what was already derived
35
+ "state_hash_cache",
36
+ "smarter_cache",
37
+ "search_memory",
38
+ "store_memory",
39
+ // Accounting & routing
40
+ "token_receipt",
41
+ "get_routing_advice",
42
+ ];
43
+ const exists = (cwd, ...rel) => rel.some((r) => {
44
+ try {
45
+ return fs_1.default.existsSync(path_1.default.join(cwd, r));
46
+ }
47
+ catch {
48
+ return false;
49
+ }
50
+ });
51
+ /** Shallow scan for an extension — deep walks at startup would cost more than they save. */
52
+ function hasExtension(cwd, ext, depth = 2) {
53
+ const walk = (dir, left) => {
54
+ let entries;
55
+ try {
56
+ entries = fs_1.default.readdirSync(dir, { withFileTypes: true });
57
+ }
58
+ catch {
59
+ return false;
60
+ }
61
+ for (const entry of entries) {
62
+ if (entry.name.startsWith(".") || entry.name === "node_modules")
63
+ continue;
64
+ if (entry.isFile() && entry.name.endsWith(ext))
65
+ return true;
66
+ if (entry.isDirectory() && left > 0 && walk(path_1.default.join(dir, entry.name), left - 1))
67
+ return true;
68
+ }
69
+ return false;
70
+ };
71
+ return walk(cwd, depth);
72
+ }
73
+ const TOOL_GROUPS = [
74
+ {
75
+ name: "infra",
76
+ tools: [
77
+ "infra_discover", "infra_connect", "infra_status", "infra_health_check",
78
+ "infra_diagnose", "infra_analyze_monolith", "infra_suggest_split",
79
+ "jenkins_generate_pipeline", "jenkins_trigger_build", "jenkins_build_status", "jenkins_logs",
80
+ "okd_generate_manifests", "okd_apply", "okd_status", "okd_logs", "okd_events",
81
+ "okd_describe", "okd_route_url",
82
+ "terraform_plan", "terraform_apply", "terraform_state_list", "terraform_drift",
83
+ ],
84
+ applies: (cwd) =>
85
+ // A provider already connected, or the repo actually contains infra to act on.
86
+ exists(process.env.HOME || "", ".lemma-cache/infra/config.json") ||
87
+ exists(cwd, "Jenkinsfile", "kustomization.yaml", ".openshift", "openshift") ||
88
+ hasExtension(cwd, ".tf", 2),
89
+ },
90
+ ];
91
+ /**
92
+ * Code review is a mode the user enters, not a property of the repo — and it cannot be
93
+ * detected at startup. Gating these on `.git` advertised them in every git repo, which is
94
+ * every repo: ~670 tokens per turn for tools most sessions never touch. They stay
95
+ * registered and `lemma_toolbox` finds them by query the moment a review actually starts.
96
+ */
97
+ exports.REVIEW_TOOL_NAMES = ["review_diff", "review_pr", "pr_status", "generate_pr_workflow"];
98
+ function readConfiguredProfile(cwd) {
99
+ try {
100
+ const configPath = path_1.default.join(cwd, "lemma.config.json");
101
+ if (fs_1.default.existsSync(configPath)) {
102
+ const config = JSON.parse(fs_1.default.readFileSync(configPath, "utf8"));
103
+ const profile = config?.mcp?.toolProfile;
104
+ if (profile === "core" || profile === "full" || profile === "auto")
105
+ return profile;
106
+ }
107
+ }
108
+ catch {
109
+ // Unreadable config must never change which tools work — fall through to the default.
110
+ }
111
+ const env = process.env.LEMMA_TOOL_PROFILE;
112
+ if (env === "core" || env === "full" || env === "auto")
113
+ return env;
114
+ return "auto";
115
+ }
116
+ function resolveToolSurface(allDefinitions, cwd = process.cwd()) {
117
+ const profile = readConfiguredProfile(cwd);
118
+ if (profile === "full") {
119
+ return { profile, advertised: allDefinitions, hidden: [], activeGroups: ["*"] };
120
+ }
121
+ // Always advertised: it is the only route back to a withheld schema, so hiding it
122
+ // would turn a deferred tool into a lost one.
123
+ const allowed = new Set([...exports.CORE_TOOL_NAMES, "lemma_toolbox"]);
124
+ const activeGroups = [];
125
+ if (profile === "auto") {
126
+ for (const group of TOOL_GROUPS) {
127
+ let matched = false;
128
+ try {
129
+ matched = group.applies(cwd);
130
+ }
131
+ catch {
132
+ // Detection must never be the reason a tool disappears; on error, include it.
133
+ matched = true;
134
+ }
135
+ if (matched) {
136
+ activeGroups.push(group.name);
137
+ group.tools.forEach((t) => allowed.add(t));
138
+ }
139
+ }
140
+ }
141
+ const advertised = [];
142
+ const hidden = [];
143
+ for (const def of allDefinitions) {
144
+ (allowed.has(def.name) ? advertised : hidden).push(def);
145
+ }
146
+ return { profile, advertised, hidden, activeGroups };
147
+ }
148
+ /**
149
+ * One-line-per-tool catalog of everything not advertised, so the model can find and
150
+ * invoke a hidden tool without its schema having been resident all session.
151
+ */
152
+ function buildToolboxCatalog(hidden, query) {
153
+ const q = (query || "").trim().toLowerCase();
154
+ const terms = q ? q.split(/\s+/).filter(Boolean) : [];
155
+ let matches = hidden;
156
+ if (terms.length) {
157
+ // Plain substring matching floods the result: "pr" hits scrub_privacy, precrime_static
158
+ // and compress_context, so a 3-word query returns the whole catalog and costs more
159
+ // than the schemas it was meant to replace. Match on word boundaries and rank by how
160
+ // many distinct terms a tool hits, so the tools the user meant come first.
161
+ const scored = hidden
162
+ .map((t) => {
163
+ const haystack = `${t.name} ${t.description || ""}`.toLowerCase();
164
+ const words = new Set(haystack.split(/[^a-z0-9]+/).filter(Boolean));
165
+ let score = 0;
166
+ for (const term of terms) {
167
+ if (words.has(term))
168
+ score += 2; // whole word — the strong signal
169
+ else if (term.length >= 4 && haystack.includes(term))
170
+ score += 1; // prefix/partial
171
+ }
172
+ return { tool: t, score };
173
+ })
174
+ .filter((s) => s.score > 0)
175
+ .sort((a, b) => b.score - a.score);
176
+ // A whole-word hit is a real match; partials only get shown when nothing matched a
177
+ // whole word, so a weak substring never crowds out the tool the user asked for.
178
+ const hasWordHit = scored.some((s) => s.score >= 2);
179
+ matches = scored.filter((s) => (hasWordHit ? s.score >= 2 : true)).map((s) => s.tool);
180
+ }
181
+ const CATALOG_LIMIT = 15;
182
+ const truncated = Math.max(0, matches.length - CATALOG_LIMIT);
183
+ const shown = matches.slice(0, CATALOG_LIMIT);
184
+ if (matches.length === 0) {
185
+ return terms.length
186
+ ? `No hidden Lemma tool matches "${query}". ${hidden.length} tools available; call lemma_toolbox with no query to list all.`
187
+ : "No hidden tools — every Lemma tool is already advertised in this session.";
188
+ }
189
+ const lines = shown.map((t) => {
190
+ // One line per tool: enough to choose, not enough to be a second schema listing.
191
+ const desc = (t.description || "").split(/\.\s|\n/)[0].slice(0, 100);
192
+ return `- ${t.name}: ${desc}`;
193
+ });
194
+ return [
195
+ `${matches.length} Lemma tool(s) available but not advertised (schemas withheld to keep per-turn context small).`,
196
+ `Invoke one with: lemma_toolbox { action: "call", tool: "<name>", args: { ... } }`,
197
+ "",
198
+ ...lines,
199
+ ...(truncated > 0 ? [`… and ${truncated} more — narrow the query to see them.`] : []),
200
+ "",
201
+ `Call lemma_toolbox { action: "schema", tool: "<name>" } for a tool's full input schema before calling it.`,
202
+ ].join("\n");
203
+ }
204
+ //# sourceMappingURL=tool-profiles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-profiles.js","sourceRoot":"","sources":["../../../src/mcp/tool-profiles.ts"],"names":[],"mappings":";;;;;;AAoJA,gDAsCC;AAMD,kDA0DC;AA1PD,4CAAoB;AACpB,gDAAwB;AAsBxB;;;;GAIG;AACU,QAAA,eAAe,GAAa;IACvC,8BAA8B;IAC9B,qBAAqB;IACrB,o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CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IACxF,CAAC;IAED,MAAM,aAAa,GAAG,EAAE,CAAC;IACzB,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,GAAG,aAAa,CAAC,CAAC;IAC9D,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC;IAE9C,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC,MAAM;YACjB,CAAC,CAAC,iCAAiC,KAAK,MAAM,MAAM,CAAC,MAAM,iEAAiE;YAC5H,CAAC,CAAC,2EAA2E,CAAC;IAClF,CAAC;IAED,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;QAC5B,iFAAiF;QACjF,MAAM,IAAI,GAAG,CAAC,CAAC,CAAC,WAAW,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;QACrE,OAAO,KAAK,CAAC,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;IAChC,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,GAAG,OAAO,CAAC,MAAM,gGAAgG;QACjH,kFAAkF;QAClF,EAAE;QACF,GAAG,KAAK;QACR,GAAG,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,SAAS,uCAAuC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACrF,EAAE;QACF,2GAA2G;KAC5G,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACf,CAAC"}
@@ -1,4 +1,18 @@
1
1
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2
+ /** Serialized size of every tool schema sent to the client, for cost accounting. */
3
+ export declare function getToolSchemaChars(): number;
4
+ /** Every tool name with a registered handler, advertised or not. */
5
+ export declare function getRegisteredToolNames(): string[];
6
+ /** Tool names whose schemas this session sends to the client. */
7
+ export declare function getAdvertisedToolNames(): string[];
8
+ /** Which tools this session advertises, and why — surfaced by `token_budget`. */
9
+ export declare function getToolSurfaceSummary(): {
10
+ profile: string;
11
+ advertised: number;
12
+ hidden: number;
13
+ total: number;
14
+ activeGroups: string[];
15
+ };
2
16
  export interface ToolCallEvent {
3
17
  tool: string;
4
18
  args: Record<string, any>;
@@ -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;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
+ {"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../../src/mcp/tools.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AA2hCnE,oFAAoF;AACpF,wBAAgB,kBAAkB,IAAI,MAAM,CAM3C;AAED,oEAAoE;AACpE,wBAAgB,sBAAsB,IAAI,MAAM,EAAE,CAEjD;AAED,iEAAiE;AACjE,wBAAgB,sBAAsB,IAAI,MAAM,EAAE,CAEjD;AAED,iFAAiF;AACjF,wBAAgB,qBAAqB,IAAI;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,EAAE,CAAC;CACxB,CAQA;AAoHD,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,CA+DN"}
@@ -36,6 +36,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
36
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.getToolSchemaChars = getToolSchemaChars;
40
+ exports.getRegisteredToolNames = getRegisteredToolNames;
41
+ exports.getAdvertisedToolNames = getAdvertisedToolNames;
42
+ exports.getToolSurfaceSummary = getToolSurfaceSummary;
39
43
  exports.setupToolsHandlers = setupToolsHandlers;
40
44
  const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
41
45
  const fs_1 = __importDefault(require("fs"));
@@ -70,6 +74,7 @@ const WormholeCompressor_1 = require("../utils/WormholeCompressor");
70
74
  const ExecutiveReport_1 = require("../utils/ExecutiveReport");
71
75
  const SurgicalASTInsert_1 = require("../utils/SurgicalASTInsert");
72
76
  const mcp_tools_1 = require("../infra/mcp-tools");
77
+ const tool_profiles_1 = require("./tool-profiles");
73
78
  const StateHashCache_1 = require("../utils/StateHashCache");
74
79
  const TokenReceipt_1 = require("../utils/TokenReceipt");
75
80
  const CommandOutputDistiller_1 = require("../utils/CommandOutputDistiller");
@@ -101,6 +106,8 @@ const FREE_TOOLS = new Set([
101
106
  "token_budget", "squeeze_prompt", "turbosqueeze",
102
107
  // Utility — just enough to function
103
108
  "read_workspace_file", "scrub_privacy", "summarize_long_text",
109
+ // Discovery of withheld schemas — gating this would hide tools with no way back
110
+ "lemma_toolbox",
104
111
  // Phase 1 zero-cost analysis tools
105
112
  "file_fingerprint", "git_blame_heat", "type_coverage_report", "import_tree_context",
106
113
  // Phase 2 smart reading & dedup tools
@@ -1029,8 +1036,59 @@ const toolDefinitions = [
1029
1036
  ];
1030
1037
  // ── Infrastructure Tool Definitions ──────────────────────────────────────────
1031
1038
  const _infraToolDefs = (0, mcp_tools_1.getInfraToolDefinitions)();
1032
- const allToolDefinitions = [...toolDefinitions, ..._infraToolDefs];
1033
- const toolDefinitionsArray = _infraToolDefs.length > 0 ? allToolDefinitions : toolDefinitions;
1039
+ // `lemma_toolbox` keeps every non-advertised tool reachable. It is defined here rather
1040
+ // than in the main array because it must never itself be hidden.
1041
+ const TOOLBOX_TOOL_DEFINITION = {
1042
+ name: "lemma_toolbox",
1043
+ description: "Discover and invoke Lemma tools whose schemas are not loaded this session. Use action='list' (optionally with a query) to see what exists, 'schema' to read one tool's inputs, and 'call' to run it. Every Lemma capability is reachable here.",
1044
+ inputSchema: {
1045
+ type: "object",
1046
+ properties: {
1047
+ action: { type: "string", enum: ["list", "schema", "call"], description: "list | schema | call", default: "list" },
1048
+ query: { type: "string", description: "Filter terms for action='list' (e.g. 'terraform drift')" },
1049
+ tool: { type: "string", description: "Tool name, for action='schema' or 'call'" },
1050
+ args: { type: "object", description: "Arguments passed through to the tool, for action='call'" },
1051
+ },
1052
+ required: ["action"],
1053
+ },
1054
+ };
1055
+ const allToolDefinitions = [
1056
+ ...toolDefinitions,
1057
+ ..._infraToolDefs,
1058
+ TOOLBOX_TOOL_DEFINITION,
1059
+ ];
1060
+ // Narrow what gets advertised to what this project can actually use. Hidden tools stay
1061
+ // registered in `toolHandlers` and callable — both directly and through `lemma_toolbox`.
1062
+ const _toolSurface = (0, tool_profiles_1.resolveToolSurface)(allToolDefinitions, process.cwd());
1063
+ const toolDefinitionsArray = _toolSurface.advertised;
1064
+ const hiddenToolDefinitions = _toolSurface.hidden;
1065
+ /** Serialized size of every tool schema sent to the client, for cost accounting. */
1066
+ function getToolSchemaChars() {
1067
+ try {
1068
+ return JSON.stringify(toolDefinitionsArray).length;
1069
+ }
1070
+ catch {
1071
+ return 0;
1072
+ }
1073
+ }
1074
+ /** Every tool name with a registered handler, advertised or not. */
1075
+ function getRegisteredToolNames() {
1076
+ return Object.keys(toolHandlers);
1077
+ }
1078
+ /** Tool names whose schemas this session sends to the client. */
1079
+ function getAdvertisedToolNames() {
1080
+ return toolDefinitionsArray.map((d) => d.name);
1081
+ }
1082
+ /** Which tools this session advertises, and why — surfaced by `token_budget`. */
1083
+ function getToolSurfaceSummary() {
1084
+ return {
1085
+ profile: _toolSurface.profile,
1086
+ advertised: toolDefinitionsArray.length,
1087
+ hidden: hiddenToolDefinitions.length,
1088
+ total: allToolDefinitions.length,
1089
+ activeGroups: _toolSurface.activeGroups,
1090
+ };
1091
+ }
1034
1092
  const toolHandlers = {
1035
1093
  scrub_privacy: handleScrubPrivacy,
1036
1094
  search_memory: handleSearchMemory,
@@ -1103,8 +1161,44 @@ const toolHandlers = {
1103
1161
  conversation_checkpoint: handleConversationCheckpoint,
1104
1162
  test_autofix_interceptor: handleTestAutofixInterceptor,
1105
1163
  prompt_pattern_cache: handlePromptPatternCache,
1164
+ lemma_toolbox: handleToolbox,
1106
1165
  ...(0, mcp_tools_1.getInfraToolHandlers)(),
1107
1166
  };
1167
+ /**
1168
+ * Reaches the tools whose schemas were withheld from the session listing.
1169
+ *
1170
+ * `call` proxies through this server rather than relying on the client to emit a name it
1171
+ * never saw advertised — some clients validate outgoing tool names against tools/list, so
1172
+ * the proxy is what makes hiding a schema safe rather than a silent capability loss.
1173
+ */
1174
+ async function handleToolbox(args) {
1175
+ const action = args?.action || "list";
1176
+ if (action === "list") {
1177
+ return {
1178
+ content: [{ type: "text", text: (0, tool_profiles_1.buildToolboxCatalog)(hiddenToolDefinitions, args?.query) }],
1179
+ };
1180
+ }
1181
+ const toolName = args?.tool;
1182
+ if (!toolName)
1183
+ throw new Error("`tool` is required for action '" + action + "'");
1184
+ if (action === "schema") {
1185
+ const def = allToolDefinitions.find((d) => d.name === toolName);
1186
+ if (!def)
1187
+ throw new Error(`Unknown tool: ${toolName}`);
1188
+ return { content: [{ type: "text", text: JSON.stringify(def, null, 2) }] };
1189
+ }
1190
+ if (action === "call") {
1191
+ const handler = toolHandlers[toolName];
1192
+ if (!handler)
1193
+ throw new Error(`Unknown tool: ${toolName}`);
1194
+ // The proxy must not become a way around the license gate the direct path enforces.
1195
+ if (!isProUser() && !FREE_TOOLS.has(toolName)) {
1196
+ return { content: [{ type: "text", text: PRO_GATE_MESSAGE }] };
1197
+ }
1198
+ return handler((args?.args || {}));
1199
+ }
1200
+ throw new Error(`Unknown action: ${action}. Use 'list', 'schema', or 'call'.`);
1201
+ }
1108
1202
  function setupToolsHandlers(server, onToolCall) {
1109
1203
  server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => ({
1110
1204
  tools: toolDefinitionsArray,
@@ -1140,13 +1234,17 @@ function setupToolsHandlers(server, onToolCall) {
1140
1234
  }
1141
1235
  try {
1142
1236
  const result = await handler((args || {}));
1237
+ const tokensImpact = (0, utils_1.estimateTokensFromResult)(result);
1143
1238
  onToolCall?.({
1144
1239
  tool: name,
1145
1240
  args: args || {},
1146
1241
  result: "OK",
1147
1242
  latency: Date.now() - startTime,
1148
- tokensImpact: (0, utils_1.estimateTokensFromResult)(result),
1243
+ tokensImpact,
1149
1244
  });
1245
+ // Every tool result lands in the model's context — that is a real cost, and it
1246
+ // is charged even when the same call also reports a saving.
1247
+ (0, reportSavings_1.reportCost)({ source: "toolResult", tokens: tokensImpact, toolName: name });
1150
1248
  return result;
1151
1249
  }
1152
1250
  catch (error) {
@@ -1177,7 +1275,7 @@ async function handleSearchMemory(args) {
1177
1275
  throw new Error("Query is required");
1178
1276
  try {
1179
1277
  const brain = (0, TheBrainV2_1.getBrain)();
1180
- const results = brain.search(query, limit);
1278
+ const results = brain.search(query, limit, 0, { projectId: (0, TheBrainV2_1.deriveProjectId)() });
1181
1279
  if (results.length === 0) {
1182
1280
  return { content: [{ type: "text", text: "No relevant memories found in Lemma's Brain." }] };
1183
1281
  }
@@ -1662,30 +1760,88 @@ async function handleSqueezePrompt(args) {
1662
1760
  return { content: [{ type: "text", text: `Error squeezing prompt: ${err.message}` }] };
1663
1761
  }
1664
1762
  }
1763
+ /**
1764
+ * Onboarding built from the filesystem alone.
1765
+ *
1766
+ * The proxy produces a richer map, but this tool is in the core surface and CLAUDE.md tells
1767
+ * the model to call it first — so when the proxy is down, the first call of every session
1768
+ * used to return an error and the session started with no context at all. A local answer is
1769
+ * strictly better than that, and it never fails.
1770
+ */
1771
+ function buildLocalOnboarding(cwd) {
1772
+ const lines = [];
1773
+ const projectName = path_1.default.basename(cwd);
1774
+ lines.push(`# ${projectName}`, "");
1775
+ let pkg = null;
1776
+ try {
1777
+ pkg = JSON.parse(fs_1.default.readFileSync(path_1.default.join(cwd, "package.json"), "utf8"));
1778
+ }
1779
+ catch { /* not a node project */ }
1780
+ if (pkg) {
1781
+ if (pkg.description)
1782
+ lines.push(pkg.description, "");
1783
+ lines.push(`**Version:** ${pkg.version || "unknown"}`);
1784
+ const deps = Object.keys(pkg.dependencies || {});
1785
+ if (deps.length)
1786
+ lines.push(`**Dependencies:** ${deps.slice(0, 12).join(", ")}${deps.length > 12 ? ` (+${deps.length - 12})` : ""}`);
1787
+ const scripts = Object.keys(pkg.scripts || {});
1788
+ if (scripts.length)
1789
+ lines.push(`**Scripts:** ${scripts.slice(0, 12).join(", ")}`);
1790
+ }
1791
+ // Current branch straight from .git/HEAD — no subprocess, works on a detached HEAD too.
1792
+ try {
1793
+ const head = fs_1.default.readFileSync(path_1.default.join(cwd, ".git", "HEAD"), "utf8").trim();
1794
+ const branch = head.startsWith("ref: ") ? head.replace("ref: refs/heads/", "") : `detached @ ${head.slice(0, 8)}`;
1795
+ lines.push(`**Branch:** ${branch}`);
1796
+ }
1797
+ catch { /* not a git repo */ }
1798
+ try {
1799
+ const entries = fs_1.default
1800
+ .readdirSync(cwd, { withFileTypes: true })
1801
+ .filter((e) => e.isDirectory() && !e.name.startsWith(".") && e.name !== "node_modules")
1802
+ .map((e) => e.name);
1803
+ if (entries.length)
1804
+ lines.push(`**Top-level directories:** ${entries.slice(0, 15).join(", ")}`);
1805
+ }
1806
+ catch { /* unreadable cwd */ }
1807
+ try {
1808
+ const memories = (0, TheBrainV2_1.getBrain)().getEntriesForProject((0, TheBrainV2_1.deriveProjectId)(cwd));
1809
+ lines.push("", `**Brain memories for this project:** ${memories.length}`);
1810
+ if (memories.length) {
1811
+ const recent = memories
1812
+ .slice()
1813
+ .sort((a, b) => (a.timestamp < b.timestamp ? 1 : -1))
1814
+ .slice(0, 5);
1815
+ lines.push("", "Most recent:");
1816
+ for (const m of recent)
1817
+ lines.push(`- ${m.query.slice(0, 100)}`);
1818
+ lines.push("", "Use `search_memory` to retrieve any of these in full.");
1819
+ }
1820
+ }
1821
+ catch { /* brain unavailable */ }
1822
+ return lines.join("\n");
1823
+ }
1665
1824
  async function handleGetProjectOnboarding(_args) {
1825
+ const cwd = process.cwd();
1666
1826
  try {
1667
1827
  const port = (0, utils_1.getProxyPort)();
1668
- const response = await axios_1.default.get(`http://localhost:${port}/api/project/onboarding`);
1669
- return {
1670
- content: [
1671
- {
1672
- type: "text",
1673
- text: response.data.markdown || "# Onboarding mental model could not be generated.",
1674
- },
1675
- ],
1676
- };
1828
+ const response = await axios_1.default.get(`http://localhost:${port}/api/project/onboarding`, { timeout: 2000 });
1829
+ if (response.data?.markdown) {
1830
+ return { content: [{ type: "text", text: response.data.markdown }] };
1831
+ }
1677
1832
  }
1678
1833
  catch (error) {
1679
- (0, utils_1.logError)("get_project_onboarding", error);
1680
- return {
1681
- content: [
1682
- {
1683
- type: "text",
1684
- text: `Failed to fetch codebase onboarding mental model: ${error.message}. Is Lemma proxy running?`,
1685
- },
1686
- ],
1687
- };
1834
+ // Proxy down or slow — fall through. Not worth surfacing as an error to the model.
1835
+ (0, utils_1.logError)("get_project_onboarding (falling back to local)", error);
1688
1836
  }
1837
+ return {
1838
+ content: [
1839
+ {
1840
+ type: "text",
1841
+ text: `${buildLocalOnboarding(cwd)}\n\n---\n*Generated locally. Start the Lemma proxy (\`lemma start\`) for the full indexed architecture map.*`,
1842
+ },
1843
+ ],
1844
+ };
1689
1845
  }
1690
1846
  function extractSymbolsWithTsCompiler(filePath, relPath) {
1691
1847
  try {
@@ -1913,7 +2069,7 @@ async function handleQueryHybridConsensus(args) {
1913
2069
  const fullQuery = context ? `${query}\n\nContext: ${context}` : query;
1914
2070
  try {
1915
2071
  const brain = (0, TheBrainV2_1.getBrain)();
1916
- const results = brain.search(fullQuery, 3);
2072
+ const results = brain.search(fullQuery, 3, 0, { projectId: (0, TheBrainV2_1.deriveProjectId)() });
1917
2073
  const topHit = results[0];
1918
2074
  if (topHit && topHit.similarity >= threshold) {
1919
2075
  const savedTokens = Math.floor((topHit.response?.length || 500) / 4);
@@ -1971,7 +2127,7 @@ async function handleGetTelepathicHints(args) {
1971
2127
  const telepathicQuery = `${basename} ${dirContext} ${ext} patterns solutions architecture`;
1972
2128
  try {
1973
2129
  const brain = (0, TheBrainV2_1.getBrain)();
1974
- const results = brain.search(telepathicQuery, limit);
2130
+ const results = brain.search(telepathicQuery, limit, 0, { projectId: (0, TheBrainV2_1.deriveProjectId)() });
1975
2131
  if (results.length === 0) {
1976
2132
  return {
1977
2133
  content: [
@@ -2217,7 +2373,7 @@ async function handleSmarterCache(args) {
2217
2373
  const fullQuery = context ? `${query}\n\nContext: ${context}` : query;
2218
2374
  try {
2219
2375
  const brain = (0, TheBrainV2_1.getBrain)();
2220
- const results = brain.search(fullQuery, 5);
2376
+ const results = brain.search(fullQuery, 5, 0, { projectId: (0, TheBrainV2_1.deriveProjectId)() });
2221
2377
  const topHit = results[0];
2222
2378
  if (topHit && topHit.similarity >= threshold) {
2223
2379
  const responseText = topHit.response;
@@ -2370,6 +2526,12 @@ async function handleTokenBudget(_args) {
2370
2526
  astNavigation: true,
2371
2527
  privacyScrubbing: true,
2372
2528
  },
2529
+ // The fixed cost of being connected, re-sent on every request of the session.
2530
+ perTurnOverhead: {
2531
+ ...getToolSurfaceSummary(),
2532
+ schemaTokens: Math.round(getToolSchemaChars() / 4),
2533
+ note: "Hidden tools stay callable via lemma_toolbox. Set mcp.toolProfile='full' in lemma.config.json to advertise all of them.",
2534
+ },
2373
2535
  recommendations: toolCallCount === 0
2374
2536
  ? ["No se han usado tools de Lemma todavía. Asegúrate de que el agente esté usando el prompt lemma-turbomode."]
2375
2537
  : ["Sigue usando turbosqueeze antes de cada code block grande.", "Usa smarter_cache antes de razonar."],
@@ -2983,7 +3145,8 @@ async function handleSemanticDedupGuard(args) {
2983
3145
  throw new Error("query and response are required");
2984
3146
  try {
2985
3147
  const brain = (0, TheBrainV2_1.getBrain)();
2986
- const results = brain.search(query, 3);
3148
+ // A dedup check is not a question the user asked — it must not register as a cache hit.
3149
+ const results = brain.search(query, 3, 0, { countStats: false, projectId: (0, TheBrainV2_1.deriveProjectId)() });
2987
3150
  const topHit = results[0];
2988
3151
  if (!topHit) {
2989
3152
  return {
@@ -4089,7 +4252,7 @@ async function handleLocalSemanticAutofix(args) {
4089
4252
  throw new Error("errorMessage is required");
4090
4253
  try {
4091
4254
  const brain = (0, TheBrainV2_1.getBrain)();
4092
- const results = brain.search(errorMessage, 3, 0.70);
4255
+ const results = brain.search(errorMessage, 3, 0.70, { projectId: (0, TheBrainV2_1.deriveProjectId)() });
4093
4256
  const topHit = results[0];
4094
4257
  if (topHit) {
4095
4258
  const responseText = topHit.response;