How saminkhan1/context-compression works
1. Core purpose
This repository implements a deterministic “context selector” that optimizes structured files before an AI agent reads them. The key architectural constraint is losslessness: the selected representation must round-trip back to the parsed source value, and the source file itself is never rewritten.
2. Main runtime flow
The central flow is: an agent or hook detects a file read, calls the selector, the selector evaluates candidate encodings, verifies round-trip safety, and returns a context-selector/v1 report with a trusted read_path. Adapters then substitute that verified path for the original file when safe.
3. Selector engine
selector.py is the core decision engine and CLI. It parses supported inputs, generates candidate representations, counts tokens with the active model tokenizer, applies safety checks, and chooses the lowest-token valid candidate subject to savings thresholds. The report includes source path, selected format, token counts, hashes, and verification status.
4. Supported data boundary
The selector is intentionally limited to structured tabular/text data: JSON, JSONL, CSV, and TSV. Unsupported or semantically risky shell-style reads stay raw. The repo’s boundary is “structured file compression,” not general prompt compression or summarization.
5. Tokenizer and model resolution
Model metadata is resolved from adapter payloads, Codex config, environment variables, and a bundled model-catalog.snapshot.json. Tokenization is handled with tiktoken for OpenAI-compatible models, or a Hugging Face tokenizer.json when configured. If exact tokenization is unavailable, fallback counts are explicitly treated as estimates.
6. Adapter layer
adapters/ contains thin runtime integrations for different agent hosts: Claude Code, Pi, Hermes Agent, MCP, OpenClaw, and a generic contract document. These adapters do not implement compression logic themselves; they all delegate to the same selector/verifier contract and only vary in how they intercept reads or expose a tool.
7. Codex hook runtime
hook.py and run-hook.sh implement the conservative Codex hook path. The hook rewrites only safe whole-file Bash reads, writes optimized sidecars under .codex/context-cache/, and avoids adding explanatory text to the conversation. The README notes that some hook surfaces, like UserPromptSubmit, are intentionally no-ops because they cannot safely substitute pasted text or attachments.
8. Verification boundary
verify_selector_report.py and the test suite enforce the contract that adapters must trust only the verified read_path, not any raw output path. Round-trip safety, hash/auditability, and deterministic decision making are treated as first-class invariants.
9. Benchmark and evidence pipeline
benchmark.py builds corpora, runs token comparisons, and can compare against external codecs via reproducible baselines. Checked-in reports under reports/ and EVIDENCE.md capture the current claim surface: deterministic savings and round-trip correctness, with answer-parity evidence left as an optional evaluation path rather than a production guarantee.
10. Optional quality evals
The evals/ directory contains scripts to build paired raw/optimized datasets and verify dataset integrity for later Inspect AI runs. This is a separate research/evaluation lane, not part of the runtime selector path, and it is explicitly gated from being treated as answer-parity proof until model runs are completed.
11. Tests and operational checks
tests/ covers adapters, selector behavior, hooks, benchmark logic, usage stats, and smoke paths. scripts/ contains operational checks such as clean-install verification, harness contract verification, evidence verification, and usage-stat exports. This keeps the repository split between runtime logic and validation tooling.
12. Artifacts and local state
Generated content is expected to live outside versioned source: .codex/context-cache/, data/benchmark-corpus/, logs/, and virtualenv/cache directories. The architecture assumes source files remain immutable and all optimization is expressed through verified sidecars and report surfaces.
Main entry points: selector.py Selector · CONTRACT.md Adapter Contract · verify_selector_report.py Report Verify · context_selector_hook.py Claude Code
Components 18
Runtime
Core selector execution and hook integration
- Selector · Parses structured files, generates candidate encodings, verifies round-trip safety, and selects the lowest-token valid representation.
selector.py - Codex Hook · Conservative file-read interception path for safe whole-file substitutions and sidecar writes.
hook.py - Hook Runner · Shell wrapper that launches the hook runtime.
run-hook.sh
Adapters
Host-specific integrations that delegate to the selector contract
- Adapter Contract · Shared contract document for thin host adapters.
adapters/CONTRACT.md - Claude Code · Claude Code hook integration that delegates to the selector contract.
adapters/claude-code/context_selector_hook.py - Pi Adapter · TypeScript adapter for Pi host integration.
adapters/pi/context-selector-tool.ts - Hermes Plugin · Hermes Agent plugin wrapper around the selector contract.
adapters/hermes-plugin/__init__.py - MCP Server · MCP server exposing selector functionality as a tool.
adapters/mcp/context_selector_server.py - OpenClaw · OpenClaw integration that exposes the selector through a plugin package.
adapters/openclaw/index.ts
Validation
Verification, tests, and operational checks
- Report Schema · Defines the context-selector/v1 report shape and trusted read-path contract.
docs/schemas/context-selector-v1.schema.json - Report Verify · Checks selector reports and enforces trust in verified read paths only.
verify_selector_report.py - Usage Stats · Exports usage telemetry and operational statistics.
scripts/export_usage_stats.py - Clean Install · Verifies reproducible clean-install behavior.
scripts/verify_clean_install.py - Harness Checks · Verifies harness contracts and smoke paths.
scripts/verify_harness_contracts.py
Research
Benchmarking, evaluation, and evidence artifacts
- Benchmark · Builds corpora and measures token savings and codec baselines.
benchmark.py - Eval Build · Creates paired raw and optimized datasets for later quality runs.
evals/build_context_quality_dataset.py - Eval Check · Verifies dataset integrity and supports context-quality smoke paths.
evals/verify_context_quality_dataset.py - Benchmark Report · Checked-in benchmark results and summary claims.
reports/benchmark-report.json
Connections 20
- Codex Hook → Selector: invokes · Hook delegates file-read decisions to the selector engine.
- Hook Runner → Codex Hook: starts · Shell wrapper launches the hook runtime.
- Selector → Report Schema: emits · Selector returns a context-selector/v1 report matching the schema.
- Report Verify → Report Schema: validates · Verifier enforces the report contract and trusted read-path semantics.
- Report Verify → Selector: checks · Verification asserts deterministic, round-trip-safe selector behavior.
- Claude Code → Adapter Contract: follows · Adapter implements the shared host contract.
- Pi Adapter → Adapter Contract: follows · Adapter implements the shared host contract.
- Hermes Plugin → Adapter Contract: follows · Adapter implements the shared host contract.
- MCP Server → Adapter Contract: follows · Adapter implements the shared host contract.
- OpenClaw → Adapter Contract: follows · Adapter implements the shared host contract.
- Claude Code → Selector: delegates · Host integration relies on the same selection engine.
- Pi Adapter → Selector: delegates · Host integration relies on the same selection engine.
- Hermes Plugin → Selector: delegates · Host integration relies on the same selection engine.
- MCP Server → Selector: delegates · Host integration relies on the same selection engine.
- OpenClaw → Selector: delegates · Host integration relies on the same selection engine.
- Benchmark → Benchmark Report: writes · Benchmark pipeline produces checked-in evidence outputs.
- Eval Build → Eval Check: feeds · Dataset builder output is verified before evaluation use.
- Harness Checks → Report Verify: tests · Operational checks exercise the report verification boundary.
- Clean Install → Selector: checks · Sanity checks cover the runtime installation path.
- Usage Stats → Benchmark Report: summarizes · Usage metrics inform evidence and reporting surfaces.