context-engineering-core 0.12.0__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 (93) hide show
  1. context_engineering_core-0.12.0/.gitignore +21 -0
  2. context_engineering_core-0.12.0/CHANGELOG.md +81 -0
  3. context_engineering_core-0.12.0/LICENSE +202 -0
  4. context_engineering_core-0.12.0/PKG-INFO +203 -0
  5. context_engineering_core-0.12.0/README.md +167 -0
  6. context_engineering_core-0.12.0/examples/evidence_answer_policy.py +31 -0
  7. context_engineering_core-0.12.0/examples/incident-demo.json +26 -0
  8. context_engineering_core-0.12.0/examples/inspection-input.json +40 -0
  9. context_engineering_core-0.12.0/examples/rolling_summary.py +71 -0
  10. context_engineering_core-0.12.0/examples/sdk_quickstart.py +97 -0
  11. context_engineering_core-0.12.0/examples/service-config.example.json +7 -0
  12. context_engineering_core-0.12.0/examples/structured_answers.py +94 -0
  13. context_engineering_core-0.12.0/examples/validated_answer.py +32 -0
  14. context_engineering_core-0.12.0/pyproject.toml +67 -0
  15. context_engineering_core-0.12.0/src/context_engine/__init__.py +15 -0
  16. context_engineering_core-0.12.0/src/context_engine/__main__.py +3 -0
  17. context_engineering_core-0.12.0/src/context_engine/_sqlite.py +19 -0
  18. context_engineering_core-0.12.0/src/context_engine/_windows_security.py +125 -0
  19. context_engineering_core-0.12.0/src/context_engine/_windows_storage.py +110 -0
  20. context_engineering_core-0.12.0/src/context_engine/adapters.py +148 -0
  21. context_engineering_core-0.12.0/src/context_engine/answers.py +90 -0
  22. context_engineering_core-0.12.0/src/context_engine/budget.py +93 -0
  23. context_engineering_core-0.12.0/src/context_engine/cli.py +222 -0
  24. context_engineering_core-0.12.0/src/context_engine/config.py +193 -0
  25. context_engineering_core-0.12.0/src/context_engine/context.py +116 -0
  26. context_engineering_core-0.12.0/src/context_engine/demo.py +98 -0
  27. context_engineering_core-0.12.0/src/context_engine/errors.py +91 -0
  28. context_engineering_core-0.12.0/src/context_engine/evaluation/__init__.py +1 -0
  29. context_engineering_core-0.12.0/src/context_engine/evaluation/commands.py +90 -0
  30. context_engineering_core-0.12.0/src/context_engine/evaluation/corpus.py +247 -0
  31. context_engineering_core-0.12.0/src/context_engine/evaluation/data/freeze.json +26 -0
  32. context_engineering_core-0.12.0/src/context_engine/evaluation/data/ground_truth.json +287 -0
  33. context_engineering_core-0.12.0/src/context_engine/evaluation/data/pin_retention.json +8 -0
  34. context_engineering_core-0.12.0/src/context_engine/evaluation/data/protocol.json +122 -0
  35. context_engineering_core-0.12.0/src/context_engine/evaluation/data/scenario.json +1226 -0
  36. context_engineering_core-0.12.0/src/context_engine/evaluation/data/summary_fixture.json +7 -0
  37. context_engineering_core-0.12.0/src/context_engine/evaluation/execution.py +538 -0
  38. context_engineering_core-0.12.0/src/context_engine/evaluation/fixtures.py +32 -0
  39. context_engineering_core-0.12.0/src/context_engine/evaluation/grading.py +62 -0
  40. context_engineering_core-0.12.0/src/context_engine/evaluation/harness.py +165 -0
  41. context_engineering_core-0.12.0/src/context_engine/evaluation/heldout/__init__.py +260 -0
  42. context_engineering_core-0.12.0/src/context_engine/evaluation/heldout/__main__.py +40 -0
  43. context_engineering_core-0.12.0/src/context_engine/evaluation/heldout/data/ground_truth.json +18 -0
  44. context_engineering_core-0.12.0/src/context_engine/evaluation/heldout/data/manifest.json +9 -0
  45. context_engineering_core-0.12.0/src/context_engine/evaluation/heldout/data/protocol.json +14 -0
  46. context_engineering_core-0.12.0/src/context_engine/evaluation/heldout/data/scenarios.json +18 -0
  47. context_engineering_core-0.12.0/src/context_engine/evaluation/heldout/data/scoring_manifest.json +4 -0
  48. context_engineering_core-0.12.0/src/context_engine/evaluation/heldout/data/scoring_protocol.json +12 -0
  49. context_engineering_core-0.12.0/src/context_engine/evaluation/heldout/scoring.py +176 -0
  50. context_engineering_core-0.12.0/src/context_engine/evaluation/protocol.py +264 -0
  51. context_engineering_core-0.12.0/src/context_engine/evaluation/reporting.py +294 -0
  52. context_engineering_core-0.12.0/src/context_engine/evaluation/results.py +331 -0
  53. context_engineering_core-0.12.0/src/context_engine/evaluation/run_commands.py +146 -0
  54. context_engineering_core-0.12.0/src/context_engine/evaluation/strategies.py +261 -0
  55. context_engineering_core-0.12.0/src/context_engine/inspection.py +266 -0
  56. context_engineering_core-0.12.0/src/context_engine/layers/__init__.py +18 -0
  57. context_engineering_core-0.12.0/src/context_engine/layers/cap.py +99 -0
  58. context_engineering_core-0.12.0/src/context_engine/layers/common.py +162 -0
  59. context_engineering_core-0.12.0/src/context_engine/layers/pin.py +68 -0
  60. context_engineering_core-0.12.0/src/context_engine/layers/retrieve.py +233 -0
  61. context_engineering_core-0.12.0/src/context_engine/layers/summarize.py +202 -0
  62. context_engineering_core-0.12.0/src/context_engine/layers/window.py +29 -0
  63. context_engineering_core-0.12.0/src/context_engine/mcp_server.py +267 -0
  64. context_engineering_core-0.12.0/src/context_engine/memory/__init__.py +5 -0
  65. context_engineering_core-0.12.0/src/context_engine/memory/codec.py +91 -0
  66. context_engineering_core-0.12.0/src/context_engine/memory/demo.py +90 -0
  67. context_engineering_core-0.12.0/src/context_engine/memory/store.py +770 -0
  68. context_engineering_core-0.12.0/src/context_engine/models.py +384 -0
  69. context_engineering_core-0.12.0/src/context_engine/pipeline.py +383 -0
  70. context_engineering_core-0.12.0/src/context_engine/providers/__init__.py +1 -0
  71. context_engineering_core-0.12.0/src/context_engine/providers/client.py +301 -0
  72. context_engineering_core-0.12.0/src/context_engine/providers/commands.py +138 -0
  73. context_engineering_core-0.12.0/src/context_engine/providers/contracts.py +324 -0
  74. context_engineering_core-0.12.0/src/context_engine/providers/credentials.py +66 -0
  75. context_engineering_core-0.12.0/src/context_engine/providers/memory.py +112 -0
  76. context_engineering_core-0.12.0/src/context_engine/providers/store.py +408 -0
  77. context_engineering_core-0.12.0/src/context_engine/providers/transport.py +195 -0
  78. context_engineering_core-0.12.0/src/context_engine/providers/windows_credentials.py +23 -0
  79. context_engineering_core-0.12.0/src/context_engine/py.typed +0 -0
  80. context_engineering_core-0.12.0/src/context_engine/service/__init__.py +1 -0
  81. context_engineering_core-0.12.0/src/context_engine/service/__main__.py +68 -0
  82. context_engineering_core-0.12.0/src/context_engine/service/app.py +404 -0
  83. context_engineering_core-0.12.0/src/context_engine/service/auth.py +132 -0
  84. context_engineering_core-0.12.0/src/context_engine/service/client.py +52 -0
  85. context_engineering_core-0.12.0/src/context_engine/service/control.py +134 -0
  86. context_engineering_core-0.12.0/src/context_engine/service/demo.py +151 -0
  87. context_engineering_core-0.12.0/src/context_engine/service/integrations.py +28 -0
  88. context_engineering_core-0.12.0/src/context_engine/service/schemas.py +59 -0
  89. context_engineering_core-0.12.0/src/context_engine/service/worker.py +59 -0
  90. context_engineering_core-0.12.0/src/context_engine/service/workers.py +156 -0
  91. context_engineering_core-0.12.0/src/context_engine/summaries.py +196 -0
  92. context_engineering_core-0.12.0/src/context_engine/tokens.py +171 -0
  93. context_engineering_core-0.12.0/src/context_engine/work.py +46 -0
@@ -0,0 +1,21 @@
1
+ .DS_Store
2
+ .env
3
+ .env.*
4
+ !.env.example
5
+ .venv/
6
+ __pycache__/
7
+ *.py[cod]
8
+ .pytest_cache/
9
+ .ruff_cache/
10
+ .mypy_cache/
11
+ .hypothesis/
12
+ *.egg-info/
13
+ build/
14
+ dist/
15
+ output/*.sqlite*
16
+ output/*.log
17
+ output/private/
18
+ **/output/*.sqlite*
19
+ **/output/*.log
20
+ **/output/private/
21
+ brain/index.json
@@ -0,0 +1,81 @@
1
+ # Changelog
2
+
3
+ Earlier versions (0.1.0–0.9.6) are documented in `docs/C01_REPORT.md` through
4
+ `docs/C10_STORAGE_ACL_REPORT.md` and preserved as wheels under `archives/`.
5
+
6
+ ## 0.12.0 — 2026-10-31 (unreleased)
7
+
8
+ ### Added
9
+ - `context_engine.adapters`: `openai_chat`, `anthropic_messages`,
10
+ `langchain_messages` and `turns_from_openai`. No new core dependencies; optional
11
+ `[langchain]` extra (`langchain-core>=1.0,<2`).
12
+ - Inception (Mercury) provider: `InceptionGenerationConfig` (mercury-3/2.5/2,
13
+ temperature 0.5–1, reasoning instant–high) and `InceptionTransport`, reusing the
14
+ bounded client, ledger and metered spend cap. `scripts/inception_smoke.py` makes
15
+ one capped call only with `--allow-live`; live smoke passed (D111).
16
+ - `context-engine-mcp`: prototype MCP server over `MemoryStore` (stdio JSON-RPC,
17
+ standard library only) with `add_message`, `pin_fact`, `get_context`, `forget`.
18
+ See `docs/MCP.md`.
19
+
20
+ ### Changed (performance)
21
+ - Bounded process-wide token-count cache (32,768 entries, texts up to 4,096
22
+ characters). Counts and assembly output are identical; benchmark gates unchanged.
23
+ All-layers assembly on the 100-turn benchmark (same history, new questions):
24
+ median 247 → 95 ms at 900 tokens, 481 → 208 ms at 3000 tokens.
25
+
26
+ ### Changed
27
+ - Provider transports carry their own endpoint/adapter identity (Groq request keys
28
+ are unchanged). HTTP 402 now maps to `billing_or_quota`.
29
+
30
+ ### Unchanged
31
+ - Assembly output and defaults. 0.11.0 wheel/freeze archived in
32
+ `archives/c12-adapters-0120/`.
33
+
34
+ ## 0.11.0 — 2026-10-31 (TestPyPI)
35
+
36
+ ### Changed (behavior)
37
+ - `RetrievalConfig.recover_capped_window` now defaults to `True` (owner decision
38
+ D109). RETRIEVE can restore original evidence that CAP removed from recent
39
+ WINDOW turns. Offline retention on the frozen corpus: 31/31 at 900 and 3000
40
+ tokens (was 29/31 and 19/31), for about 5–15% more assembly latency. Pass
41
+ `RetrievalConfig(recover_capped_window=False)` for the old behavior.
42
+ - Existing `MemoryStore` chunk indexes are rebuilt once after upgrading, because
43
+ the index profile hashes the full retrieval configuration.
44
+ - `strategy-eval`: `combined` now uses the new defaults; `combined_recover` is
45
+ replaced by `combined_no_recover` (the 0.10.0 defaults).
46
+
47
+ ### Added
48
+ - Apache-2.0 `LICENSE`; license, classifiers and project URLs in package metadata.
49
+ - Newcomer README with a tested quick example (`tests/test_readme_example.py`).
50
+
51
+ ### Unchanged
52
+ - The frozen benchmark harness pins `recover_capped_window=False`, so A0–A5 and
53
+ historical gates reproduce. 0.10.0 wheel/freeze archived in
54
+ `archives/c11-recover-default-0110/`.
55
+
56
+ ## 0.10.0 — 2026-10-31 (unreleased)
57
+
58
+ ### Added
59
+ - `context_engine.summaries`: `update_summary` for explicit, amortized summary
60
+ refresh; `ExtractiveSummarizer` (deterministic, model-free); `PromptSummarizer`
61
+ (wraps any `complete(messages) -> str` function; no provider SDK imported).
62
+ - `PrefixSummaryPolicy` and `SummarySnapshot.covers_prefix`: opt-in reuse of a
63
+ summary that covers the oldest omitted turns. New diagnostic reason
64
+ `summary_reused_prefix`.
65
+ - `MemoryStore.assemble(..., summary_prefix=True)`.
66
+ - `context-engine strategy-eval`: offline comparison of full history, WINDOW,
67
+ WINDOW+summary, WINDOW+retrieval, all layers, and all layers with
68
+ `recover_capped_window`, with retention, chunk precision, turn recall, token
69
+ reduction and latency.
70
+ - `examples/rolling_summary.py`.
71
+
72
+ ### Changed
73
+ - `MemoryStore` keeps a stored summary when the appended, updated or deleted turn
74
+ is not covered by it. Changing or deleting a covered turn still removes it, and
75
+ lineage is still re-verified on every read.
76
+ - `pytest` now works without `python -m` (`pythonpath = ["."]`).
77
+
78
+ ### Unchanged
79
+ - Default `assemble_context` output, `FrozenSummaryPolicy` exact matching, and
80
+ `recover_capped_window=False` default.
81
+ - Historical experiments: 0.9.6 wheel/freeze archived in `archives/c11-summaries-0100/`.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,203 @@
1
+ Metadata-Version: 2.5
2
+ Name: context-engineering-core
3
+ Version: 0.12.0
4
+ Summary: Provider-independent context layers, source contracts, and token budget accounting
5
+ Project-URL: Homepage, https://github.com/Nandu1729/Context-Engine
6
+ Project-URL: Repository, https://github.com/Nandu1729/Context-Engine
7
+ Project-URL: Issues, https://github.com/Nandu1729/Context-Engine/issues
8
+ Project-URL: Changelog, https://github.com/Nandu1729/Context-Engine/blob/main/CHANGELOG.md
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Keywords: context-window,llm,memory,retrieval,summarization,token-budget
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Classifier: Typing :: Typed
20
+ Requires-Python: >=3.12
21
+ Requires-Dist: pywin32==311; sys_platform == 'win32'
22
+ Requires-Dist: rank-bm25<0.3,>=0.2.2
23
+ Requires-Dist: tiktoken<0.15,>=0.14
24
+ Provides-Extra: langchain
25
+ Requires-Dist: langchain-core<2,>=1.0; extra == 'langchain'
26
+ Provides-Extra: provider
27
+ Requires-Dist: httpx<0.29,>=0.28; extra == 'provider'
28
+ Provides-Extra: reports
29
+ Requires-Dist: matplotlib<3.12,>=3.11; extra == 'reports'
30
+ Provides-Extra: service
31
+ Requires-Dist: fastapi<0.136,>=0.135; extra == 'service'
32
+ Requires-Dist: httpx<0.29,>=0.28; extra == 'service'
33
+ Requires-Dist: pyjwt[crypto]<3,>=2.12; extra == 'service'
34
+ Requires-Dist: uvicorn<0.42,>=0.41; extra == 'service'
35
+ Description-Content-Type: text/markdown
36
+
37
+ # Context Engine
38
+
39
+ Fit long conversations into a fixed token budget for any LLM, and know exactly
40
+ where every piece of context came from.
41
+
42
+ `context-engineering-core` is a small Python library. You give it the full chat
43
+ history, the new question and a token budget. It returns a list of chat messages
44
+ that fits the budget. Those messages keep the recent turns, the facts you pinned,
45
+ and the older evidence that matches the question. It never calls a model by
46
+ itself, so you send the messages to any provider you like.
47
+
48
+ > **Status: alpha (0.12.0).** Good for developers to try locally. It is not yet
49
+ > proven on real-world data, and there is no hosted service. See
50
+ > [What is not proven yet](#what-is-not-proven-yet).
51
+
52
+ ## Install
53
+
54
+ Requires Python 3.12+.
55
+
56
+ ```sh
57
+ pip install context-engineering-core # after the first PyPI release
58
+ ```
59
+
60
+ From source:
61
+
62
+ ```sh
63
+ git clone https://github.com/Nandu1729/Context-Engine.git
64
+ cd Context-Engine
65
+ uv sync --locked
66
+ uv run context-engine check # prints "deps ok"
67
+ ```
68
+
69
+ The first run downloads the tokenizer vocabulary (tiktoken `o200k_harmony`) once.
70
+ After that, everything works offline.
71
+
72
+ ## Quick example
73
+
74
+ ```python
75
+ from context_engine import assemble_context
76
+ from context_engine.config import BudgetConfig
77
+ from context_engine.models import KeyedPins, Message, Role, Scope, Turn
78
+
79
+ scope = Scope("my-app", "chat-1")
80
+ notes = [f"Note {i}: shipment {i} left from dock D-{i}." for i in range(300)]
81
+ history = [Turn(f"t{i}", scope, (Message(f"m{i}", Role.USER, text),)) for i, text in enumerate(notes)]
82
+
83
+ result = assemble_context(history, "Which dock did shipment 42 leave from?", KeyedPins(scope),
84
+ BudgetConfig(input_cap=900), system="Answer only from the context.")
85
+ print(result.diagnostics.estimate.estimated_tokens, "of", result.diagnostics.input_allowance)
86
+ print("evidence from turns:", result.diagnostics.retrieved_turn_ids)
87
+ messages = result.messages # ready for any chat-completions API
88
+ ```
89
+
90
+ The 300 notes are far over the 900-token budget. The result fits (about 716 of
91
+ 900 tokens), and turn `t42`, the one with the answer, is among the retrieved
92
+ evidence. Then send `messages` to OpenAI, Anthropic, Groq or a local model.
93
+
94
+ ## Use it with your provider
95
+
96
+ `context_engine.adapters` reshapes the result for common clients. None of them is
97
+ a dependency of the engine:
98
+
99
+ ```python
100
+ from context_engine.adapters import anthropic_messages, langchain_messages, openai_chat, turns_from_openai
101
+
102
+ openai_client.chat.completions.create(model="...", **openai_chat(result)) # OpenAI, Groq, local servers
103
+ anthropic_client.messages.create(model="...", max_tokens=512, **anthropic_messages(result))
104
+ chat_model.invoke(langchain_messages(result)) # pip install 'context-engineering-core[langchain]'
105
+
106
+ history = turns_from_openai(existing_openai_messages, scope) # reuse a chat history you already have
107
+ ```
108
+
109
+ ### Use it from an MCP client
110
+
111
+ `context-engine-mcp --data-dir ./context-memory` runs a local MCP server (stdio) with
112
+ `add_message`, `pin_fact`, `get_context` and `forget` tools. Prototype; see the
113
+ [MCP guide](https://github.com/Nandu1729/Context-Engine/blob/main/docs/MCP.md).
114
+
115
+ ## How it works
116
+
117
+ Every call runs five steps, in this order:
118
+
119
+ | Step | What it does |
120
+ |---|---|
121
+ | CAP | Shortens very long messages (keeps the start and end). |
122
+ | PIN | Always includes facts you pinned with `KeyedPins`. |
123
+ | RETRIEVE | Finds older text that matches the question (BM25), including text that CAP cut from recent turns. |
124
+ | WINDOW | Keeps as many recent turns as fit. |
125
+ | SUMMARIZE | Adds a summary of older turns, if you supply one. |
126
+
127
+ You can turn each step on or off with `AssemblyOptions`. For a chat that keeps
128
+ growing, `MemoryStore` saves turns, pins and summaries in SQLite. The
129
+ [rolling summary example](https://github.com/Nandu1729/Context-Engine/blob/main/examples/rolling_summary.py) shows a full loop.
130
+
131
+ ## What the engine guarantees
132
+
133
+ These are enforced in code and covered by about 1,400 tests:
134
+
135
+ - **Within budget, or a clear error.** The result never exceeds your input
136
+ budget as counted by the local tokenizer. If the required parts (system
137
+ prompt, question, pins) cannot fit, you get a typed error, not a silently
138
+ cut prompt.
139
+ - **Provenance.** Every recent-turn and retrieved block carries source
140
+ references: turn, message, revision, character range and a content hash.
141
+ A summary records which turns it covers and their hashes, and is rejected if
142
+ any of them changed. `result.diagnostics` lists which turns were kept,
143
+ retrieved or dropped, and why.
144
+ - **No hidden model calls.** Assembly is local and deterministic. Summaries are
145
+ made only when you call `update_summary()`, with a summarizer you choose.
146
+ - **History is data, not instructions.** Older turns, retrieved text and
147
+ summaries are labelled as data blocks inside user content, never as system
148
+ instructions.
149
+ - **Your originals are never changed.** CAP and retrieval work on copies.
150
+ Retrieval can bring back the original text of a capped message.
151
+
152
+ ## What is not proven yet
153
+
154
+ - **Answer quality.** Tests measure whether the needed facts *reach* the model's
155
+ context, not whether the model then answers correctly. On the built-in
156
+ benchmark, the default settings keep 31 of 31 planted facts at 900 and 3000
157
+ tokens. But that benchmark is one synthetic conversation written alongside
158
+ the engine.
159
+ - **Real-world data.** There are no results yet on real user conversations or
160
+ on questions phrased differently from the source text. Retrieval is
161
+ keyword-based (BM25), so heavily reworded questions may miss evidence.
162
+ - **Exact provider token counts.** Budgets use a local tokenizer estimate.
163
+ Providers may count differently. On small requests, provider counts were up
164
+ to 1.78× the local estimate in past tests, so leave headroom or calibrate.
165
+ - **Speed.** With all five steps on, assembly takes about 250–350 ms on the
166
+ benchmark (about 17–35 ms without CAP). Faster CAP is on the roadmap.
167
+ - **Production use.** The local HTTP service is for development. It has not
168
+ been load-tested or security-qualified for production.
169
+
170
+ ## Try it
171
+
172
+ ```sh
173
+ uv run context-engine demo-layers # the five steps on sample data
174
+ uv run --offline context-engine strategy-eval --budget 900 # compare strategies, no model calls
175
+ uv run --offline python examples/rolling_summary.py # memory + rolling summaries
176
+ ```
177
+
178
+ ## Documentation
179
+
180
+ - [SDK and CLI guide](https://github.com/Nandu1729/Context-Engine/blob/main/docs/PIPELINE.md): inputs, options, diagnostics, summaries.
181
+ - [Benchmark report](https://github.com/Nandu1729/Context-Engine/blob/main/docs/BENCHMARK_REPORT.md): strategy comparison and its limits.
182
+ - [Security audit](https://github.com/Nandu1729/Context-Engine/blob/main/docs/SECURITY_AUDIT.md): trust boundaries and known gaps.
183
+ - [Architecture](https://github.com/Nandu1729/Context-Engine/blob/main/docs/ARCHITECTURE.md) and [current status](https://github.com/Nandu1729/Context-Engine/blob/main/docs/CURRENT_STATUS.md).
184
+ - [Changelog](https://github.com/Nandu1729/Context-Engine/blob/main/CHANGELOG.md).
185
+
186
+ ## For contributors
187
+
188
+ ```sh
189
+ uv sync --locked
190
+ uv run --locked --offline pytest -q
191
+ uv run ruff check src
192
+ ```
193
+
194
+ The repository also holds frozen historical benchmarks under `archives/` and
195
+ `output/`, and a project log under `brain/` (start at `brain/INDEX.md`; check it
196
+ with `python3 scripts/brain.py check`). Historical experiments only run with their
197
+ archived wheels, on purpose. The [local quickstart](https://github.com/Nandu1729/Context-Engine/blob/main/docs/LOCAL_QUICKSTART.md)
198
+ and [checkpoints](https://github.com/Nandu1729/Context-Engine/blob/main/docs/CHECKPOINTS.md)
199
+ cover the full project history.
200
+
201
+ ## License
202
+
203
+ [Apache-2.0](https://github.com/Nandu1729/Context-Engine/blob/main/LICENSE).
@@ -0,0 +1,167 @@
1
+ # Context Engine
2
+
3
+ Fit long conversations into a fixed token budget for any LLM, and know exactly
4
+ where every piece of context came from.
5
+
6
+ `context-engineering-core` is a small Python library. You give it the full chat
7
+ history, the new question and a token budget. It returns a list of chat messages
8
+ that fits the budget. Those messages keep the recent turns, the facts you pinned,
9
+ and the older evidence that matches the question. It never calls a model by
10
+ itself, so you send the messages to any provider you like.
11
+
12
+ > **Status: alpha (0.12.0).** Good for developers to try locally. It is not yet
13
+ > proven on real-world data, and there is no hosted service. See
14
+ > [What is not proven yet](#what-is-not-proven-yet).
15
+
16
+ ## Install
17
+
18
+ Requires Python 3.12+.
19
+
20
+ ```sh
21
+ pip install context-engineering-core # after the first PyPI release
22
+ ```
23
+
24
+ From source:
25
+
26
+ ```sh
27
+ git clone https://github.com/Nandu1729/Context-Engine.git
28
+ cd Context-Engine
29
+ uv sync --locked
30
+ uv run context-engine check # prints "deps ok"
31
+ ```
32
+
33
+ The first run downloads the tokenizer vocabulary (tiktoken `o200k_harmony`) once.
34
+ After that, everything works offline.
35
+
36
+ ## Quick example
37
+
38
+ ```python
39
+ from context_engine import assemble_context
40
+ from context_engine.config import BudgetConfig
41
+ from context_engine.models import KeyedPins, Message, Role, Scope, Turn
42
+
43
+ scope = Scope("my-app", "chat-1")
44
+ notes = [f"Note {i}: shipment {i} left from dock D-{i}." for i in range(300)]
45
+ history = [Turn(f"t{i}", scope, (Message(f"m{i}", Role.USER, text),)) for i, text in enumerate(notes)]
46
+
47
+ result = assemble_context(history, "Which dock did shipment 42 leave from?", KeyedPins(scope),
48
+ BudgetConfig(input_cap=900), system="Answer only from the context.")
49
+ print(result.diagnostics.estimate.estimated_tokens, "of", result.diagnostics.input_allowance)
50
+ print("evidence from turns:", result.diagnostics.retrieved_turn_ids)
51
+ messages = result.messages # ready for any chat-completions API
52
+ ```
53
+
54
+ The 300 notes are far over the 900-token budget. The result fits (about 716 of
55
+ 900 tokens), and turn `t42`, the one with the answer, is among the retrieved
56
+ evidence. Then send `messages` to OpenAI, Anthropic, Groq or a local model.
57
+
58
+ ## Use it with your provider
59
+
60
+ `context_engine.adapters` reshapes the result for common clients. None of them is
61
+ a dependency of the engine:
62
+
63
+ ```python
64
+ from context_engine.adapters import anthropic_messages, langchain_messages, openai_chat, turns_from_openai
65
+
66
+ openai_client.chat.completions.create(model="...", **openai_chat(result)) # OpenAI, Groq, local servers
67
+ anthropic_client.messages.create(model="...", max_tokens=512, **anthropic_messages(result))
68
+ chat_model.invoke(langchain_messages(result)) # pip install 'context-engineering-core[langchain]'
69
+
70
+ history = turns_from_openai(existing_openai_messages, scope) # reuse a chat history you already have
71
+ ```
72
+
73
+ ### Use it from an MCP client
74
+
75
+ `context-engine-mcp --data-dir ./context-memory` runs a local MCP server (stdio) with
76
+ `add_message`, `pin_fact`, `get_context` and `forget` tools. Prototype; see the
77
+ [MCP guide](https://github.com/Nandu1729/Context-Engine/blob/main/docs/MCP.md).
78
+
79
+ ## How it works
80
+
81
+ Every call runs five steps, in this order:
82
+
83
+ | Step | What it does |
84
+ |---|---|
85
+ | CAP | Shortens very long messages (keeps the start and end). |
86
+ | PIN | Always includes facts you pinned with `KeyedPins`. |
87
+ | RETRIEVE | Finds older text that matches the question (BM25), including text that CAP cut from recent turns. |
88
+ | WINDOW | Keeps as many recent turns as fit. |
89
+ | SUMMARIZE | Adds a summary of older turns, if you supply one. |
90
+
91
+ You can turn each step on or off with `AssemblyOptions`. For a chat that keeps
92
+ growing, `MemoryStore` saves turns, pins and summaries in SQLite. The
93
+ [rolling summary example](https://github.com/Nandu1729/Context-Engine/blob/main/examples/rolling_summary.py) shows a full loop.
94
+
95
+ ## What the engine guarantees
96
+
97
+ These are enforced in code and covered by about 1,400 tests:
98
+
99
+ - **Within budget, or a clear error.** The result never exceeds your input
100
+ budget as counted by the local tokenizer. If the required parts (system
101
+ prompt, question, pins) cannot fit, you get a typed error, not a silently
102
+ cut prompt.
103
+ - **Provenance.** Every recent-turn and retrieved block carries source
104
+ references: turn, message, revision, character range and a content hash.
105
+ A summary records which turns it covers and their hashes, and is rejected if
106
+ any of them changed. `result.diagnostics` lists which turns were kept,
107
+ retrieved or dropped, and why.
108
+ - **No hidden model calls.** Assembly is local and deterministic. Summaries are
109
+ made only when you call `update_summary()`, with a summarizer you choose.
110
+ - **History is data, not instructions.** Older turns, retrieved text and
111
+ summaries are labelled as data blocks inside user content, never as system
112
+ instructions.
113
+ - **Your originals are never changed.** CAP and retrieval work on copies.
114
+ Retrieval can bring back the original text of a capped message.
115
+
116
+ ## What is not proven yet
117
+
118
+ - **Answer quality.** Tests measure whether the needed facts *reach* the model's
119
+ context, not whether the model then answers correctly. On the built-in
120
+ benchmark, the default settings keep 31 of 31 planted facts at 900 and 3000
121
+ tokens. But that benchmark is one synthetic conversation written alongside
122
+ the engine.
123
+ - **Real-world data.** There are no results yet on real user conversations or
124
+ on questions phrased differently from the source text. Retrieval is
125
+ keyword-based (BM25), so heavily reworded questions may miss evidence.
126
+ - **Exact provider token counts.** Budgets use a local tokenizer estimate.
127
+ Providers may count differently. On small requests, provider counts were up
128
+ to 1.78× the local estimate in past tests, so leave headroom or calibrate.
129
+ - **Speed.** With all five steps on, assembly takes about 250–350 ms on the
130
+ benchmark (about 17–35 ms without CAP). Faster CAP is on the roadmap.
131
+ - **Production use.** The local HTTP service is for development. It has not
132
+ been load-tested or security-qualified for production.
133
+
134
+ ## Try it
135
+
136
+ ```sh
137
+ uv run context-engine demo-layers # the five steps on sample data
138
+ uv run --offline context-engine strategy-eval --budget 900 # compare strategies, no model calls
139
+ uv run --offline python examples/rolling_summary.py # memory + rolling summaries
140
+ ```
141
+
142
+ ## Documentation
143
+
144
+ - [SDK and CLI guide](https://github.com/Nandu1729/Context-Engine/blob/main/docs/PIPELINE.md): inputs, options, diagnostics, summaries.
145
+ - [Benchmark report](https://github.com/Nandu1729/Context-Engine/blob/main/docs/BENCHMARK_REPORT.md): strategy comparison and its limits.
146
+ - [Security audit](https://github.com/Nandu1729/Context-Engine/blob/main/docs/SECURITY_AUDIT.md): trust boundaries and known gaps.
147
+ - [Architecture](https://github.com/Nandu1729/Context-Engine/blob/main/docs/ARCHITECTURE.md) and [current status](https://github.com/Nandu1729/Context-Engine/blob/main/docs/CURRENT_STATUS.md).
148
+ - [Changelog](https://github.com/Nandu1729/Context-Engine/blob/main/CHANGELOG.md).
149
+
150
+ ## For contributors
151
+
152
+ ```sh
153
+ uv sync --locked
154
+ uv run --locked --offline pytest -q
155
+ uv run ruff check src
156
+ ```
157
+
158
+ The repository also holds frozen historical benchmarks under `archives/` and
159
+ `output/`, and a project log under `brain/` (start at `brain/INDEX.md`; check it
160
+ with `python3 scripts/brain.py check`). Historical experiments only run with their
161
+ archived wheels, on purpose. The [local quickstart](https://github.com/Nandu1729/Context-Engine/blob/main/docs/LOCAL_QUICKSTART.md)
162
+ and [checkpoints](https://github.com/Nandu1729/Context-Engine/blob/main/docs/CHECKPOINTS.md)
163
+ cover the full project history.
164
+
165
+ ## License
166
+
167
+ [Apache-2.0](https://github.com/Nandu1729/Context-Engine/blob/main/LICENSE).