coreinsight-cli 0.3.2__tar.gz → 0.3.4__tar.gz

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 (35) hide show
  1. {coreinsight_cli-0.3.2/coreinsight_cli.egg-info → coreinsight_cli-0.3.4}/PKG-INFO +37 -11
  2. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/README.md +16 -0
  3. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/analyzer.py +62 -193
  4. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/main.py +6 -4
  5. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/memory.py +3 -2
  6. coreinsight_cli-0.3.4/coreinsight/prompts/__init__.py +59 -0
  7. coreinsight_cli-0.3.4/coreinsight/prompts/_base.py +15 -0
  8. coreinsight_cli-0.3.4/coreinsight/prompts/bottleneck.py +131 -0
  9. coreinsight_cli-0.3.4/coreinsight/prompts/harness.py +298 -0
  10. coreinsight_cli-0.3.4/coreinsight/prompts/optimizer.py +97 -0
  11. coreinsight_cli-0.3.4/coreinsight/prompts/test_cases.py +55 -0
  12. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/sandbox.py +1 -0
  13. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4/coreinsight_cli.egg-info}/PKG-INFO +37 -11
  14. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight_cli.egg-info/SOURCES.txt +6 -1
  15. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight_cli.egg-info/requires.txt +25 -9
  16. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/pyproject.toml +31 -10
  17. coreinsight_cli-0.3.2/coreinsight/prompts.py +0 -301
  18. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/LICENSE +0 -0
  19. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/__init__.py +0 -0
  20. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/config.py +0 -0
  21. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/demo/__init__.py +0 -0
  22. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/demo/bad_loop.py +0 -0
  23. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/demo/data_processor.py +0 -0
  24. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/demo/slow.cpp +0 -0
  25. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/embeddings.py +0 -0
  26. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/hardware.py +0 -0
  27. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/indexer.py +0 -0
  28. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/parser.py +0 -0
  29. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/profiler.py +0 -0
  30. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/scanner.py +0 -0
  31. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight/tui.py +0 -0
  32. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight_cli.egg-info/dependency_links.txt +0 -0
  33. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight_cli.egg-info/entry_points.txt +0 -0
  34. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/coreinsight_cli.egg-info/top_level.txt +0 -0
  35. {coreinsight_cli-0.3.2 → coreinsight_cli-0.3.4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: coreinsight-cli
3
- Version: 0.3.2
3
+ Version: 0.3.4
4
4
  Summary: Local-first AI performance profiler that mathematically verifies optimizations for Python, C++, and CUDA
5
5
  Author: Varun Jani
6
6
  License: GPL-3.0-or-later
@@ -20,20 +20,30 @@ Requires-Python: >=3.9
20
20
  Description-Content-Type: text/markdown
21
21
  License-File: LICENSE
22
22
  Requires-Dist: rich>=13.0
23
+ Requires-Dist: textual>=0.60.0
24
+ Requires-Dist: psutil>=5.9
25
+ Requires-Dist: pydantic>=2.0
23
26
  Requires-Dist: docker>=6.0
24
- Requires-Dist: tree-sitter==0.21.3
25
- Requires-Dist: tree-sitter-languages
26
- Requires-Dist: langchain>=0.2.0
27
27
  Requires-Dist: langchain-core>=0.2.0
28
+ Requires-Dist: langchain>=0.2.0
28
29
  Requires-Dist: langchain-ollama>=0.1.0
29
- Requires-Dist: langchain-google-genai>=1.0.0
30
30
  Requires-Dist: langchain-openai>=0.1.0
31
- Requires-Dist: langchain-anthropic>=0.1.0
32
- Requires-Dist: pydantic>=2.0
33
- Requires-Dist: chromadb>=0.5.0
34
- Requires-Dist: sentence-transformers>=3.0.0
35
- Requires-Dist: textual>=0.60.0
36
- Requires-Dist: psutil>=5.9
31
+ Requires-Dist: tree-sitter==0.21.3
32
+ Requires-Dist: tree-sitter-languages
33
+ Provides-Extra: openai
34
+ Provides-Extra: google
35
+ Requires-Dist: langchain-google-genai>=1.0.0; extra == "google"
36
+ Provides-Extra: anthropic
37
+ Requires-Dist: langchain-anthropic>=0.1.0; extra == "anthropic"
38
+ Provides-Extra: memory
39
+ Requires-Dist: chromadb>=0.5.0; extra == "memory"
40
+ Requires-Dist: sentence-transformers>=3.0.0; extra == "memory"
41
+ Provides-Extra: cloud
42
+ Requires-Dist: langchain-openai>=0.1.0; extra == "cloud"
43
+ Requires-Dist: langchain-google-genai>=1.0.0; extra == "cloud"
44
+ Requires-Dist: langchain-anthropic>=0.1.0; extra == "cloud"
45
+ Provides-Extra: all
46
+ Requires-Dist: coreinsight-cli[cloud,memory]; extra == "all"
37
47
  Provides-Extra: compat
38
48
  Requires-Dist: pysqlite3-binary>=0.5.0; extra == "compat"
39
49
  Dynamic: license-file
@@ -49,7 +59,23 @@ CoreInsight finds hardware bottlenecks in your code, generates optimized replace
49
59
  ## Install
50
60
 
51
61
  ```bash
62
+ # OpenAI key - quick install
63
+ pip install coreinsight-cli[openai]
64
+
65
+ # Gemini key - quick install
66
+ pip install coreinsight-cli[google]
67
+
68
+ # Claude key - quick install
69
+ pip install coreinsight-cli[anthropic]
70
+
71
+ # Local Ollama install
52
72
  pip install coreinsight-cli
73
+
74
+ # Memory and additional usage install
75
+ pip install coreinsight-cli[openai,memory]
76
+
77
+ # Install everything
78
+ pip install coreinsight-cli[all]
53
79
  ```
54
80
 
55
81
  **Requirements:** Python 3.9+ · Docker Desktop · [Ollama](https://ollama.com/download) (for local inference)
@@ -9,7 +9,23 @@ CoreInsight finds hardware bottlenecks in your code, generates optimized replace
9
9
  ## Install
10
10
 
11
11
  ```bash
12
+ # OpenAI key - quick install
13
+ pip install coreinsight-cli[openai]
14
+
15
+ # Gemini key - quick install
16
+ pip install coreinsight-cli[google]
17
+
18
+ # Claude key - quick install
19
+ pip install coreinsight-cli[anthropic]
20
+
21
+ # Local Ollama install
12
22
  pip install coreinsight-cli
23
+
24
+ # Memory and additional usage install
25
+ pip install coreinsight-cli[openai,memory]
26
+
27
+ # Install everything
28
+ pip install coreinsight-cli[all]
13
29
  ```
14
30
 
15
31
  **Requirements:** Python 3.9+ · Docker Desktop · [Ollama](https://ollama.com/download) (for local inference)
@@ -8,11 +8,11 @@ from langchain_core.prompts import PromptTemplate
8
8
  from langchain_core.exceptions import OutputParserException
9
9
 
10
10
  from langchain_ollama import ChatOllama
11
- from langchain_google_genai import ChatGoogleGenerativeAI
12
- from langchain_openai import ChatOpenAI
13
- from langchain_anthropic import ChatAnthropic
14
11
 
15
- from coreinsight.prompts import SYSTEM_PROMPT, ANALYSIS_TEMPLATE, HARNESS_ADDENDUM
12
+ from coreinsight.prompts import (
13
+ SYSTEM_PROMPT, ANALYSIS_TEMPLATE, HARNESS_ADDENDUM,
14
+ _HARNESS_TEMPLATE, _FIX_TEMPLATE, _TEST_CASES_TEMPLATE,
15
+ )
16
16
 
17
17
  # Phrases that appear at the start of a truncated LLM response
18
18
  _TRUNCATION_HINTS = (
@@ -99,117 +99,6 @@ class AuditResult(BaseModel):
99
99
  optimized_code: Optional[str] = Field(description="The entirely rewritten optimized code, ready to drop in", default=None)
100
100
 
101
101
 
102
- _HARNESS_TEMPLATE = """
103
- You are a strict QA engineer writing a standalone asymptotic scaling benchmark script in {language}.
104
-
105
- ORIGINAL FUNCTION (Name: {func_name}):
106
- {original}
107
-
108
- OPTIMIZED FUNCTION:
109
- {optimized}
110
-
111
- GLOBAL DEPENDENCIES (Helper functions/structs required to run the code):
112
- {context}
113
-
114
- Write the complete executable script (e.g., `int main()` or `if __name__ == "__main__":`) that:
115
- 1. Includes necessary imports/headers.
116
- 2. Includes ALL required helper functions or structs from GLOBAL DEPENDENCIES so the script is fully standalone.
117
- 3. Defines BOTH the original and optimized functions exactly as provided above.
118
- 4. Tests multiple data sizes (e.g., N=10, 100, 1000, 5000).
119
- 5. Target Hardware: {hardware_target}. The largest N MUST cross cache boundaries but MUST NOT exceed 20% of available RAM to prevent OOM crashes.
120
- 6. Initializes realistic dummy data for each size N.
121
- 7. Times execution of original vs optimized using high-resolution timers.
122
-
123
- CRITICAL TIMING:
124
- - Python: use `time.perf_counter()`. C++: use `std::chrono::high_resolution_clock`.
125
- - Clamp: `orig_time = max(end - start, 1e-9)` to prevent zero-division.
126
- - Speedup: `speedup = orig_time / opt_time`.
127
-
128
- ISOLATION RULES (CRITICAL):
129
- - This runs in an empty Docker container. NO local files exist.
130
- - DO NOT use local imports. Define everything inline.
131
- - DO NOT rename the original function — call it exactly `{func_name}`.
132
-
133
- OUTPUT FORMAT (CRITICAL):
134
- Print ONLY this exact CSV to stdout, no other text:
135
- N,Original_Time,Optimized_Time,Speedup
136
- 10,0.002,0.001,2.00
137
-
138
- [PYTHON ONLY]: Also import matplotlib, plot results, and save as `benchmark_plot.png`.
139
-
140
- FORMATTING RULE: Wrap your ENTIRE script in a single markdown code block. No text before or after.
141
- """
142
-
143
- _FIX_TEMPLATE = """
144
- You are an expert {language} developer. Your previous benchmark script FAILED in an isolated sandbox.
145
-
146
- ORIGINAL FUNCTION (Name: {func_name}):
147
- {original}
148
-
149
- GLOBAL DEPENDENCIES:
150
- {context}
151
-
152
- YOUR FAILED SCRIPT:
153
- {bad_harness}
154
-
155
- EXECUTION ERROR LOGS:
156
- {error_logs}
157
-
158
- ISOLATION CONSTRAINTS (CRITICAL):
159
- - Empty Docker container. No local files. NO local imports.
160
- - Define `{func_name}` and all GLOBAL DEPENDENCIES inline.
161
-
162
- FIX INSTRUCTIONS:
163
- 1. Diagnose the failure from the error logs above.
164
- 2. Fix imports, NameErrors, type mismatches, infinite loops, or OOM issues.
165
- 3. Maintain the CSV stdout format exactly: N,Original_Time,Optimized_Time,Speedup
166
- 4. Use high-resolution timers and clamp with `max(t, 1e-9)`.
167
- 5. [PYTHON ONLY]: Save benchmark plot as `benchmark_plot.png`.
168
-
169
- FORMATTING RULE: Wrap your ENTIRE fixed script in a single markdown code block. No text before or after.
170
- """
171
-
172
- _TEST_CASES_TEMPLATE = """
173
- You are a QA engineer writing correctness test cases for a function.
174
-
175
- FUNCTION NAME: {func_name}
176
- LANGUAGE: {language}
177
-
178
- FUNCTION SIGNATURE AND BODY:
179
- {original}
180
-
181
- GLOBAL DEPENDENCIES (helper functions / structs this function relies on):
182
- {context}
183
-
184
- Your task: generate {num_cases} diverse test cases that call `{func_name}` with different
185
- arguments. The cases must cover:
186
- - Small inputs (N ~ 10)
187
- - Medium inputs (N ~ 100-500)
188
- - Edge cases: empty collections, single-element, all-zeros, negative values (where applicable)
189
- - Boundary conditions specific to this function's logic
190
-
191
- OUTPUT FORMAT — respond with ONLY a valid JSON array, nothing else. No markdown fences,
192
- no explanation. Each element must be a JSON object with exactly two keys:
193
- "args" : a JSON array of positional arguments (use only JSON-serialisable types:
194
- numbers, strings, booleans, arrays, objects — NO numpy, NO bytes)
195
- "kwargs": a JSON object of keyword arguments (may be empty {{}})
196
-
197
- Example (do NOT copy this — generate cases specific to {func_name}):
198
- [
199
- {{"args": [[1, 2, 3]], "kwargs": {{}}}},
200
- {{"args": [[]], "kwargs": {{}}}},
201
- {{"args": [[9, -1, 4, 0, 7]], "kwargs": {{"reverse": true}}}}
202
- ]
203
-
204
- CONSTRAINTS:
205
- - All values must be plain JSON types — no numpy arrays, no custom objects.
206
- - If the function operates on a matrix, represent it as a list-of-lists.
207
- - If the function takes a size integer N, generate concrete data of that size inline.
208
- - Do NOT include function calls or expressions — only literal values.
209
- - Produce exactly {num_cases} test cases.
210
- """
211
-
212
-
213
102
  class AnalyzerAgent:
214
103
  def __init__(self, provider="ollama", model_name="llama3.2", api_keys=None, model_tier="large"):
215
104
  self.model_tier = model_tier
@@ -217,70 +106,15 @@ class AnalyzerAgent:
217
106
  self.provider = provider
218
107
  api_keys = api_keys or {}
219
108
 
220
- if provider == "openai":
221
- if not api_keys.get("openai"):
222
- raise ValueError("OpenAI API Key required.")
223
- self.base_llm = ChatOpenAI(
224
- model=model_name,
225
- api_key=api_keys["openai"],
226
- temperature=0.1,
227
- model_kwargs={"response_format": {"type": "json_object"}},
228
- )
229
- self.json_llm = self.base_llm
230
-
109
+ # Reuse shared LLM factory — handles lazy imports and provider validation
110
+ from coreinsight.prompts import ModelTier
111
+ if provider == "ollama":
112
+ api_keys["_ctx"] = 4096 if model_tier == ModelTier.SMALL else 8192
113
+ api_keys["_predict"] = 2048 if model_tier == ModelTier.SMALL else 4096
231
114
  elif provider == "local_server":
232
- from coreinsight.prompts import ModelTier
233
- base_url = api_keys.get("local_url", "http://localhost:1234/v1")
234
- _max_tokens = 2048 if model_tier == ModelTier.SMALL else 4096
235
- self.base_llm = ChatOpenAI(
236
- model=model_name,
237
- api_key="not-needed",
238
- base_url=base_url,
239
- temperature=0.1,
240
- max_tokens=_max_tokens,
241
- model_kwargs={"response_format": {"type": "json_object"}},
242
- )
243
- self.json_llm = self.base_llm
244
-
245
- elif provider == "anthropic":
246
- if not api_keys.get("anthropic"):
247
- raise ValueError("Anthropic API Key required.")
248
- self.base_llm = ChatAnthropic(
249
- model=model_name,
250
- api_key=api_keys["anthropic"],
251
- temperature=0.1,
252
- )
253
- # Anthropic doesn't support response_format; JSON is enforced via prompt only
254
- self.json_llm = self.base_llm
255
-
256
- elif provider == "google":
257
- if not api_keys.get("google"):
258
- raise ValueError("Google Gemini API Key required.")
259
- self.base_llm = ChatGoogleGenerativeAI(
260
- model=model_name,
261
- google_api_key=api_keys["google"],
262
- temperature=0.1,
263
- convert_system_message_to_human=True,
264
- )
265
- self.json_llm = self.base_llm
266
-
267
- else: # Ollama default
268
- from coreinsight.prompts import ModelTier
269
- # Small models (7B) typically have 4096 native context.
270
- # Asking for more causes silent degradation or OOM on the host.
271
- # Medium/large local models can handle 8192 comfortably.
272
- _ctx = 4096 if model_tier == ModelTier.SMALL else 8192
273
- # num_predict: small models need room for JSON + code in one shot.
274
- # Capping at 2048 for small prevents runaway generation that hits
275
- # the limit mid-JSON and returns truncated garbage.
276
- _predict = 2048 if model_tier == ModelTier.SMALL else 4096
277
- self.base_llm = ChatOllama(
278
- model=model_name,
279
- temperature=0.1,
280
- num_predict=_predict,
281
- num_ctx=_ctx,
282
- )
283
- self.json_llm = self.base_llm.bind(format="json")
115
+ api_keys["_predict"] = 2048 if model_tier == ModelTier.SMALL else 4096
116
+
117
+ self.base_llm, self.json_llm = _build_llm(provider, model_name, api_keys)
284
118
 
285
119
  self.prompt = PromptTemplate(
286
120
  template=ANALYSIS_TEMPLATE + "\n\n{format_instructions}",
@@ -422,6 +256,13 @@ class AnalyzerAgent:
422
256
  hardware_target: str = "Generic CPU",
423
257
  stream_callback: Optional[Callable[[str], None]] = None,
424
258
  ) -> str:
259
+ from coreinsight.prompts import ModelTier
260
+ if language.lower() in ("cpp", "c++") and self.model_tier == ModelTier.SMALL:
261
+ raise ValueError(
262
+ f"C++ harness generation requires a MEDIUM or LARGE model "
263
+ f"(current tier: {self.model_tier}). "
264
+ "Switch to a 13B+ model (e.g. codellama:13b, mistral) for C++ support."
265
+ )
425
266
  try:
426
267
  context, original_code = _compress_for_small_model(
427
268
  context, original_code, self.model_tier
@@ -556,16 +397,17 @@ class AnalyzerAgent:
556
397
  # ---------------------------------------------------------------------------
557
398
 
558
399
  def _build_llm(provider: str, model_name: str, api_keys: dict):
559
- """
560
- Shared LLM factory for all multi-agent classes.
561
- Returns (base_llm, json_llm) — same pattern as AnalyzerAgent.__init__.
562
- Raises ValueError on missing credentials.
563
- """
564
400
  api_keys = api_keys or {}
565
401
 
566
402
  if provider == "openai":
567
403
  if not api_keys.get("openai"):
568
404
  raise ValueError("OpenAI API key required.")
405
+ try:
406
+ from langchain_openai import ChatOpenAI
407
+ except ImportError:
408
+ raise ImportError(
409
+ "OpenAI provider requires: pip install coreinsight-cli[openai]"
410
+ )
569
411
  llm = ChatOpenAI(
570
412
  model=model_name,
571
413
  api_key=api_keys["openai"],
@@ -575,8 +417,14 @@ def _build_llm(provider: str, model_name: str, api_keys: dict):
575
417
  return llm, llm
576
418
 
577
419
  if provider == "local_server":
420
+ try:
421
+ from langchain_openai import ChatOpenAI
422
+ except ImportError:
423
+ raise ImportError(
424
+ "local_server provider requires: pip install coreinsight-cli[openai]"
425
+ )
578
426
  base_url = api_keys.get("local_url", "http://localhost:1234/v1")
579
- _max_tokens = api_keys.pop("_predict", 4096) # reuse same key as Ollama path
427
+ _max_tokens = api_keys.pop("_predict", 4096)
580
428
  llm = ChatOpenAI(
581
429
  model=model_name,
582
430
  api_key="not-needed",
@@ -590,6 +438,12 @@ def _build_llm(provider: str, model_name: str, api_keys: dict):
590
438
  if provider == "anthropic":
591
439
  if not api_keys.get("anthropic"):
592
440
  raise ValueError("Anthropic API key required.")
441
+ try:
442
+ from langchain_anthropic import ChatAnthropic
443
+ except ImportError:
444
+ raise ImportError(
445
+ "Anthropic provider requires: pip install coreinsight-cli[anthropic]"
446
+ )
593
447
  llm = ChatAnthropic(
594
448
  model=model_name,
595
449
  api_key=api_keys["anthropic"],
@@ -600,6 +454,12 @@ def _build_llm(provider: str, model_name: str, api_keys: dict):
600
454
  if provider == "google":
601
455
  if not api_keys.get("google"):
602
456
  raise ValueError("Google Gemini API key required.")
457
+ try:
458
+ from langchain_google_genai import ChatGoogleGenerativeAI
459
+ except ImportError:
460
+ raise ImportError(
461
+ "Google provider requires: pip install coreinsight-cli[google]"
462
+ )
603
463
  llm = ChatGoogleGenerativeAI(
604
464
  model=model_name,
605
465
  google_api_key=api_keys["google"],
@@ -608,9 +468,7 @@ def _build_llm(provider: str, model_name: str, api_keys: dict):
608
468
  )
609
469
  return llm, llm
610
470
 
611
- # Ollama default — context and predict budget are passed in from the
612
- # calling agent which knows its own model_tier.
613
- # Default to medium-safe values; callers override via kwargs if needed.
471
+ # Ollama default
614
472
  _ctx = api_keys.pop("_ctx", 8192)
615
473
  _predict = api_keys.pop("_predict", 4096)
616
474
  base = ChatOllama(
@@ -650,13 +508,13 @@ class BottleneckAgent:
650
508
  api_keys: dict,
651
509
  model_tier: str,
652
510
  ) -> None:
653
- from coreinsight.prompts import BOTTLENECK_TEMPLATE, SYSTEM_PROMPT
511
+ from coreinsight.prompts import BOTTLENECK_TEMPLATES, SYSTEM_PROMPT
654
512
  self.model_tier = model_tier
655
513
  self.parser = JsonOutputParser(pydantic_object=AuditResult)
656
514
  self._base_llm, self._json_llm = _build_llm_tiered(provider, model_name, api_keys, model_tier)
657
515
 
658
516
  self._prompt = PromptTemplate(
659
- template=BOTTLENECK_TEMPLATE,
517
+ template=BOTTLENECK_TEMPLATES[model_tier],
660
518
  input_variables=[
661
519
  "language", "code_content", "context", "hardware_target",
662
520
  ],
@@ -736,10 +594,10 @@ class OptimizerAgent:
736
594
  api_keys: dict,
737
595
  model_tier: str,
738
596
  ) -> None:
739
- from coreinsight.prompts import OPTIMIZER_TEMPLATE
597
+ from coreinsight.prompts import OPTIMIZER_TEMPLATES
740
598
  self.model_tier = model_tier
741
599
  self._base_llm, _ = _build_llm_tiered(provider, model_name, api_keys, model_tier)
742
- self._template = OPTIMIZER_TEMPLATE
600
+ self._template = OPTIMIZER_TEMPLATES[model_tier]
743
601
 
744
602
  def _extract_code(self, raw: str) -> str:
745
603
  """Reuse the same extraction logic as AnalyzerAgent."""
@@ -898,7 +756,13 @@ class HarnessAgent:
898
756
  except Exception as e:
899
757
  return False, f"Harness generation failed: {e}", None, 0
900
758
 
901
- success, logs, plot_data = sandbox.execute_benchmark(harness, language)
759
+ # Catch missing int main() before hitting the sandbox
760
+ if language in ("cpp", "c++") and "int main(" not in harness and "int main (" not in harness:
761
+ logs = "Missing CSV output (exit 1).\nFull output:\nundefined reference to `main'"
762
+ success = False
763
+ plot_data = None
764
+ else:
765
+ success, logs, plot_data = sandbox.execute_benchmark(harness, language)
902
766
  is_valid = self._check_speedup(success, logs)
903
767
  retries = 0
904
768
 
@@ -921,7 +785,12 @@ class HarnessAgent:
921
785
  logs += f"\nFix generation failed: {e}"
922
786
  break
923
787
 
924
- success, logs, plot_data = sandbox.execute_benchmark(harness, language)
788
+ if language in ("cpp", "c++") and "int main(" not in harness and "int main (" not in harness:
789
+ logs = "Missing CSV output (exit 1).\nFull output:\nundefined reference to `main'"
790
+ success = False
791
+ plot_data = None
792
+ else:
793
+ success, logs, plot_data = sandbox.execute_benchmark(harness, language)
925
794
  is_valid = self._check_speedup(success, logs)
926
795
  retries += 1
927
796
 
@@ -161,7 +161,6 @@ def _run_multi_agent(
161
161
  optimized_code = multi_agents["optimizer"].generate(
162
162
  func_name, original_code, result,
163
163
  language, context, hardware_target,
164
- stream_callback=stream_callback, # readable code, stream it
165
164
  )
166
165
  if not optimized_code or optimized_code == original_code:
167
166
  return result, None, False, "", None, False
@@ -875,7 +874,8 @@ def run_analysis(file_path: str, no_docker: bool = False, tui_console=None, stre
875
874
  elif "out of memory" in exc_low or "oom" in exc_low:
876
875
  console.print(f"[bold yellow]⚠️ {func['name']}: Sandbox ran out of memory.[/bold yellow]")
877
876
  else:
878
- console.print(f"[bold red]❌ {func['name']}: Unexpected error — {exc}[/bold red]")
877
+ from rich.markup import escape
878
+ console.print(f"[bold red]❌ {func['name']}: Unexpected error — {escape(str(exc))}[/bold red]")
879
879
 
880
880
  console.print(Panel.fit(f"✅ [bold green]Analysis Complete![/bold green] Final report saved to:\n{report_path.absolute()}"))
881
881
 
@@ -1029,7 +1029,8 @@ def _run_test_cmd(func_name: str, no_docker: bool = False):
1029
1029
  num_cases=tier_limits["num_test_cases"],
1030
1030
  )
1031
1031
  except Exception as exc:
1032
- console.print(f"[red]LLM error generating test cases: {exc}[/red]")
1032
+ from rich.markup import escape
1033
+ console.print(f"[red]LLM error generating test cases: {escape(str(exc))}[/red]")
1033
1034
  return
1034
1035
 
1035
1036
  if not test_cases:
@@ -1146,7 +1147,8 @@ def _run_memory_cmd(clear: bool, export_path: str = None, export_fmt: str = "csv
1146
1147
  metadatas = all_records.get("metadatas", []) or []
1147
1148
  ids = all_records.get("ids", []) or []
1148
1149
  except Exception as exc:
1149
- console.print(f"[red]Failed to read memory store: {exc}[/red]")
1150
+ from rich.markup import escape
1151
+ console.print(f"[red]Failed to read memory store: {escape(str(exc))}[/red]")
1150
1152
  return
1151
1153
 
1152
1154
  # Build the detail table
@@ -85,9 +85,10 @@ class OptimizationMemory:
85
85
  import chromadb
86
86
  except Exception as sqlite_exc:
87
87
  self._init_error = (
88
- f"ChromaDB unavailable (likely outdated SQLite): {sqlite_exc}. "
88
+ f"ChromaDB unavailable: {sqlite_exc}. "
89
89
  "Optimization memory disabled. "
90
- "Fix: pip install coreinsight-cli[compat]"
90
+ "Fix: pip install coreinsight-cli[memory] "
91
+ "(or [compat] if you have an outdated SQLite)"
91
92
  )
92
93
  return False
93
94
 
@@ -0,0 +1,59 @@
1
+ """
2
+ coreinsight/prompts/__init__.py
3
+ Public API — all existing imports continue to work unchanged.
4
+ """
5
+
6
+ from coreinsight.prompts._base import ModelTier, SYSTEM_PROMPT
7
+ from coreinsight.prompts.bottleneck import (
8
+ BOTTLENECK_TEMPLATE,
9
+ BOTTLENECK_TEMPLATE_SMALL,
10
+ BOTTLENECK_TEMPLATE_MEDIUM,
11
+ BOTTLENECK_TEMPLATE_LARGE,
12
+ BOTTLENECK_TEMPLATES,
13
+ )
14
+ from coreinsight.prompts.optimizer import (
15
+ OPTIMIZER_TEMPLATE,
16
+ OPTIMIZER_TEMPLATE_SMALL,
17
+ OPTIMIZER_TEMPLATE_MEDIUM,
18
+ OPTIMIZER_TEMPLATE_LARGE,
19
+ OPTIMIZER_TEMPLATES,
20
+ )
21
+ from coreinsight.prompts.harness import (
22
+ ANALYSIS_TEMPLATE,
23
+ HARNESS_TEMPLATE,
24
+ FIX_TEMPLATE,
25
+ HARNESS_TEMPLATE_MULTI,
26
+ FIX_TEMPLATE_MULTI,
27
+ HARNESS_ADDENDUM,
28
+ HARNESS_ADDENDUM_MULTI,
29
+ )
30
+ from coreinsight.prompts.test_cases import TEST_CASES_TEMPLATE
31
+
32
+ # Backward-compatible alias — analyzer.py uses _TEST_CASES_TEMPLATE
33
+ _TEST_CASES_TEMPLATE = TEST_CASES_TEMPLATE
34
+
35
+ __all__ = [
36
+ "ModelTier",
37
+ "SYSTEM_PROMPT",
38
+ "BOTTLENECK_TEMPLATE",
39
+ "BOTTLENECK_TEMPLATE_SMALL",
40
+ "BOTTLENECK_TEMPLATE_MEDIUM",
41
+ "BOTTLENECK_TEMPLATE_LARGE",
42
+ "BOTTLENECK_TEMPLATES",
43
+ "OPTIMIZER_TEMPLATE",
44
+ "OPTIMIZER_TEMPLATE_SMALL",
45
+ "OPTIMIZER_TEMPLATE_MEDIUM",
46
+ "OPTIMIZER_TEMPLATE_LARGE",
47
+ "OPTIMIZER_TEMPLATES",
48
+ "HARNESS_TEMPLATE_MULTI",
49
+ "FIX_TEMPLATE_MULTI",
50
+ "HARNESS_ADDENDUM",
51
+ "HARNESS_ADDENDUM_MULTI",
52
+ "TEST_CASES_TEMPLATE",
53
+ "_TEST_CASES_TEMPLATE",
54
+ ]
55
+
56
+ # Backward-compatible private aliases used in analyzer.py
57
+ _HARNESS_TEMPLATE = HARNESS_TEMPLATE
58
+ _FIX_TEMPLATE = FIX_TEMPLATE
59
+ _TEST_CASES_TEMPLATE = TEST_CASES_TEMPLATE
@@ -0,0 +1,15 @@
1
+ """
2
+ coreinsight/prompts/_base.py
3
+ Shared constants used across all prompt modules.
4
+ """
5
+
6
+ class ModelTier:
7
+ SMALL = "small" # 7B and under: codellama:7b, llama3.2:3b
8
+ MEDIUM = "medium" # 13B-34B: mistral, codellama:13b, local_server models
9
+ LARGE = "large" # 70B+, cloud: GPT-4, Claude, Gemini
10
+
11
+ SYSTEM_PROMPT = """
12
+ You are a Senior HPC Performance Engineer, an elite, strict HPC Performance Architect, an elite Algorithmic Expert, and a strict Code Reviewer.
13
+ Your goal is to optimize Python, C++, and CUDA code for maximum throughput and low latency, and perfect hardware utilization.
14
+ You know that the greatest hardware bottleneck is a mathematically inefficient algorithm. You ruthlessly identify O(N^2) nested loops, memory inefficiencies, performance bottlenecks and suboptimal data structures, upgrading them to O(N) or O(1) solutions using vectorization, hash maps, or low-level C-backed libraries.
15
+ """