contextops 0.1.0__tar.gz → 0.2.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 (39) hide show
  1. {contextops-0.1.0 → contextops-0.2.1}/PKG-INFO +2 -2
  2. {contextops-0.1.0 → contextops-0.2.1}/README.md +238 -238
  3. contextops-0.2.1/contextops/analyzers/redundancy.py +371 -0
  4. {contextops-0.1.0 → contextops-0.2.1}/contextops/core/engine.py +358 -355
  5. {contextops-0.1.0 → contextops-0.2.1}/contextops.egg-info/PKG-INFO +2 -2
  6. {contextops-0.1.0 → contextops-0.2.1}/pyproject.toml +1 -1
  7. {contextops-0.1.0 → contextops-0.2.1}/tests/test_benchmarks.py +87 -86
  8. contextops-0.2.1/tests/test_redundancy.py +163 -0
  9. {contextops-0.1.0 → contextops-0.2.1}/tests/test_signal_contract.py +304 -294
  10. contextops-0.1.0/contextops/analyzers/redundancy.py +0 -362
  11. contextops-0.1.0/tests/test_redundancy.py +0 -83
  12. {contextops-0.1.0 → contextops-0.2.1}/LICENSE +0 -0
  13. {contextops-0.1.0 → contextops-0.2.1}/contextops/__init__.py +0 -0
  14. {contextops-0.1.0 → contextops-0.2.1}/contextops/analyzers/__init__.py +0 -0
  15. {contextops-0.1.0 → contextops-0.2.1}/contextops/analyzers/density.py +0 -0
  16. {contextops-0.1.0 → contextops-0.2.1}/contextops/analyzers/structure.py +0 -0
  17. {contextops-0.1.0 → contextops-0.2.1}/contextops/analyzers/tokens.py +0 -0
  18. {contextops-0.1.0 → contextops-0.2.1}/contextops/api/__init__.py +0 -0
  19. {contextops-0.1.0 → contextops-0.2.1}/contextops/api/diff.py +0 -0
  20. {contextops-0.1.0 → contextops-0.2.1}/contextops/api/inspect.py +0 -0
  21. {contextops-0.1.0 → contextops-0.2.1}/contextops/api/stability.py +0 -0
  22. {contextops-0.1.0 → contextops-0.2.1}/contextops/cli/__init__.py +0 -0
  23. {contextops-0.1.0 → contextops-0.2.1}/contextops/cli/main.py +0 -0
  24. {contextops-0.1.0 → contextops-0.2.1}/contextops/cli/renderer.py +0 -0
  25. {contextops-0.1.0 → contextops-0.2.1}/contextops/core/__init__.py +0 -0
  26. {contextops-0.1.0 → contextops-0.2.1}/contextops/core/config.py +0 -0
  27. {contextops-0.1.0 → contextops-0.2.1}/contextops/core/models.py +0 -0
  28. {contextops-0.1.0 → contextops-0.2.1}/contextops/core/normalizer.py +0 -0
  29. {contextops-0.1.0 → contextops-0.2.1}/contextops.egg-info/SOURCES.txt +0 -0
  30. {contextops-0.1.0 → contextops-0.2.1}/contextops.egg-info/dependency_links.txt +0 -0
  31. {contextops-0.1.0 → contextops-0.2.1}/contextops.egg-info/entry_points.txt +0 -0
  32. {contextops-0.1.0 → contextops-0.2.1}/contextops.egg-info/requires.txt +0 -0
  33. {contextops-0.1.0 → contextops-0.2.1}/contextops.egg-info/top_level.txt +0 -0
  34. {contextops-0.1.0 → contextops-0.2.1}/setup.cfg +0 -0
  35. {contextops-0.1.0 → contextops-0.2.1}/tests/test_chaos.py +0 -0
  36. {contextops-0.1.0 → contextops-0.2.1}/tests/test_density.py +0 -0
  37. {contextops-0.1.0 → contextops-0.2.1}/tests/test_diff.py +0 -0
  38. {contextops-0.1.0 → contextops-0.2.1}/tests/test_schema.py +0 -0
  39. {contextops-0.1.0 → contextops-0.2.1}/tests/test_structure.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: contextops
3
- Version: 0.1.0
3
+ Version: 0.2.1
4
4
  Summary: Deterministic context linter for LLM applications — analyze, score, and optimize your LLM context payloads.
5
5
  Author: Abhijeet Baug
6
6
  License-Expression: MIT
@@ -36,7 +36,7 @@ Dynamic: license-file
36
36
 
37
37
  **The deterministic context linter for LLM applications.**
38
38
 
39
- [![PyPI version](https://img.shields.io/pypi/v/contextops.svg)](https://pypi.org/project/contextops/)
39
+ [![PyPI version](https://img.shields.io/pypi/v/contextops.svg)](https://pypi.org/project/contextops/)
40
40
  [![Python](https://img.shields.io/pypi/pyversions/contextops.svg)](https://pypi.org/project/contextops/)
41
41
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
42
42
  [![CI](https://img.shields.io/badge/CI-stable-brightgreen.svg)](STABILITY.md)
@@ -1,238 +1,238 @@
1
- # ContextOps
2
-
3
- **The deterministic context linter for LLM applications.**
4
-
5
- [![PyPI version](https://img.shields.io/pypi/v/contextops.svg)](https://pypi.org/project/contextops/)
6
- [![Python](https://img.shields.io/pypi/pyversions/contextops.svg)](https://pypi.org/project/contextops/)
7
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
- [![CI](https://img.shields.io/badge/CI-stable-brightgreen.svg)](STABILITY.md)
9
-
10
- ContextOps analyzes the context fed into your LLM and tells you what's broken — redundant chunks, wasted tokens, structural imbalance — with a **deterministic 0–100 score** and actionable fixes.
11
-
12
- Think of it as **ESLint for your LLM prompts**.
13
-
14
- ---
15
-
16
- ## Why ContextOps?
17
-
18
- Most LLM applications blindly stuff context into the prompt window. This leads to:
19
-
20
- - 💸 **Wasted spend** — paying for redundant tokens that don't improve output
21
- - 🔁 **Silent regressions** — a "small RAG change" floods the context with duplicates
22
- - 🏗️ **Structural drift** — retrieval chunks slowly dominate the entire prompt
23
- - 🎯 **No visibility** — teams have no way to measure context quality in CI
24
-
25
- ContextOps gives you that visibility. It runs in your CI pipeline, scores every context payload, and fails the build if quality degrades.
26
-
27
- ---
28
-
29
- ## Quick Start
30
-
31
- ```bash
32
- pip install contextops
33
- ```
34
-
35
- ### See it in action
36
-
37
- ```bash
38
- # Run the built-in demo — instant "wow moment"
39
- contextops demo
40
- ```
41
-
42
- ### Analyze your own context
43
-
44
- ```bash
45
- # Full analysis with rich terminal output
46
- contextops inspect context.json
47
-
48
- # CI mode: fail if score drops below threshold
49
- contextops check context.json --min-score 70
50
-
51
- # Compare two snapshots for regressions
52
- contextops diff before.json after.json
53
-
54
- # JSON output for dashboards and automation
55
- contextops inspect context.json --json-output
56
- ```
57
-
58
- ### Python API
59
-
60
- ```python
61
- from contextops.api.inspect import inspect_context
62
-
63
- result = inspect_context({
64
- "system": "You are a helpful assistant.",
65
- "chunks": [
66
- {"content": "Refund policy: 30 days...", "source": "docs/refund.md"},
67
- {"content": "Refund policy: within 30 days...", "source": "docs/refund.md"},
68
- ],
69
- "memory": ["User asked about refunds before."],
70
- })
71
-
72
- print(f"Score: {result.score}/100")
73
- print(f"Wasted tokens: {result.token_breakdown.wasted_tokens}")
74
- for rec in result.recommendations:
75
- print(f" → {rec.fix}")
76
- ```
77
-
78
- ---
79
-
80
- ## What It Measures
81
-
82
- ContextOps computes a **0–100 Context Score** from four independent penalty dimensions:
83
-
84
- | Dimension | What It Detects | Max Penalty |
85
- |---|---|---|
86
- | **Redundancy** | Duplicate / near-duplicate chunks (N-gram + Jaccard) | 30 pts |
87
- | **Density** | Wasted tokens from structural bloat | 30 pts |
88
- | **Structure** | Imbalanced type distribution (e.g., retrieval > 70%) | 20 pts |
89
- | **Concentration** | Source dominance or highly imbalanced chunk distribution | 20 pts |
90
-
91
- ```
92
- Context Score = 100 - (Redundancy + Density + Structure + Concentration)
93
- ```
94
-
95
- Every penalty maps to a **specific finding** with **token savings** and an **actionable fix**.
96
-
97
- ---
98
-
99
- ## CI / CD Integration
100
-
101
- ### GitHub Actions
102
-
103
- ```yaml
104
- name: Context Quality Gate
105
-
106
- on: [pull_request]
107
-
108
- jobs:
109
- context-check:
110
- runs-on: ubuntu-latest
111
- steps:
112
- - uses: actions/checkout@v4
113
- - uses: actions/setup-python@v5
114
- with:
115
- python-version: "3.12"
116
-
117
- - run: pip install contextops
118
-
119
- - name: Check context quality
120
- run: contextops check prompts/context.json --min-score 75
121
- ```
122
-
123
- ### Exit Codes
124
-
125
- | Code | Meaning |
126
- |---|---|
127
- | `0` | Score meets threshold — build passes |
128
- | `1` | Score below threshold — build fails |
129
-
130
- ### Regression Detection
131
-
132
- ```bash
133
- # Save a baseline
134
- contextops inspect prompts/v1.json --json-output > baseline.json
135
-
136
- # After changes, compare
137
- contextops diff baseline.json prompts/v2.json
138
- ```
139
-
140
- ---
141
-
142
- ## Context File Format
143
-
144
- ContextOps accepts a JSON file with any combination of these keys:
145
-
146
- ```json
147
- {
148
- "system": "Your system prompt here",
149
- "messages": [
150
- {"role": "user", "content": "User question"}
151
- ],
152
- "chunks": [
153
- {"content": "Retrieved chunk text", "source": "docs/page.md"}
154
- ],
155
- "memory": [
156
- "Previous conversation context"
157
- ],
158
- "tools": [
159
- {"name": "search_api", "output": "Tool response text"}
160
- ]
161
- }
162
- ```
163
-
164
- It also accepts raw OpenAI message lists:
165
-
166
- ```json
167
- [
168
- {"role": "system", "content": "You are helpful."},
169
- {"role": "user", "content": "What is the refund policy?"}
170
- ]
171
- ```
172
-
173
- ---
174
-
175
- ## CLI Reference
176
-
177
- | Command | Purpose |
178
- |---|---|
179
- | `contextops inspect <file>` | Analyze and display results |
180
- | `contextops check <file> --min-score N` | CI gate with exit codes |
181
- | `contextops demo` | Built-in demo context |
182
- | `contextops stability <file>` | Deterministic stability report |
183
- | `contextops diff <file_a> <file_b>` | Compare two snapshots |
184
-
185
- ### Flags
186
-
187
- | Flag | Commands | Purpose |
188
- |---|---|---|
189
- | `--json-output` | inspect, check | Machine-readable JSON output |
190
- | `--min-score N` | check | Minimum passing score (0–100) |
191
- | `--model <name>` | inspect, check | Target model for cost estimation |
192
- | `--explain` | inspect, check | Show detailed penalty reasoning |
193
- | `--config <file>` | inspect, check | Custom threshold config file |
194
-
195
- ---
196
-
197
- ## Design Principles
198
-
199
- 1. **Deterministic** — Same input → same output. Always. No randomness, no embeddings, no LLM calls.
200
- 2. **Explainable** — Every penalty maps to a real issue with a token count and a fix.
201
- 3. **CI-native** — Designed for pipelines first. Exit codes, JSON output, threshold gating.
202
- 4. **Zero network** — Runs entirely offline. No API keys, no external services.
203
-
204
- ---
205
-
206
- ## Stability Contract
207
-
208
- ContextOps ships with a formal [Stability Contract](STABILITY.md) that guarantees:
209
-
210
- - **Scoring determinism** — same input always produces the same score
211
- - **Schema stability** — JSON output fields never change within a major version
212
- - **Performance bounds** — sub-second for payloads up to 50,000 tokens
213
- - **Semantic versioning** — scoring formula changes require a major version bump
214
-
215
- This contract exists so teams can trust ContextOps in production CI pipelines.
216
-
217
- ---
218
-
219
- ## Development
220
-
221
- ```bash
222
- # Clone and install in dev mode
223
- git clone https://github.com/Abhijeet777/contextops.git
224
- cd contextops
225
- pip install -e ".[dev]"
226
-
227
- # Run tests
228
- pytest
229
-
230
- # Run chaos stress tests
231
- pytest tests/test_chaos.py -v
232
- ```
233
-
234
- ---
235
-
236
- ## License
237
-
238
- [MIT](LICENSE)
1
+ # ContextOps
2
+
3
+ **The deterministic context linter for LLM applications.**
4
+
5
+ [![PyPI version](https://img.shields.io/pypi/v/contextops.svg)](https://pypi.org/project/contextops/)
6
+ [![Python](https://img.shields.io/pypi/pyversions/contextops.svg)](https://pypi.org/project/contextops/)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8
+ [![CI](https://img.shields.io/badge/CI-stable-brightgreen.svg)](STABILITY.md)
9
+
10
+ ContextOps analyzes the context fed into your LLM and tells you what's broken — redundant chunks, wasted tokens, structural imbalance — with a **deterministic 0–100 score** and actionable fixes.
11
+
12
+ Think of it as **ESLint for your LLM prompts**.
13
+
14
+ ---
15
+
16
+ ## Why ContextOps?
17
+
18
+ Most LLM applications blindly stuff context into the prompt window. This leads to:
19
+
20
+ - 💸 **Wasted spend** — paying for redundant tokens that don't improve output
21
+ - 🔁 **Silent regressions** — a "small RAG change" floods the context with duplicates
22
+ - 🏗️ **Structural drift** — retrieval chunks slowly dominate the entire prompt
23
+ - 🎯 **No visibility** — teams have no way to measure context quality in CI
24
+
25
+ ContextOps gives you that visibility. It runs in your CI pipeline, scores every context payload, and fails the build if quality degrades.
26
+
27
+ ---
28
+
29
+ ## Quick Start
30
+
31
+ ```bash
32
+ pip install contextops
33
+ ```
34
+
35
+ ### See it in action
36
+
37
+ ```bash
38
+ # Run the built-in demo — instant "wow moment"
39
+ contextops demo
40
+ ```
41
+
42
+ ### Analyze your own context
43
+
44
+ ```bash
45
+ # Full analysis with rich terminal output
46
+ contextops inspect context.json
47
+
48
+ # CI mode: fail if score drops below threshold
49
+ contextops check context.json --min-score 70
50
+
51
+ # Compare two snapshots for regressions
52
+ contextops diff before.json after.json
53
+
54
+ # JSON output for dashboards and automation
55
+ contextops inspect context.json --json-output
56
+ ```
57
+
58
+ ### Python API
59
+
60
+ ```python
61
+ from contextops.api.inspect import inspect_context
62
+
63
+ result = inspect_context({
64
+ "system": "You are a helpful assistant.",
65
+ "chunks": [
66
+ {"content": "Refund policy: 30 days...", "source": "docs/refund.md"},
67
+ {"content": "Refund policy: within 30 days...", "source": "docs/refund.md"},
68
+ ],
69
+ "memory": ["User asked about refunds before."],
70
+ })
71
+
72
+ print(f"Score: {result.score}/100")
73
+ print(f"Wasted tokens: {result.token_breakdown.wasted_tokens}")
74
+ for rec in result.recommendations:
75
+ print(f" → {rec.fix}")
76
+ ```
77
+
78
+ ---
79
+
80
+ ## What It Measures
81
+
82
+ ContextOps computes a **0–100 Context Score** from four independent penalty dimensions:
83
+
84
+ | Dimension | What It Detects | Max Penalty |
85
+ |---|---|---|
86
+ | **Redundancy** | Duplicate / near-duplicate chunks (N-gram + Jaccard) | 30 pts |
87
+ | **Density** | Wasted tokens from structural bloat | 30 pts |
88
+ | **Structure** | Imbalanced type distribution (e.g., retrieval > 70%) | 20 pts |
89
+ | **Concentration** | Source dominance or highly imbalanced chunk distribution | 20 pts |
90
+
91
+ ```
92
+ Context Score = 100 - (Redundancy + Density + Structure + Concentration)
93
+ ```
94
+
95
+ Every penalty maps to a **specific finding** with **token savings** and an **actionable fix**.
96
+
97
+ ---
98
+
99
+ ## CI / CD Integration
100
+
101
+ ### GitHub Actions
102
+
103
+ ```yaml
104
+ name: Context Quality Gate
105
+
106
+ on: [pull_request]
107
+
108
+ jobs:
109
+ context-check:
110
+ runs-on: ubuntu-latest
111
+ steps:
112
+ - uses: actions/checkout@v4
113
+ - uses: actions/setup-python@v5
114
+ with:
115
+ python-version: "3.12"
116
+
117
+ - run: pip install contextops
118
+
119
+ - name: Check context quality
120
+ run: contextops check prompts/context.json --min-score 75
121
+ ```
122
+
123
+ ### Exit Codes
124
+
125
+ | Code | Meaning |
126
+ |---|---|
127
+ | `0` | Score meets threshold — build passes |
128
+ | `1` | Score below threshold — build fails |
129
+
130
+ ### Regression Detection
131
+
132
+ ```bash
133
+ # Save a baseline
134
+ contextops inspect prompts/v1.json --json-output > baseline.json
135
+
136
+ # After changes, compare
137
+ contextops diff baseline.json prompts/v2.json
138
+ ```
139
+
140
+ ---
141
+
142
+ ## Context File Format
143
+
144
+ ContextOps accepts a JSON file with any combination of these keys:
145
+
146
+ ```json
147
+ {
148
+ "system": "Your system prompt here",
149
+ "messages": [
150
+ {"role": "user", "content": "User question"}
151
+ ],
152
+ "chunks": [
153
+ {"content": "Retrieved chunk text", "source": "docs/page.md"}
154
+ ],
155
+ "memory": [
156
+ "Previous conversation context"
157
+ ],
158
+ "tools": [
159
+ {"name": "search_api", "output": "Tool response text"}
160
+ ]
161
+ }
162
+ ```
163
+
164
+ It also accepts raw OpenAI message lists:
165
+
166
+ ```json
167
+ [
168
+ {"role": "system", "content": "You are helpful."},
169
+ {"role": "user", "content": "What is the refund policy?"}
170
+ ]
171
+ ```
172
+
173
+ ---
174
+
175
+ ## CLI Reference
176
+
177
+ | Command | Purpose |
178
+ |---|---|
179
+ | `contextops inspect <file>` | Analyze and display results |
180
+ | `contextops check <file> --min-score N` | CI gate with exit codes |
181
+ | `contextops demo` | Built-in demo context |
182
+ | `contextops stability <file>` | Deterministic stability report |
183
+ | `contextops diff <file_a> <file_b>` | Compare two snapshots |
184
+
185
+ ### Flags
186
+
187
+ | Flag | Commands | Purpose |
188
+ |---|---|---|
189
+ | `--json-output` | inspect, check | Machine-readable JSON output |
190
+ | `--min-score N` | check | Minimum passing score (0–100) |
191
+ | `--model <name>` | inspect, check | Target model for cost estimation |
192
+ | `--explain` | inspect, check | Show detailed penalty reasoning |
193
+ | `--config <file>` | inspect, check | Custom threshold config file |
194
+
195
+ ---
196
+
197
+ ## Design Principles
198
+
199
+ 1. **Deterministic** — Same input → same output. Always. No randomness, no embeddings, no LLM calls.
200
+ 2. **Explainable** — Every penalty maps to a real issue with a token count and a fix.
201
+ 3. **CI-native** — Designed for pipelines first. Exit codes, JSON output, threshold gating.
202
+ 4. **Zero network** — Runs entirely offline. No API keys, no external services.
203
+
204
+ ---
205
+
206
+ ## Stability Contract
207
+
208
+ ContextOps ships with a formal [Stability Contract](STABILITY.md) that guarantees:
209
+
210
+ - **Scoring determinism** — same input always produces the same score
211
+ - **Schema stability** — JSON output fields never change within a major version
212
+ - **Performance bounds** — sub-second for payloads up to 50,000 tokens
213
+ - **Semantic versioning** — scoring formula changes require a major version bump
214
+
215
+ This contract exists so teams can trust ContextOps in production CI pipelines.
216
+
217
+ ---
218
+
219
+ ## Development
220
+
221
+ ```bash
222
+ # Clone and install in dev mode
223
+ git clone https://github.com/Abhijeet777/contextops.git
224
+ cd contextops
225
+ pip install -e ".[dev]"
226
+
227
+ # Run tests
228
+ pytest
229
+
230
+ # Run chaos stress tests
231
+ pytest tests/test_chaos.py -v
232
+ ```
233
+
234
+ ---
235
+
236
+ ## License
237
+
238
+ [MIT](LICENSE)