compression-safety — правило для Claude Code: как установить
Правило compression-safety для Claude Code блокирует запись маркеров lean-ctx обратно в файлы при использовании lean-ctx в shadow-mode.
Apache-2.0 ★ 3 956 обновлён 2026-10-29
.cursor/rules/compression-safety.mdcЧто делает compression-safety
Хуки lean-ctx сжимают вывод при чтении файлов, заменяя блоки строк на [lean-ctx: omitted N lines]. Если инструмент редактирования (StrReplace, Write) читает через хук, он видит сжатое содержимое и записывает маркеры на диск — файл молча ломается. Правило учит агента обходить сжатие переменной LEAN_CTX_DISABLED=1 при восстановлении из git, проверять размер файла после правки и не коммитить маркеры.
Встроенный pre-commit хук lean-ctx блокирует коммит с маркерами. Если коммит заблокирован — файл испорчен, восстанавливай LEAN_CTX_DISABLED=1 git restore --source=HEAD -- <file>. Для правок, которые ломаются через StrReplace, правило предписывает sed с той же переменной.
Правило живёт в .claude/rules/, .cursor/rules/, .clinerules/ или .devin/rules/ — зависит от IDE.
Какие задачи решает
- предотвратить запись маркеров lean-ctx в файлы
- восстановить файл после повреждения сжатым выводом
- обойти хуки lean-ctx при работе с git
- проверить файл на маркеры перед коммитом
Примеры запросов агенту
- Восстанови
src/utils.tsиз HEAD, отключив lean-ctx - Замени
oldFuncнаnewFuncвapp.jsчерезsed, чтобы обойти хуки - Проверь, есть ли в
config.jsonмаркеры[lean-ctx:
Как установить compression-safety
Вручную: положить файлы
.cursor/rules/compression-safety.mdcВо frontmatter: description, globs, alwaysApply
Вручную: положить файлы
.claude/rules/compression-safety.mdПоле paths во frontmatter ограничивает файлы: globs из источника → paths
AGENTS.md
Codex читает AGENTS.md: вставьте текст правила туда
Вручную: положить файлы
.github/instructions/compression-safety.instructions.mdПоле applyTo задаёт, к каким файлам применять: globs из источника → applyTo
Вручную: положить файлы
.devin/rules/compression-safety.mdВручную: положить файлы
.clinerules/compression-safety.mdПо шагам, с командами для каждой платформы: Cursor rules: файлы .mdc, четыре режима и замена .cursorrules
Вопросы о compression-safety
Как установить compression-safety в Claude Code?
Положи файл правила в .claude/rules/compression-safety.md.
Что делает compression-safety?
Учит агента обходить хуки lean-ctx переменной LEAN_CTX_DISABLED=1, чтобы не записывать маркеры сжатия обратно в файлы, и восстанавливать повреждённые файлы из git.
Когда нужно правило compression-safety?
Если в проекте включён lean-ctx в shadow-mode. Без правила агент может записать сжатый вывод на диск и молча уничтожить содержимое файла.
Исходный текст
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