cognitive-cache 0.1.1__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 (40) hide show
  1. cognitive_cache-0.1.1/LICENSE +190 -0
  2. cognitive_cache-0.1.1/PKG-INFO +263 -0
  3. cognitive_cache-0.1.1/README.md +234 -0
  4. cognitive_cache-0.1.1/pyproject.toml +55 -0
  5. cognitive_cache-0.1.1/src/cognitive_cache/__init__.py +12 -0
  6. cognitive_cache-0.1.1/src/cognitive_cache/api.py +265 -0
  7. cognitive_cache-0.1.1/src/cognitive_cache/baselines/__init__.py +0 -0
  8. cognitive_cache-0.1.1/src/cognitive_cache/baselines/base.py +11 -0
  9. cognitive_cache-0.1.1/src/cognitive_cache/baselines/embedding_select.py +37 -0
  10. cognitive_cache-0.1.1/src/cognitive_cache/baselines/full_stuff.py +19 -0
  11. cognitive_cache-0.1.1/src/cognitive_cache/baselines/grep_select.py +32 -0
  12. cognitive_cache-0.1.1/src/cognitive_cache/baselines/llm_triage.py +42 -0
  13. cognitive_cache-0.1.1/src/cognitive_cache/baselines/random_select.py +26 -0
  14. cognitive_cache-0.1.1/src/cognitive_cache/cli.py +141 -0
  15. cognitive_cache-0.1.1/src/cognitive_cache/core/__init__.py +0 -0
  16. cognitive_cache-0.1.1/src/cognitive_cache/core/chunker.py +155 -0
  17. cognitive_cache-0.1.1/src/cognitive_cache/core/orderer.py +30 -0
  18. cognitive_cache-0.1.1/src/cognitive_cache/core/selector.py +112 -0
  19. cognitive_cache-0.1.1/src/cognitive_cache/core/value_function.py +96 -0
  20. cognitive_cache-0.1.1/src/cognitive_cache/indexer/__init__.py +0 -0
  21. cognitive_cache-0.1.1/src/cognitive_cache/indexer/git_analyzer.py +113 -0
  22. cognitive_cache-0.1.1/src/cognitive_cache/indexer/graph_builder.py +139 -0
  23. cognitive_cache-0.1.1/src/cognitive_cache/indexer/repo_indexer.py +103 -0
  24. cognitive_cache-0.1.1/src/cognitive_cache/indexer/token_counter.py +19 -0
  25. cognitive_cache-0.1.1/src/cognitive_cache/llm/__init__.py +0 -0
  26. cognitive_cache-0.1.1/src/cognitive_cache/llm/adapter.py +37 -0
  27. cognitive_cache-0.1.1/src/cognitive_cache/llm/claude_adapter.py +45 -0
  28. cognitive_cache-0.1.1/src/cognitive_cache/llm/llamacpp_adapter.py +72 -0
  29. cognitive_cache-0.1.1/src/cognitive_cache/llm/openai_adapter.py +45 -0
  30. cognitive_cache-0.1.1/src/cognitive_cache/mcp_server.py +100 -0
  31. cognitive_cache-0.1.1/src/cognitive_cache/models.py +59 -0
  32. cognitive_cache-0.1.1/src/cognitive_cache/py.typed +0 -0
  33. cognitive_cache-0.1.1/src/cognitive_cache/signals/__init__.py +0 -0
  34. cognitive_cache-0.1.1/src/cognitive_cache/signals/base.py +27 -0
  35. cognitive_cache-0.1.1/src/cognitive_cache/signals/change_recency.py +19 -0
  36. cognitive_cache-0.1.1/src/cognitive_cache/signals/embedding_sim.py +63 -0
  37. cognitive_cache-0.1.1/src/cognitive_cache/signals/file_role_prior.py +60 -0
  38. cognitive_cache-0.1.1/src/cognitive_cache/signals/graph_distance.py +41 -0
  39. cognitive_cache-0.1.1/src/cognitive_cache/signals/redundancy.py +29 -0
  40. cognitive_cache-0.1.1/src/cognitive_cache/signals/symbol_overlap.py +32 -0
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to the Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by the Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding any notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 Anish S.
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
@@ -0,0 +1,263 @@
1
+ Metadata-Version: 2.4
2
+ Name: cognitive-cache
3
+ Version: 0.1.1
4
+ Summary: Optimal context orchestration for LLMs
5
+ Author: Anish Shrestha
6
+ License-Expression: MIT
7
+ License-File: LICENSE
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
14
+ Classifier: Intended Audience :: Developers
15
+ Requires-Dist: tiktoken>=0.7.0
16
+ Requires-Dist: scikit-learn>=1.4.0
17
+ Requires-Dist: networkx>=3.2
18
+ Requires-Dist: numpy>=1.26.0
19
+ Requires-Dist: anthropic>=0.40.0 ; extra == 'benchmark'
20
+ Requires-Dist: openai>=1.50.0 ; extra == 'benchmark'
21
+ Requires-Dist: mcp>=1.0.0 ; extra == 'mcp'
22
+ Requires-Python: >=3.11
23
+ Project-URL: Homepage, https://github.com/Anyesh/cognitive-cache
24
+ Project-URL: Repository, https://github.com/Anyesh/cognitive-cache
25
+ Project-URL: Issues, https://github.com/Anyesh/cognitive-cache/issues
26
+ Provides-Extra: benchmark
27
+ Provides-Extra: mcp
28
+ Description-Content-Type: text/markdown
29
+
30
+ # cognitive-cache
31
+
32
+ Every LLM tool right now (Cursor, Claude Code, Copilot, all of them) decides what to put in the context window using heuristics: grep for some symbols, embed and cosine-similarity search, or just cram as many files as will fit. Nobody has an actual algorithm for this.
33
+
34
+ This project is an attempt to build one.
35
+
36
+ **No any LLM calls or API keys or cloud. It runs entirely local.**
37
+
38
+ ![cognitive-cache finding the right files for a real GitHub issue](demo/demo.gif)
39
+
40
+ ## the problem: context as an os-level resource
41
+
42
+ Think of it this way:
43
+
44
+ | Classic OS | LLM Equivalent | Current State |
45
+ |---|---|---|
46
+ | RAM | Context window | Manually managed |
47
+ | Virtual memory / page swaps | Context eviction + retrieval | Crude summarization |
48
+ | Process scheduler | Agent orchestration | Hand-coded loops |
49
+ | File system cache | Knowledge retrieval | Cosine similarity |
50
+ | Memory allocator | Token budget allocation | Nobody does this |
51
+
52
+ Early computers had programmers manually managing memory addresses. Then virtual memory was invented, a single algorithm, and it unlocked everything we know as modern computing.
53
+
54
+ **The LLM ecosystem is at the "manual memory management" stage right now.** Context is the single most important resource where reasoning happens, yet every tool manages it with heuristics instead of optimization.
55
+
56
+ Cognitive-cache is building the "virtual memory" for LLM reasoning.
57
+
58
+ ## what it does
59
+
60
+ Given a task (like a GitHub issue) and a codebase, cognitive-cache picks which files to include in the LLM's context window. It combines multiple signals (symbol matching, dependency graph distance, git recency, semantic similarity, redundancy penalties) into a scoring function and runs greedy submodular optimization to select the highest-value set of files that fits within a token budget.
61
+
62
+ The key insight is treating context selection as a **constrained optimization problem** rather than a retrieval problem. RAG systems ask "what's most similar to the query?" but what you actually want is "what maximizes the chance the model gets this right?" Those are different questions.
63
+
64
+ ## benchmark results
65
+
66
+ Benchmarked on **23 real bug-fix PRs** across 8 open-source repositories. For each issue, we run every strategy with a 12K token budget and measure **file recall**: did the algorithm select the files that were actually modified in the fix?
67
+
68
+ ### overall performance
69
+
70
+ | Strategy | Avg Recall | Median | Head-to-head vs cognitive-cache |
71
+ |---|---|---|---|
72
+ | **cognitive-cache** | **34.7%** | **40%** | |
73
+ | llm-triage | 25.7% | 20% | 8W / 11T / 4L |
74
+ | embedding (RAG) | 25.9% | 0% | 9W / 9T / 5L |
75
+ | grep | 22.8% | 0% | 7W / 15T / 1L |
76
+ | random | 4.7% | 0% | 12W / 11T / 0L |
77
+ | full-stuff | 2.2% | 0% | 13W / 10T / 0L |
78
+
79
+ cognitive-cache **never loses to random or full-stuff** and has a winning record against every baseline, including llm-triage (asking the LLM to pick its own files), which is the hardest one to beat.
80
+
81
+ ### per-repo breakdown
82
+
83
+ | Repository | cognitive-cache | llm-triage | embedding | grep | Issues |
84
+ |---|---|---|---|---|---|
85
+ | Textualize/rich | 100% | 0% | 50% | 100% | 1 |
86
+ | pallets/werkzeug | 62% | 38% | 44% | 44% | 4 |
87
+ | psf/requests | 50% | 25% | 25% | 25% | 2 |
88
+ | pallets/flask | 38% | 36% | 23% | 22% | 3 |
89
+ | pallets/click | 33% | 33% | 0% | 33% | 1 |
90
+ | pallets/jinja | 20% | 40% | 20% | 20% | 5 |
91
+ | fastify/fastify | 17% | 0% | 25% | 0% | 6 |
92
+ | tiangolo/fastapi | 0% | 50% | 0% | 0% | 1 |
93
+
94
+ ### per-issue detail
95
+
96
+ Every issue, every strategy, full transparency:
97
+
98
+ | Issue | CC | LLM | Emb | Grep | Rand | Full |
99
+ |---|---|---|---|---|---|---|
100
+ | Textualize/rich#4006 | **100%** | 0% | 50% | 100% | 0% | 0% |
101
+ | fastify/fastify#6013 | 0% | 0% | 0% | 0% | 0% | 0% |
102
+ | fastify/fastify#6021 | **50%** | 0% | 0% | 0% | 0% | 0% |
103
+ | fastify/fastify#6026 | 0% | 0% | 0% | 0% | 0% | 0% |
104
+ | fastify/fastify#6030 | 0% | 0% | **50%** | 0% | 0% | 0% |
105
+ | fastify/fastify#6064 | 0% | 0% | 0% | 0% | 0% | 0% |
106
+ | fastify/fastify#6613 | 50% | 0% | **100%** | 0% | 0% | 0% |
107
+ | pallets/click#3225 | 33% | 33% | 0% | 33% | 33% | 0% |
108
+ | pallets/flask#5899 | 50% | 50% | 50% | 0% | 0% | 0% |
109
+ | pallets/flask#5917 | **40%** | 20% | 20% | 40% | 0% | 0% |
110
+ | pallets/flask#5928 | 25% | **38%** | 0% | 25% | 0% | 0% |
111
+ | pallets/jinja#1663 | 0% | **100%** | 50% | 50% | 0% | 0% |
112
+ | pallets/jinja#1665 | 0% | 0% | 0% | 0% | 0% | 0% |
113
+ | pallets/jinja#1706 | 0% | 0% | **50%** | 0% | 0% | 0% |
114
+ | pallets/jinja#1852 | 0% | **50%** | 0% | 0% | 0% | 0% |
115
+ | pallets/jinja#2061 | **100%** | 50% | 0% | 50% | 0% | 0% |
116
+ | pallets/werkzeug#3038 | 50% | 50% | **75%** | 25% | 25% | 0% |
117
+ | pallets/werkzeug#3078 | 50% | 50% | 50% | 50% | 0% | 0% |
118
+ | pallets/werkzeug#3080 | **50%** | 0% | 0% | 0% | 0% | 0% |
119
+ | pallets/werkzeug#3129 | **100%** | 50% | 50% | 100% | 0% | 0% |
120
+ | psf/requests#7308 | 50% | 50% | 0% | 0% | 0% | 50% |
121
+ | psf/requests#7309 | 50% | 0% | 50% | 50% | 50% | 0% |
122
+ | tiangolo/fastapi#15139 | 0% | **50%** | 0% | 0% | 0% | 0% |
123
+
124
+ Bold = best recall for that issue. All runs use Qwen 3.5 9B (Q4_K_M) via llama.cpp at zero API cost.
125
+
126
+ ### what the baselines do
127
+
128
+ - **random**: picks files at random until the token budget is full
129
+ - **full-stuff**: crams files alphabetically until the budget is full, which is roughly what most tools approximate
130
+ - **embedding (RAG)**: TF-IDF cosine similarity (scikit-learn, up to 5K features) between the issue text and file contents. This uses TF-IDF rather than neural embeddings, so it's a simpler version of what most RAG tools do; real-world RAG with a proper embedding model would likely score somewhat higher
131
+ - **grep**: searches for symbols and identifiers mentioned in the issue
132
+ - **llm-triage**: gives the LLM (same Qwen 3.5 9B) the issue plus a full file listing and asks it to pick the most relevant files. This simulates what tools like Cursor and Claude Code do when they decide what to read, and is the hardest baseline to beat
133
+
134
+ ## how it works
135
+
136
+ Six signals score each file:
137
+
138
+ - **symbol overlap**: does this file define or mention identifiers from the task? Strongest signal when it fires, so it gets the highest weight
139
+ - **graph distance**: how many imports away is this file from the ones the task mentions? Built with networkx from the dependency graph
140
+ - **change recency**: files changed recently in git tend to be more relevant, and more likely to contain the bug
141
+ - **redundancy penalty**: if you already selected a file with similar symbols, this one becomes less valuable, which prevents the algorithm from blowing the budget on five files from the same directory
142
+ - **embedding similarity**: TF-IDF cosine similarity, essentially what RAG does. Weighted low because we're trying to beat this, not replicate it
143
+ - **file role prior**: test files, type definitions, and config files get small baseline boosts since they tend to be informative
144
+
145
+ These get combined into a weighted score, and a greedy selector picks files one at a time, re-evaluating redundancy after each pick. If a file is too large to fit (like a 13K token app.py when your budget is 12K), it gets chunked to extract just the relevant functions.
146
+
147
+ ## using it
148
+
149
+ ### as a library
150
+
151
+ ```python
152
+ from cognitive_cache import select_context_from_repo
153
+
154
+ result = select_context_from_repo(".", "fix the login redirect bug")
155
+ for item in result.selected:
156
+ print(f"{item.source.path} (score: {item.score:.3f})")
157
+ ```
158
+
159
+ For repeated queries against the same repo, build the index once and reuse it:
160
+
161
+ ```python
162
+ from cognitive_cache import RepoIndex, select_context
163
+
164
+ index = RepoIndex.build(".")
165
+ r1 = select_context(index, "fix the login bug")
166
+ r2 = select_context(index, "add rate limiting to the API")
167
+ ```
168
+
169
+ ### as a CLI
170
+
171
+ ```
172
+ uv sync
173
+ uv run cognitive-cache select --repo . --task "fix the login redirect bug"
174
+ uv run cognitive-cache select --repo . --task "fix login" --json # machine-readable
175
+ uv run cognitive-cache select --repo . --task "fix login" --output ctx.txt # dump context to file
176
+ ```
177
+
178
+ ### as an MCP server (for Claude Code, Cursor, etc.)
179
+
180
+ ```
181
+ uv sync --extra mcp
182
+ uv run cognitive-cache-mcp # starts stdio server
183
+ ```
184
+
185
+ Add to your Claude Code MCP settings:
186
+
187
+ ```json
188
+ {
189
+ "mcpServers": {
190
+ "cognitive-cache": {
191
+ "command": "uv",
192
+ "args": ["run", "--project", "/path/to/cognitive-cache", "--extra", "mcp", "cognitive-cache-mcp"]
193
+ }
194
+ }
195
+ }
196
+ ```
197
+
198
+ ### as a GitHub Action
199
+
200
+ The included workflow (`.github/workflows/context-suggest.yml`) automatically comments on new issues with the most relevant files. It runs entirely in CI with no API keys required.
201
+
202
+ ## running the benchmark
203
+
204
+ ```
205
+ uv sync --dev
206
+ uv run pytest tests/ # 97 tests
207
+ ```
208
+
209
+ To run the benchmark with a local llama.cpp server:
210
+
211
+ ```
212
+ LLAMACPP_BASE_URL=http://localhost:8080 PYTHONPATH=. uv run python benchmark/run_local.py # full 23-issue benchmark
213
+ PYTHONPATH=. uv run python benchmark/run_test.py # quick 3-issue test
214
+ ```
215
+
216
+ Configure the llama.cpp connection with environment variables:
217
+
218
+ | Variable | Default | Description |
219
+ |---|---|---|
220
+ | `LLAMACPP_BASE_URL` | `http://localhost:8080` | llama.cpp server URL |
221
+ | `LLAMACPP_MODEL` | `Qwen3.5-9B-Q4_K_M` | Model name to request |
222
+
223
+ To expand the benchmark dataset (needs a GitHub token for API access):
224
+
225
+ ```
226
+ GITHUB_TOKEN=ghp_xxx uv run python benchmark/curate_dataset.py
227
+ ```
228
+
229
+ ## project structure
230
+
231
+ ```
232
+ src/cognitive_cache/
233
+ api.py # public API: RepoIndex, select_context
234
+ cli.py # CLI entry point
235
+ mcp_server.py # MCP server for Claude Code / Cursor
236
+ models.py # core types (Source, Task, ScoredSource, SelectionResult)
237
+ indexer/ # turns a repo directory into a list of Source objects
238
+ signals/ # the six scoring signals
239
+ core/ # value function, greedy selector, file chunker
240
+ baselines/ # the five baseline strategies we benchmark against
241
+ llm/ # adapters for calling LLMs (claude, openai, llama.cpp)
242
+ benchmark/
243
+ dataset/ # curated github issues with known fixes
244
+ runner.py # orchestrates benchmark runs
245
+ evaluator.py # computes recall and efficiency metrics
246
+ ```
247
+
248
+ ## whats next
249
+
250
+ - Weight tuning on a larger dataset, since the signal weights are currently hand-tuned
251
+ - Improving the test-vs-source ranking for bug-fix tasks (the algorithm sometimes ranks test files above the source files that need fixing)
252
+ - Adaptive replanning that re-optimizes context mid-conversation after the model calls a tool or asks a followup
253
+ - Task-aware compression that goes beyond chunking to actually compress file content while preserving what's relevant
254
+
255
+ ## why this matters
256
+
257
+ Context windows are getting bigger (1M tokens, soon more) but bigger doesn't mean the problem goes away. More capacity means more choices, and just stuffing everything in wastes compute while actively degrading output quality because the model's attention gets diluted. The optimization becomes more valuable as windows grow, not less.
258
+
259
+ If there's going to be one algorithm at the center of how LLM tools work, it should probably be this one. Or something like it.
260
+
261
+ ## license
262
+
263
+ Apache 2.0. See [LICENSE](LICENSE).