contextsage 0.1.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.
- contextsage-0.1.0/.env.example +16 -0
- contextsage-0.1.0/.github/workflows/ci.yml +43 -0
- contextsage-0.1.0/.gitignore +20 -0
- contextsage-0.1.0/.pre-commit-config.yaml +28 -0
- contextsage-0.1.0/ARCHITECTURE_INTERNALS.md +192 -0
- contextsage-0.1.0/CHANGELOG.md +40 -0
- contextsage-0.1.0/CODE_OF_CONDUCT.md +63 -0
- contextsage-0.1.0/CONTRIBUTING.md +68 -0
- contextsage-0.1.0/LICENSE +190 -0
- contextsage-0.1.0/PKG-INFO +204 -0
- contextsage-0.1.0/README.md +147 -0
- contextsage-0.1.0/SECURITY.md +43 -0
- contextsage-0.1.0/docs/api-reference.md +138 -0
- contextsage-0.1.0/docs/architecture.md +109 -0
- contextsage-0.1.0/docs/configuration.md +56 -0
- contextsage-0.1.0/docs/images/observability-grafana-dashboard.png +0 -0
- contextsage-0.1.0/docs/images/observability-tokens-before-after.png +0 -0
- contextsage-0.1.0/docs/index.md +27 -0
- contextsage-0.1.0/docs/migration.md +71 -0
- contextsage-0.1.0/docs/observability.md +121 -0
- contextsage-0.1.0/docs/provenance.md +64 -0
- contextsage-0.1.0/examples/_llm.py +108 -0
- contextsage-0.1.0/examples/agent_create_agent.py +116 -0
- contextsage-0.1.0/examples/all_parameters.py +373 -0
- contextsage-0.1.0/examples/basic.py +51 -0
- contextsage-0.1.0/examples/deep_agent.py +113 -0
- contextsage-0.1.0/examples/large_tool_output.py +71 -0
- contextsage-0.1.0/examples/logs.py +56 -0
- contextsage-0.1.0/examples/mixed_mcp_output.py +64 -0
- contextsage-0.1.0/examples/observability_prometheus.py +146 -0
- contextsage-0.1.0/examples/observability_prometheus.yml +54 -0
- contextsage-0.1.0/examples/policy.py +49 -0
- contextsage-0.1.0/examples/rag.py +53 -0
- contextsage-0.1.0/examples/recovery.py +55 -0
- contextsage-0.1.0/examples/sql_parser.py +244 -0
- contextsage-0.1.0/examples/structured_data.py +49 -0
- contextsage-0.1.0/examples/swarm.py +156 -0
- contextsage-0.1.0/mkdocs.yml +55 -0
- contextsage-0.1.0/pyproject.toml +109 -0
- contextsage-0.1.0/pyrefly.toml +3 -0
- contextsage-0.1.0/src/contextsage/__init__.py +22 -0
- contextsage-0.1.0/src/contextsage/budget/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/budget/analyzer.py +100 -0
- contextsage-0.1.0/src/contextsage/budget/tokens.py +216 -0
- contextsage-0.1.0/src/contextsage/classification/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/classification/signals.py +131 -0
- contextsage-0.1.0/src/contextsage/context/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/context/decomposition.py +208 -0
- contextsage-0.1.0/src/contextsage/context/observer.py +90 -0
- contextsage-0.1.0/src/contextsage/core/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/core/models.py +254 -0
- contextsage-0.1.0/src/contextsage/core/reconstruction.py +61 -0
- contextsage-0.1.0/src/contextsage/exceptions.py +67 -0
- contextsage-0.1.0/src/contextsage/importance/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/importance/engine.py +127 -0
- contextsage-0.1.0/src/contextsage/integrations/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/integrations/langgraph/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/integrations/langgraph/adapter.py +92 -0
- contextsage-0.1.0/src/contextsage/integrations/langgraph_xai/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/integrations/langgraph_xai/provenance.py +148 -0
- contextsage-0.1.0/src/contextsage/lineage/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/lineage/manager.py +83 -0
- contextsage-0.1.0/src/contextsage/middleware/__init__.py +9 -0
- contextsage-0.1.0/src/contextsage/middleware/summarization.py +467 -0
- contextsage-0.1.0/src/contextsage/observability/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/observability/events.py +59 -0
- contextsage-0.1.0/src/contextsage/parsers/__init__.py +49 -0
- contextsage-0.1.0/src/contextsage/parsers/base.py +97 -0
- contextsage-0.1.0/src/contextsage/parsers/code_parser.py +157 -0
- contextsage-0.1.0/src/contextsage/parsers/error_parser.py +74 -0
- contextsage-0.1.0/src/contextsage/parsers/grok_patterns.py +90 -0
- contextsage-0.1.0/src/contextsage/parsers/json_parser.py +56 -0
- contextsage-0.1.0/src/contextsage/parsers/log_parser.py +78 -0
- contextsage-0.1.0/src/contextsage/parsers/table_parser.py +37 -0
- contextsage-0.1.0/src/contextsage/parsers/tree_sitter_support.py +134 -0
- contextsage-0.1.0/src/contextsage/planning/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/planning/planner.py +119 -0
- contextsage-0.1.0/src/contextsage/planning/trigger.py +78 -0
- contextsage-0.1.0/src/contextsage/preservation/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/preservation/engine.py +130 -0
- contextsage-0.1.0/src/contextsage/py.typed +0 -0
- contextsage-0.1.0/src/contextsage/recovery/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/recovery/manager.py +98 -0
- contextsage-0.1.0/src/contextsage/relationships/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/relationships/engine.py +123 -0
- contextsage-0.1.0/src/contextsage/transformation/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/transformation/engine.py +106 -0
- contextsage-0.1.0/src/contextsage/transformation/logs.py +71 -0
- contextsage-0.1.0/src/contextsage/transformation/structured.py +81 -0
- contextsage-0.1.0/src/contextsage/validation/__init__.py +3 -0
- contextsage-0.1.0/src/contextsage/validation/validator.py +116 -0
- contextsage-0.1.0/tests/conftest.py +68 -0
- contextsage-0.1.0/tests/failure/test_summarization_failure.py +58 -0
- contextsage-0.1.0/tests/integration/test_middleware_pipeline.py +209 -0
- contextsage-0.1.0/tests/unit/test_budget_analyzer.py +78 -0
- contextsage-0.1.0/tests/unit/test_decomposition.py +40 -0
- contextsage-0.1.0/tests/unit/test_importance.py +32 -0
- contextsage-0.1.0/tests/unit/test_lineage.py +76 -0
- contextsage-0.1.0/tests/unit/test_observability.py +101 -0
- contextsage-0.1.0/tests/unit/test_planner.py +60 -0
- contextsage-0.1.0/tests/unit/test_preservation.py +98 -0
- contextsage-0.1.0/tests/unit/test_reconstruction.py +36 -0
- contextsage-0.1.0/tests/unit/test_recovery.py +59 -0
- contextsage-0.1.0/tests/unit/test_relationships.py +53 -0
- contextsage-0.1.0/tests/unit/test_signals.py +40 -0
- contextsage-0.1.0/tests/unit/test_tokens.py +54 -0
- contextsage-0.1.0/tests/unit/test_transformation_engine.py +84 -0
- contextsage-0.1.0/tests/unit/test_trigger.py +65 -0
- contextsage-0.1.0/tests/unit/test_validator.py +69 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Copy to .env and fill in the API key(s) below. `.env` is gitignored so
|
|
2
|
+
# your key is never committed. The bundled examples use the
|
|
3
|
+
# experientiallabs.ai OpenAI-compatible endpoint by default (see
|
|
4
|
+
# examples/_llm.py) and only require EXPLABS_API_KEY.
|
|
5
|
+
|
|
6
|
+
# Required for running examples/*.py
|
|
7
|
+
EXPLABS_API_KEY=
|
|
8
|
+
# Optional overrides (defaults shown)
|
|
9
|
+
# EXPLABS_BASE_URL=https://api.experientiallabs.ai/v1
|
|
10
|
+
# EXPLABS_MODEL=gpt-5.6-luna
|
|
11
|
+
|
|
12
|
+
# Example: OpenAI (only needed if you swap a real OpenAI model in yourself)
|
|
13
|
+
OPENAI_API_KEY=
|
|
14
|
+
|
|
15
|
+
# Example: Anthropic
|
|
16
|
+
ANTHROPIC_API_KEY=
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.12", "3.13"]
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
|
|
18
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
19
|
+
uses: actions/setup-python@v5
|
|
20
|
+
with:
|
|
21
|
+
python-version: ${{ matrix.python-version }}
|
|
22
|
+
cache: pip
|
|
23
|
+
|
|
24
|
+
- name: Install package with dev extras
|
|
25
|
+
run: pip install -e ".[dev]"
|
|
26
|
+
|
|
27
|
+
- name: Lint (ruff check)
|
|
28
|
+
run: ruff check src tests examples
|
|
29
|
+
|
|
30
|
+
- name: Format check (ruff format)
|
|
31
|
+
run: ruff format --check src tests examples
|
|
32
|
+
|
|
33
|
+
- name: Type check (pyrefly)
|
|
34
|
+
run: pyrefly check
|
|
35
|
+
|
|
36
|
+
- name: Test (pytest)
|
|
37
|
+
run: pytest tests -q
|
|
38
|
+
|
|
39
|
+
- name: Build docs (mkdocs --strict)
|
|
40
|
+
run: mkdocs build --strict
|
|
41
|
+
|
|
42
|
+
- name: Build package
|
|
43
|
+
run: pip install build && python -m build
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
.venv/
|
|
2
|
+
venv/
|
|
3
|
+
__pycache__/
|
|
4
|
+
*.pyc
|
|
5
|
+
*.egg-info/
|
|
6
|
+
dist/
|
|
7
|
+
build/
|
|
8
|
+
.pytest_cache/
|
|
9
|
+
.ruff_cache/
|
|
10
|
+
.mypy_cache/
|
|
11
|
+
.pyrefly_cache/
|
|
12
|
+
site/
|
|
13
|
+
node_modules/
|
|
14
|
+
package.json
|
|
15
|
+
package-lock.json
|
|
16
|
+
.env
|
|
17
|
+
uv.lock.bak
|
|
18
|
+
*.log
|
|
19
|
+
.coverage
|
|
20
|
+
htmlcov/
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
repos:
|
|
2
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
3
|
+
rev: v0.16.0
|
|
4
|
+
hooks:
|
|
5
|
+
- id: ruff
|
|
6
|
+
name: ruff check
|
|
7
|
+
args: [--fix]
|
|
8
|
+
files: ^(src|tests|examples)/
|
|
9
|
+
- id: ruff-format
|
|
10
|
+
name: ruff format
|
|
11
|
+
files: ^(src|tests|examples)/
|
|
12
|
+
|
|
13
|
+
- repo: local
|
|
14
|
+
hooks:
|
|
15
|
+
- id: pyrefly
|
|
16
|
+
name: pyrefly check
|
|
17
|
+
entry: pyrefly check
|
|
18
|
+
language: system
|
|
19
|
+
pass_filenames: false
|
|
20
|
+
types: [python]
|
|
21
|
+
|
|
22
|
+
- id: pytest
|
|
23
|
+
name: pytest
|
|
24
|
+
entry: pytest tests -q
|
|
25
|
+
language: system
|
|
26
|
+
pass_filenames: false
|
|
27
|
+
types: [python]
|
|
28
|
+
stages: [pre-push]
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# Architecture internals
|
|
2
|
+
|
|
3
|
+
> **Internal engineering artifact.** This is a guided tour of ContextSage's
|
|
4
|
+
> internal pipeline stages/classes, kept for contributors and maintainers
|
|
5
|
+
> debugging summarization behavior. It is intentionally **not** part of the
|
|
6
|
+
> published documentation site (`docs/`) — per the project's design
|
|
7
|
+
> principle, users of `IntelligentSummarizationMiddleware` should never
|
|
8
|
+
> need to know about `ContextUnit`, engines, or any of the internals below
|
|
9
|
+
> to use the package. See [`docs/architecture.md`](docs/architecture.md)
|
|
10
|
+
> for the user-facing architecture overview, and
|
|
11
|
+
> [`docs/api-reference.md`](docs/api-reference.md) for the supported
|
|
12
|
+
> public surface.
|
|
13
|
+
|
|
14
|
+
This page is a guided tour of ContextSage's internal pipeline stages. These
|
|
15
|
+
classes are implementation details (see the module docstrings, or
|
|
16
|
+
`docs/api-reference.md`) but are documented for contributors and anyone
|
|
17
|
+
debugging summarization behavior.
|
|
18
|
+
|
|
19
|
+
## `ContextObserver`
|
|
20
|
+
|
|
21
|
+
`contextsage.context.observer.ContextObserver` — the entry point. Counts
|
|
22
|
+
tokens for the full incoming message list and produces an observation used
|
|
23
|
+
by the budget analyzer; this is the cheapest possible pre-check so the
|
|
24
|
+
expensive pipeline stages below only run when actually needed.
|
|
25
|
+
|
|
26
|
+
## `BudgetAnalyzer`
|
|
27
|
+
|
|
28
|
+
`contextsage.budget.analyzer.BudgetAnalyzer` implements an explicit,
|
|
29
|
+
inspectable budget model:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
maximum_context_tokens
|
|
33
|
+
reserved_output_tokens
|
|
34
|
+
system_tokens
|
|
35
|
+
tool_schema_tokens
|
|
36
|
+
current_input_tokens
|
|
37
|
+
safety_margin_tokens
|
|
38
|
+
available_input_tokens
|
|
39
|
+
overflow_tokens
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`infer_max_context_tokens(model)` looks up known context-window sizes for
|
|
43
|
+
common providers/models, falling back to a conservative default when the
|
|
44
|
+
model is unrecognized.
|
|
45
|
+
|
|
46
|
+
## `ContextDecomposer`
|
|
47
|
+
|
|
48
|
+
`contextsage.context.decomposition.ContextDecomposer` converts a heterogeneous
|
|
49
|
+
message list into a flat list of `ContextUnit`s. It never
|
|
50
|
+
treats one message as one content type:
|
|
51
|
+
|
|
52
|
+
1. Fenced code blocks (`` ``` ``) are extracted as `CODE` regions.
|
|
53
|
+
2. Embedded JSON values inside the remaining text are located with
|
|
54
|
+
`json.JSONDecoder.raw_decode`, which can find a JSON value starting at a
|
|
55
|
+
given index without needing to pre-guess where it ends.
|
|
56
|
+
3. Remaining text is segmented into contiguous log-like vs. plain-text runs.
|
|
57
|
+
|
|
58
|
+
## `StructuralSignalDetector`
|
|
59
|
+
|
|
60
|
+
`contextsage.classification.signals.StructuralSignalDetector`
|
|
61
|
+
assigns a `RegionKind` (or custom string kind — see below) and a confidence
|
|
62
|
+
score to each unit, plus lightweight `ContentSignals` (identifiers found,
|
|
63
|
+
whether an error/severity marker is present, table-likeness, etc.).
|
|
64
|
+
Detection is conservative by design: e.g. log-line detection requires a
|
|
65
|
+
leading timestamp pattern, so unlabeled plain text is never misclassified
|
|
66
|
+
as a log purely because it contains the word "error".
|
|
67
|
+
|
|
68
|
+
Kind-detection itself is delegated to a pluggable
|
|
69
|
+
`contextsage.parsers.ParserRegistry` — an ordered collection of
|
|
70
|
+
`ContentParser` implementations, tried in turn, first non-`None` result
|
|
71
|
+
wins. The built-in registry (`default_registry()`) contains:
|
|
72
|
+
|
|
73
|
+
1. `JSONParser` / `TableParser` — the most structurally unambiguous, so
|
|
74
|
+
they run first.
|
|
75
|
+
2. `CodeParser` — fenced code blocks are trusted directly; a stated
|
|
76
|
+
language tag or a line-shape heuristic match is then *validated/refined*
|
|
77
|
+
with real `tree-sitter` parsing (a core dependency), never blind-guessed.
|
|
78
|
+
3. `LogParser` / `ErrorParser` — anchored to the public ELK/Logstash "grok"
|
|
79
|
+
pattern vocabulary (`contextsage.parsers.grok_patterns`) rather than ad hoc
|
|
80
|
+
regexes; `ErrorParser` runs last since it deliberately overlaps with
|
|
81
|
+
ERROR-severity log lines.
|
|
82
|
+
|
|
83
|
+
Every built-in parser's default patterns/thresholds are overridable via
|
|
84
|
+
constructor keyword arguments (e.g. `LogParser(timestamp_pattern=...)`),
|
|
85
|
+
and a fully custom `ParserRegistry` can be passed to
|
|
86
|
+
`StructuralSignalDetector(registry=...)` — an internal-but-supported
|
|
87
|
+
extension point, not a public plugin-registration API.
|
|
88
|
+
|
|
89
|
+
## `ImportanceEngine`
|
|
90
|
+
|
|
91
|
+
`contextsage.importance.engine.ImportanceEngine` computes a
|
|
92
|
+
composite, bounded `[0, 1]` importance score per unit from independent
|
|
93
|
+
signals — recency, user corrections, active constraints, decision language,
|
|
94
|
+
error severity, identifiers (especially identifiers co-occurring with an
|
|
95
|
+
error), and repetition-group membership. Every contribution is recorded in
|
|
96
|
+
a `reasons` tuple so behavior is auditable and testable, not an opaque
|
|
97
|
+
score.
|
|
98
|
+
|
|
99
|
+
## `RelationshipEngine`
|
|
100
|
+
|
|
101
|
+
`contextsage.relationships.engine.RelationshipEngine`
|
|
102
|
+
detects `Relationship`s between units: `CONTRADICTS` (same key, conflicting
|
|
103
|
+
values, e.g. two RAG sources disagreeing), `CORRECTED_BY` (a user
|
|
104
|
+
correction referencing an earlier value), and tool-call pairing via
|
|
105
|
+
`pair_tool_calls` (an `AIMessage`'s `tool_calls` matched to their
|
|
106
|
+
`ToolMessage` results by `tool_call_id`), which the planner and validator
|
|
107
|
+
use to keep AI-tool-call / tool-result pairs intact together.
|
|
108
|
+
|
|
109
|
+
## `PreservationEngine`
|
|
110
|
+
|
|
111
|
+
`contextsage.preservation.engine.PreservationEngine` converts
|
|
112
|
+
importance + relationships into an explicit `PreservationLevel`
|
|
113
|
+
(`MUST_PRESERVE`, `SHOULD_PRESERVE`, `COMPRESSIBLE`, `REDUNDANT`,
|
|
114
|
+
`SAFE_TO_DROP`) per unit, and extracts concrete literal
|
|
115
|
+
`must_preserve_facts` (identifiers, correction referents, constraint
|
|
116
|
+
sentences) that `SummaryValidator` later checks the produced summary text
|
|
117
|
+
for. Its thresholds are tunable via the `policy` constructor argument
|
|
118
|
+
(`"balanced"` / `"maximum_preservation"` / `"maximum_compression"`) — see
|
|
119
|
+
[`docs/configuration.md`](docs/configuration.md#policies). Certain reasons (user
|
|
120
|
+
correction, contradiction participant, identifier-in-error-context) are
|
|
121
|
+
*always* `MUST_PRESERVE` regardless of policy, because they are
|
|
122
|
+
structurally important, not just numerically important.
|
|
123
|
+
|
|
124
|
+
## `SummarizationPlanner`
|
|
125
|
+
|
|
126
|
+
`contextsage.planning.planner.SummarizationPlanner` is the
|
|
127
|
+
core intelligence: given classified units, it decides which units become
|
|
128
|
+
`transform_deterministic` targets (structured/log/table content with a
|
|
129
|
+
compressible/redundant preservation level), which are routed to
|
|
130
|
+
`semantic_summarize` (left for the LLM), and which are `preserve_untouched`
|
|
131
|
+
(must/should-preserve content, or content belonging to a protected
|
|
132
|
+
message).
|
|
133
|
+
|
|
134
|
+
## `ContextTransformationEngine`
|
|
135
|
+
|
|
136
|
+
`contextsage.transformation.engine.ContextTransformationEngine`
|
|
137
|
+
applies per-kind deterministic transformers
|
|
138
|
+
(`contextsage.transformation.logs.reduce_log_block` for repetitive/verbose log
|
|
139
|
+
blocks, `contextsage.transformation.structured.compact_json` for JSON) only to
|
|
140
|
+
units the planner explicitly targeted. If a transformer raises
|
|
141
|
+
`TransformationError`, or produces a result that isn't actually smaller, the
|
|
142
|
+
unit is left completely untouched — deterministic transformation is always
|
|
143
|
+
an optimization, never a requirement.
|
|
144
|
+
|
|
145
|
+
## `LangGraphSummarizationAdapter`
|
|
146
|
+
|
|
147
|
+
`contextsage.integrations.langgraph.adapter.LangGraphSummarizationAdapter`
|
|
148
|
+
wraps LangChain's `SummarizationMiddleware` directly — it
|
|
149
|
+
does not fork it or reimplement LLM summarization. It configures the
|
|
150
|
+
wrapped middleware with an always-eligible trigger (ContextSage has already
|
|
151
|
+
decided summarization is required upstream) and the user's `keep`
|
|
152
|
+
parameter, then invokes its `before_model`/`abefore_model` hooks against
|
|
153
|
+
the prepared (post-transformation) message list.
|
|
154
|
+
|
|
155
|
+
## `SummaryValidator`
|
|
156
|
+
|
|
157
|
+
`contextsage.validation.validator.SummaryValidator` checks
|
|
158
|
+
the produced summary text for every literal must-preserve fact, for tool
|
|
159
|
+
call/result pairing integrity, and for contradiction survival (both
|
|
160
|
+
conflicting values must be simultaneously discoverable — a summary that
|
|
161
|
+
silently drops both sides of a contradiction, or collapses to just one
|
|
162
|
+
side, fails validation just as surely as one that fabricates false
|
|
163
|
+
certainty).
|
|
164
|
+
|
|
165
|
+
## `RecoveryManager`
|
|
166
|
+
|
|
167
|
+
`contextsage.recovery.manager.RecoveryManager` implements
|
|
168
|
+
the staged recovery process: on validation failure, it restates missing
|
|
169
|
+
facts in an appended message without re-invoking the LLM; on a hard
|
|
170
|
+
summarization failure (timeout, malformed response, rate limit), it falls
|
|
171
|
+
back to a deterministic trim of the most recent `keep` messages with a
|
|
172
|
+
preserved-facts message prepended, and never silently discards
|
|
173
|
+
`MUST_PRESERVE` content.
|
|
174
|
+
|
|
175
|
+
## `LineageManager`
|
|
176
|
+
|
|
177
|
+
`contextsage.lineage.manager.LineageManager` records a
|
|
178
|
+
`SummaryLineage` per operation (source unit/message IDs, generation number,
|
|
179
|
+
token counts, compression ratio, validation/recovery/fallback status) and
|
|
180
|
+
`find_prior_summary_ids` walks prior `RemoveMessage`/summary markers so a
|
|
181
|
+
summary-of-a-summary retains lineage back to its original sources, rather
|
|
182
|
+
than degrading silently across repeated summarizations.
|
|
183
|
+
|
|
184
|
+
## `ObservabilityHook`
|
|
185
|
+
|
|
186
|
+
`contextsage.observability.events.ObservabilityHook` is the
|
|
187
|
+
structured-events interface. `LoggingObservabilityHook` (default) logs a
|
|
188
|
+
`SummarizationEvent` per operation at `INFO` level — trigger reason, token
|
|
189
|
+
counts, selected targets, compression ratio, validation/recovery/fallback
|
|
190
|
+
status, latency — and never logs raw message content. `NullObservabilityHook`
|
|
191
|
+
disables this. Supply your own `ObservabilityHook` implementation to forward
|
|
192
|
+
events to your own telemetry system.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0] - 2026-09-23
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Initial public release of `contextsage`.
|
|
15
|
+
- `IntelligentSummarizationMiddleware`: a production-grade, information-aware
|
|
16
|
+
replacement for LangGraph's `SummarizationMiddleware`.
|
|
17
|
+
- Heterogeneous context decomposition (`context/decomposition.py`) — a
|
|
18
|
+
single message is split into independently-classified regions instead of
|
|
19
|
+
being treated as one content type.
|
|
20
|
+
- Pluggable structural content parsers (JSON, table, code, log, error) via
|
|
21
|
+
`ContentParser` / `ParserRegistry`, with a public `parsers` constructor
|
|
22
|
+
parameter for domain-specific extensions (see `examples/sql_parser.py`).
|
|
23
|
+
- Budget analysis, importance scoring, relationship/dependency tracking,
|
|
24
|
+
preservation classification, planning, and deterministic transformation
|
|
25
|
+
engines feeding a single LangGraph summarization call.
|
|
26
|
+
- Post-summarization validation and staged recovery on summarization
|
|
27
|
+
failure.
|
|
28
|
+
- Summary lineage tracking and optional `langgraph-xai` provenance
|
|
29
|
+
integration.
|
|
30
|
+
- Structured observability events via a pluggable `ObservabilityHook`.
|
|
31
|
+
- Full parameter overridability: every internal default (token budget
|
|
32
|
+
fractions, encoding name/timeout/cache size, classification patterns,
|
|
33
|
+
parser patterns) is exposed through `IntelligentSummarizationMiddleware`'s
|
|
34
|
+
public constructor — no subclassing required for common customization.
|
|
35
|
+
- Documentation site (`mkdocs`), requirements traceability matrix, and a
|
|
36
|
+
runnable example suite (`examples/`) exercising every constructor
|
|
37
|
+
parameter against a real LLM.
|
|
38
|
+
|
|
39
|
+
[Unreleased]: https://github.com/smuniharish/contextsage/compare/v0.1.0...HEAD
|
|
40
|
+
[0.1.0]: https://github.com/smuniharish/contextsage/releases/tag/v0.1.0
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in
|
|
6
|
+
our community a harassment-free experience for everyone, regardless of
|
|
7
|
+
age, body size, visible or invisible disability, ethnicity, sex
|
|
8
|
+
characteristics, gender identity and expression, level of experience,
|
|
9
|
+
education, socio-economic status, nationality, personal appearance, race,
|
|
10
|
+
caste, color, religion, or sexual identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open,
|
|
13
|
+
welcoming, diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment include:
|
|
18
|
+
|
|
19
|
+
- Demonstrating empathy and kindness toward other people
|
|
20
|
+
- Being respectful of differing opinions, viewpoints, and experiences
|
|
21
|
+
- Giving and gracefully accepting constructive feedback
|
|
22
|
+
- Accepting responsibility and apologizing to those affected by our
|
|
23
|
+
mistakes, and learning from the experience
|
|
24
|
+
- Focusing on what is best not just for us as individuals, but for the
|
|
25
|
+
overall community
|
|
26
|
+
|
|
27
|
+
Examples of unacceptable behavior include:
|
|
28
|
+
|
|
29
|
+
- The use of sexualized language or imagery, and sexual attention or
|
|
30
|
+
advances of any kind
|
|
31
|
+
- Trolling, insulting or derogatory comments, and personal or political
|
|
32
|
+
attacks
|
|
33
|
+
- Public or private harassment
|
|
34
|
+
- Publishing others' private information, such as a physical or email
|
|
35
|
+
address, without their explicit permission
|
|
36
|
+
- Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Project maintainers are responsible for clarifying and enforcing our
|
|
42
|
+
standards of acceptable behavior and will take appropriate and fair
|
|
43
|
+
corrective action in response to any behavior that they deem
|
|
44
|
+
inappropriate, threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies within all community spaces (issues, pull
|
|
49
|
+
requests, discussions) and also applies when an individual is officially
|
|
50
|
+
representing the community in public spaces.
|
|
51
|
+
|
|
52
|
+
## Enforcement
|
|
53
|
+
|
|
54
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may
|
|
55
|
+
be reported to the maintainer listed in `pyproject.toml` (`authors`). All
|
|
56
|
+
complaints will be reviewed and investigated promptly and fairly.
|
|
57
|
+
|
|
58
|
+
## Attribution
|
|
59
|
+
|
|
60
|
+
This Code of Conduct is adapted from the
|
|
61
|
+
[Contributor Covenant](https://www.contributor-covenant.org), version 2.1,
|
|
62
|
+
available at
|
|
63
|
+
<https://www.contributor-covenant.org/version/2/1/code_of_conduct.html>.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Contributing to ContextSage
|
|
2
|
+
|
|
3
|
+
Thanks for your interest in contributing! ContextSage is a focused,
|
|
4
|
+
production-grade package — see [`REQUIREMENTS_TRACEABILITY.md`](REQUIREMENTS_TRACEABILITY.md)
|
|
5
|
+
for the architectural contract every change is expected to respect (in
|
|
6
|
+
particular: no public plugin architecture, no forced heavyweight
|
|
7
|
+
dependencies, and no new top-level product surface beyond
|
|
8
|
+
`IntelligentSummarizationMiddleware`), and
|
|
9
|
+
[`ARCHITECTURE_INTERNALS.md`](ARCHITECTURE_INTERNALS.md) for a guided tour
|
|
10
|
+
of the internal pipeline engines (`ContextDecomposer`, `ImportanceEngine`,
|
|
11
|
+
`SummarizationPlanner`, etc.) if you're extending or debugging one of them.
|
|
12
|
+
|
|
13
|
+
## Getting started
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
git clone https://github.com/smuniharish/contextsage.git
|
|
17
|
+
cd contextsage
|
|
18
|
+
pip install -e ".[dev,examples]"
|
|
19
|
+
pre-commit install
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Development workflow
|
|
23
|
+
|
|
24
|
+
Before opening a pull request, run the same checks CI runs
|
|
25
|
+
(`.github/workflows/ci.yml`):
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
ruff check src tests examples
|
|
29
|
+
ruff format --check src tests examples
|
|
30
|
+
pyrefly check
|
|
31
|
+
pytest tests -q
|
|
32
|
+
mkdocs build --strict
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`pre-commit run --all-files` runs the lint/format checks automatically on
|
|
36
|
+
every commit once installed.
|
|
37
|
+
|
|
38
|
+
## Making changes
|
|
39
|
+
|
|
40
|
+
- **Tests are required** for any behavioral change. Unit tests live in
|
|
41
|
+
`tests/unit/` (one file per engine), integration tests in
|
|
42
|
+
`tests/integration/`, and failure-injection tests in `tests/failure/`.
|
|
43
|
+
- **Public API changes** (anything on `IntelligentSummarizationMiddleware`'s
|
|
44
|
+
constructor) must update `docs/api-reference.md` and
|
|
45
|
+
`docs/configuration.md`, and should extend `examples/all_parameters.py`
|
|
46
|
+
to exercise the new parameter against a real LLM call.
|
|
47
|
+
- **New default values or hardcoded patterns/thresholds** should be
|
|
48
|
+
exposed as overridable constructor keyword arguments, following the
|
|
49
|
+
`DEFAULT_*` module-constant convention used throughout `src/contextsage/`.
|
|
50
|
+
- Keep [`REQUIREMENTS_TRACEABILITY.md`](REQUIREMENTS_TRACEABILITY.md) in
|
|
51
|
+
sync when a change affects a numbered requirement's implementation or
|
|
52
|
+
test coverage.
|
|
53
|
+
- Update `CHANGELOG.md` under `[Unreleased]` for any user-facing change.
|
|
54
|
+
|
|
55
|
+
## Reporting bugs / requesting features
|
|
56
|
+
|
|
57
|
+
Open a GitHub issue with a minimal reproduction (a message list and the
|
|
58
|
+
`IntelligentSummarizationMiddleware` constructor arguments used) where
|
|
59
|
+
possible.
|
|
60
|
+
|
|
61
|
+
## Security issues
|
|
62
|
+
|
|
63
|
+
Do not open a public issue for a security vulnerability — see
|
|
64
|
+
[SECURITY.md](SECURITY.md).
|
|
65
|
+
|
|
66
|
+
## Code of conduct
|
|
67
|
+
|
|
68
|
+
This project follows the [Code of Conduct](CODE_OF_CONDUCT.md).
|
|
@@ -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 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 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 those 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
|
|
141
|
+
the 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 S MUNI HARISH
|
|
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.
|