# Strategic Compact Skill

> **Intelligent context compaction timing for long sessions.**

---

## When to Compact

### Context Warning Signs

| Signal | Meaning |
|--------|---------|
| Tool calls taking longer | Context window filling up |
| Repeated information | Earlier context being forgotten |
| Inconsistent responses | Context fragmentation |
| Token count warnings | Approaching limits |

### Optimal Timing

```
┌─────────────────────────────────────────────────────────────┐
│              CONTEXT LIFECYCLE                              │
│                                                             │
│   Fresh ──────────────────────────────────────────► Full    │
│                                                             │
│   |         |              |              |                 │
│   Start     25%            50%            75%       100%    │
│             ↑              ↑              ↑                 │
│             Safe           Consider       Compact now!      │
│             zone           compacting     Critical zone     │
│                                                             │
└─────────────────────────────────────────────────────────────┘
```

---

## Pre-Compact Checklist

Before compacting, ensure:

- [ ] Current task state is clear
- [ ] Important decisions are documented
- [ ] Code changes are committed
- [ ] Session notes are saved

---

## What to Preserve

### Always Preserve
- Current task description
- Key decisions made
- File paths being worked on
- Error states and their fixes
- User preferences stated

### Safe to Forget
- Exploratory searches
- Failed approaches (unless relevant)
- Verbose tool outputs
- Intermediate debugging steps

---

## Session State Management

### Save State Before Compact

```json
// .claude/state/session.json
{
  "currentTask": "implementing user authentication",
  "filesModified": [
    "src/auth.py",
    "tests/test_auth.py"
  ],
  "decisions": [
    "Using JWT for tokens",
    "bcrypt for password hashing"
  ],
  "blockers": [],
  "nextSteps": [
    "Implement /login endpoint",
    "Add password reset"
  ]
}
```

### Reload After Compact

After context compaction:
1. Read session state file
2. Remind of current task
3. Continue from next steps

---

## Hooks Integration

### PreCompact Hook

Triggered before context compaction:

```json
{
  "hooks": {
    "PreCompact": [
      {
        "type": "command",
        "command": "bash .claude/scripts/save-session-state.sh"
      }
    ]
  }
}
```

### Session Reload

After compaction, the assistant should:
1. Check for session state file
2. Load context from state
3. Announce current position

---

## Manual Compaction Triggers

When to manually request compaction:

1. **Task boundary** - Finished one task, starting another
2. **Context shift** - Moving to different area of codebase
3. **Debugging complete** - Solved issue, don't need debug history
4. **Before complex task** - Clear space for upcoming work

---

## Session Summary Template

Request summary before compact:

```markdown
## Session Summary

### Completed
- [x] Added User model with password hashing
- [x] Created /register endpoint
- [x] Added tests for registration

### In Progress
- [ ] Implement /login endpoint (50% done)

### Decisions Made
- JWT tokens with 1-hour expiry
- bcrypt with cost factor 12

### Files Modified
- src/models/user.py
- src/api/auth.py
- tests/test_auth.py

### Next Steps
1. Complete /login endpoint
2. Add password reset flow
```

---

## Related Skills

- [continuous-learning](../continuous-learning/) - Session pattern extraction
- [verification-loop](../verification-loop/) - Pre-commit checks
