Skip to main content
rule KR Kiro CX Codex

compression-safety — Claude Code rule: how to install

compression-safety blocks lean-ctx markers from being written back to files. Required if you run lean-ctx in shadow mode.

Apache-2.0 ★ 3,956 updated 2026-10-29

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/compression-safety.mdc
Open on GitHub

What compression-safety does

lean-ctx shadow-mode hooks compress file reads by replacing line blocks with [lean-ctx: omitted N lines]. If an edit tool (StrReplace, Write) reads through the hook, it sees compressed content and writes markers to disk, silently destroying the file. The rule teaches the agent to bypass compression with LEAN_CTX_DISABLED=1 when restoring from git, check file size after edits, and refuse to commit markers.

The built-in lean-ctx pre-commit hook blocks commits containing markers. Blocked commit means corrupted file: restore with LEAN_CTX_DISABLED=1 git restore --source=HEAD -- <file>. For edits that break through StrReplace, the rule prescribes sed with the same variable.

Place the rule in .claude/rules/, .cursor/rules/, .clinerules/ or .devin/rules/ depending on your IDE.

What it helps with

  • prevent lean-ctx markers from being written to files
  • restore a file after corruption by compressed output
  • bypass lean-ctx hooks when working with git
  • check a file for markers before committing

Example prompts for the agent

  • Restore src/utils.ts from HEAD with lean-ctx disabled
  • Replace oldFunc with newFunc in app.js using sed to bypass hooks
  • Check if config.json contains [lean-ctx: markers

How to install compression-safety

Manually: put the files in place

.cursor/rules/compression-safety.mdc

Frontmatter: description, globs, alwaysApply

Step by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules

Questions about compression-safety

How to install compression-safety in Claude Code?

Place the rule file in .claude/rules/compression-safety.md.

What does compression-safety do?

Teaches the agent to bypass lean-ctx hooks with LEAN_CTX_DISABLED=1 to prevent writing compression markers back to files, and to restore corrupted files from git.

When do I need compression-safety?

If lean-ctx is enabled in shadow mode in the project. Without the rule, the agent may write compressed output to disk and silently destroy file content.

Source text

compression-safety.mdc · Apache-2.0
# Compression Safety — Prevent lean-ctx Marker Corruption

## The problem

lean-ctx shadow-mode hooks compress file reads. If an edit tool (StrReplace, Write)
reads through the hook, it sees compressed content and writes markers like
`[lean-ctx: omitted N lines]` back to disk. This silently destroys file content.

## Rules for ALL agents

### Never write compressed content to files

The pre-commit hook blocks commits containing `[lean-ctx: omitted` markers.
If your commit is blocked, the file was corrupted. Fix:

```bash
LEAN_CTX_DISABLED=1 git restore --source=HEAD -- <file>
```

### File restoration commands

Always use `LEAN_CTX_DISABLED=1` when restoring files from git:

```bash
LEAN_CTX_DISABLED=1 git restore --source=<commit> -- <file>
LEAN_CTX_DISABLED=1 git checkout <commit> -- <file>
```

NEVER use `git show commit:file > file` — the shell redirect writes compressed output.

### When StrReplace produces unexpected results

If a file shrinks after StrReplace or contains `[lean-ctx:` text:
1. `LEAN_CTX_DISABLED=1 git restore --source=HEAD -- <file>`
2. Use `LEAN_CTX_DISABLED=1 sed -i '' 's/old/new/g' <file>` for the edit
3. Verify: `wc -l` + `rg '[lean-ctx:' <file>` must show 0

### Environment variable: `LEAN_CTX_DISABLED`

Set `LEAN_CTX_DISABLED=1` to bypass ALL lean-ctx compression for a command.
This is the escape hatch for any compression-related issue.

## NEVER do

- Pipe `git show` output to a file without `LEAN_CTX_DISABLED=1`
- Trust `wc -l` output that's significantly lower than expected
- Commit files containing `[lean-ctx: omitted` or `--- lean-ctx:` text

© yvgude · Apache-2.0 · yvgude/lean-ctx

Similar IDE rules

All IDE rules for Agent setup: memory, context, orchestration →

From the same set4

More in the catalog

Project sponsors

Companies that keep this analytics open