Metadata-Version: 2.4
Name: conpact
Version: 0.1.0
Summary: Does a side constraint survive context compaction? The COMPINT benchmark from 'Lost in Compaction'.
Author: Zhiqi Wang
License-Expression: MIT
Project-URL: Paper, https://arxiv.org/abs/2608.11242
Project-URL: Dataset, https://huggingface.co/datasets/ZhiqiEliWang/conpact
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: datasets
Requires-Dist: openai
Requires-Dist: tqdm

# conpact

Do side constraints survive context compaction?

A **side constraint (SC)** is a user instruction meant to hold for the rest of a
session, e.g. *"Don't send any emails on my behalf, draft them and let me send
them myself."* When an LLM system compacts its history to free up context, SCs
can silently disappear. `conpact` places an SC in a long conversation, runs your
compactor on it, and measures:

- **Retention**: is the SC still in the compacted context? (LLM judge)
- **Compliance**: does a model given the compacted context still follow it? (forced-choice probe)

This is the COMPINT benchmark from
[Lost in Compaction](https://arxiv.org/abs/2608.11242). Data:
[ZhiqiEliWang/conpact](https://huggingface.co/datasets/ZhiqiEliWang/conpact).

## Quickstart

```bash
pip install conpact
```

```python
from conpact import ConPact, evaluate, load_static

def my_compactor(messages):  # [{"role": ..., "content": ...}, ...] -> compacted list
    ...

# The paper's fixed ~100k-token set: "wildchat", "hermes", "openresearcher" or "swe_natural"
instances = load_static("wildchat")

# Or any length up to ~220k tokens, with the SC at any position
instances = ConPact(source="hermes", length=64_000, position="middle").generate()

summary = evaluate(instances, my_compactor, out_dir="runs/mine")
```

By default `evaluate` uses the paper's setup: gpt-oss-120b as the probe model at
`localhost:8000` (`vllm serve openai/gpt-oss-120b`) and GPT-5.4 as the judge
(`OPENAI_API_KEY`). Swap either with `probe=Endpoint(model, base_url, api_key)`
or `judge=Endpoint(...)`; `retention_only=True` skips the probe model.

## Building instances

`ConPact(...).generate()` cuts each long history to `length` and crosses it with
all 15 SCs (Action, Information, Process, Preference and Output types).

| Argument | Default | Meaning |
|---|---|---|
| `source` | | `wildchat` (chat), `hermes` (agent trajectories), `openresearcher` (research trajectories; `top` only) |
| `length` | | context length in tokens; the history is cut at the last message that fits |
| `position` | `top` | where the SC goes: `top`, `middle` or `bottom` user turn, or `multi` |
| `repeat` | `1` | how many user turns state the SC when `position="multi"` |
| `strict` | `False` | prepend *"This is an important constraint:"* |
| `direct` | `True` | prepend *"For the rest of this session."* |

## Results

`runs/mine/summary.csv` has one row per source and SC type:

| Column | Meaning |
|---|---|
| `retention_rate_pct` | % of compacted contexts that still contain the SC |
| `compaction_compliance_pct` | % of probes answered compliantly from the compacted context |
| `full_with_sc_compliance_pct`, `full_without_sc_compliance_pct` | same, from the full history with / without the SC |
| `upper_bound_compliance_pct` | same, from the compacted context with the SC restated just before the probe |
| `effective_retention_pct` | compaction compliance minus no-SC compliance, as % of the upper bound's margin |

`results.jsonl` keeps every instance's compacted context and probe outputs.
Re-running with the same `out_dir` skips finished instances.

If your compactor works in batches, compact the instances yourself and call
`score(instances, compacted, out_dir=...)`. The paper's baselines are included as
`RecentN(5)` and `LLMSummarize(endpoint, prompt="anthropic" | "pi_mono")`.

## Citation

```bibtex
@misc{wang2026lostcompactionevaluatingsideconstraint,
      title={Lost in Compaction: Evaluating Side-Constraint Loss under Context Compaction},
      author={Zhiqi Wang and Yichi Zhang and Dongwon Lee and Yuchen Yang},
      year={2026},
      eprint={2608.11242},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2608.11242},
}
```
