# Context preservation & auto-compact.

Long sessions hit token limits. team.management intercepts both events — approaching the limit, and the compaction itself — to save a checkpoint and restore it, so the workflow continues exactly where it left off instead of forgetting the task.

## Token monitoring

team.management’s `PostToolUse` hook monitors token usage for auto-compact after every tool call; as a fallback when auto-compact is off, the user-message hook warns once at 80% and again at 90%. With auto-compact on, compaction fires first — you rarely see the 90% warning.

## Auto-compact

`auto_compact.enabled` (default `true`) and `auto_compact.threshold` (default `85`%) live in `team-management/config.json`. At the threshold the context-compaction protocol triggers; the `PreCompact` hook fires first and writes a checkpoint — active task, branch, protocol step, and DAIC mode.

## Restoration

After a session restart, `session-start.py` reads `.claude/state/current_task.json` and injects a session-state summary — the task file, the protocol position, and the DAIC mode are all restored. After an in-session compaction, the user-message hook injects the same restoration on your next message. The agent picks up mid-protocol, not from scratch.

Both `.claude/state/current_task.json` and `.claude/state/daic-mode.json` are protected — the protocol engine and hooks own them; never edit by hand.
