# ctx Compacts oversized Pi `read` results before they hit context. ## Install / load Loaded through the root `pi-ext` package. See [../../README.md](../../README.md). ## Commands / tools / settings Pi commands: none. Pi tools: none. Pi hooks: - `tool_result`: watches builtin `read` results and may replace large read output with a bounded head/tail view. Pi settings: none. Env vars: ```text PI_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 reads with `limit` or `l` 3. compares UTF-8 byte length against threshold 4. if 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 read. No DB. No search tool. No replacement tools. No workspace writes. Agents can recover hidden middle content with normal builtin tools: ```text grep target path read path offset=120 limit=40 ``` Patch metadata preserves existing details and adds: ```json { "ctxCompactedRead": true } ```