# ctxCompacts oversized Pi `read` results before they hit context.
## Install / loadLoaded through the root `pi-ext` package.
See [../../README.md](../../README.md).
## Commands / tools / settingsPi commands:
none.
Pi tools:
none.
Pi hooks:
- `tool_result`:
watches builtin `read` results and may replace large full-file reads with a
bounded head/tail view.
Pi settings:
none.
Env vars:
```textPI_CTX_COMPACT_THRESHOLD=20000
PI_CTX_READ_OUTPUT_THRESHOLD=20000
```
`PI_CTX_COMPACT_THRESHOLD` wins if both are set.
## Behavior`ctx` does one thing:
1. watches `read` tool results
2. ignores errors and bounded reads with `limit` or `l`
3. compares UTF-8 byte length against threshold
4. if full read output exceeds threshold, returns:
- first 8 lines
- `[ctx compacted read:
...]` marker
- grep/read-offset instruction
- last 32 lines
5. leaves `bash`, `grep`, `find`, `ls`, and all other tools untouched
Default threshold:
`20000` bytes.
Invalid env values fall back to default.
`0` compacts any non-empty full read.
No DB.
No search tool.
No replacement tools.
No workspace writes.
Agents can recover hidden middle content with normal builtin tools:
```textgrep target path
read path offset=120 limit=40
```
Patch metadata preserves existing details and adds:
```json{ "ctxCompactedRead": true }
```