$curl -o .claude/agents/context-engine.md https://raw.githubusercontent.com/Rune-kit/rune/HEAD/agents/context-engine.mdContext window management — tracks tool call count, auto-detects when context fills up, triggers state save + compaction. Artifact folding for large outputs (>4000 chars → file + preview).
| 1 | # context-engine |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Context window management for long sessions. Detects when context is approaching limits, triggers smart compaction preserving critical decisions and progress, and coordinates with session-bridge to save state before compaction. Prevents the common failure mode of losing important context mid-workflow. |
| 6 | |
| 7 | ### Behavioral Contexts |
| 8 | |
| 9 | Context-engine also manages **behavioral mode injection** via `contexts/` directory. Three modes are available: |
| 10 | |
| 11 | | Mode | File | When to Use | |
| 12 | |------|------|-------------| |
| 13 | | `dev` | `contexts/dev.md` | Active coding — bias toward action, code-first | |
| 14 | | `research` | `contexts/research.md` | Investigation — read widely, evidence-based | |
| 15 | | `review` | `contexts/review.md` | Code review — systematic, severity-labeled | |
| 16 | |
| 17 | **Mode activation**: Orchestrators (cook, team, rescue) can set the active mode by writing to `.rune/active-context.md`. The session-start hook injects the active context file into the session. Mode switches mid-session are supported — the orchestrator updates the file and references the new behavioral rules. |
| 18 | |
| 19 | **Default**: If no `.rune/active-context.md` exists, no behavioral mode is injected (standard Claude behavior). |
| 20 | |
| 21 | ## Triggers |
| 22 | |
| 23 | - Called by `cook` and `team` automatically at context boundaries |
| 24 | - Auto-trigger: when tool call count exceeds threshold or context utilization is high |
| 25 | - Auto-trigger: before compaction events |
| 26 | |
| 27 | ## Calls (outbound) |
| 28 | |
| 29 | # Exception: L3→L3 coordination |
| 30 | - `session-bridge` (L3): coordinate state save when context critical |
| 31 | |
| 32 | ## Called By (inbound) |
| 33 | |
| 34 | - `cook` (L1): Phase boundaries and when tool count exceeds thresholds |
| 35 | - `team` (L1): before parallel workstream dispatch, after merge |
| 36 | - `rescue` (L1): between refactoring sessions for state persistence |
| 37 | - `context-pack` (L3): when packaging context for sub-agent handoff |
| 38 | - `session-bridge` (L3): coordinates with context-engine for compaction timing |
| 39 | - `adversary` (L2): (oracle-mode) emit `context.preview` before bundle build to gate token cost |
| 40 | |
| 41 | ## Execution |
| 42 | |
| 43 | ### Step 1 — Count tool calls |
| 44 | |
| 45 | Count total tool calls made so far in this session. This is the ONLY reliable metric — token usage is not exposed by Claude Code and any estimate will be dangerously inaccurate. |
| 46 | |
| 47 | Do NOT attempt to estimate token percentages. Tool count is a directional proxy, not a precise measurement. |
| 48 | |
| 49 | ### Step 2 — Classify health |
| 50 | |
| 51 | Map tool call count to health level: |
| 52 | |
| 53 | ``` |
| 54 | GREEN (<50 calls) — Healthy, continue normally |
| 55 | YELLOW (50-80 calls) — Load only essential files going forward |
| 56 | ORANGE (80-120 calls) — Recommend /compact at next logical boundary |
| 57 | RED (>120 calls) — Trigger immediate compaction, save state first |
| 58 | ``` |
| 59 | |
| 60 | These thresholds are directional heuristics, not precise limits. Sessions with many large file reads may hit context limits earlier; sessions with mostly Grep/Glob may go longer. |
| 61 | |
| 62 | #### Large-File Adjustment |
| 63 | |
| 64 | Projects with large source files (Python modules often 500-1500 LOC, Java files similarly) consume significantly more context per `Read` call. If the session has read files averaging >500 lines, apply a 0.8x multiplier to all thresholds: |
| 65 | |
| 66 | ``` |
| 67 | Adjusted thresholds (large-file sessions): |
| 68 | GREEN (<40 calls) — Healthy, continue normally |
| 69 | YELLOW (40-65 calls) — Load only essential files going forward |
| 70 | ORANGE (65-100 calls) — Recommend /compact at next logical boundary |
| 71 | RED (>100 calls) — Trigger immediate compaction, save state first |
| 72 | ``` |
| 73 | |
| 74 | Detection: count `Read` tool calls that returned >500 lines. If ≥3 such calls → activate large-file thresholds for the remainder of the session. |
| 75 | |
| 76 | ### Step 3 — If YELLOW |
| 77 | |
| 78 | Emit advisory to the calling orchestrator: |
| 79 | |
| 80 | > "[X] tool calls. Load only essential files. Avoid reading full files when Grep will do." |
| 81 | |
| 82 | Do NOT trigger compaction yet. Continue execution. |
| 83 | |
| 84 | ### Step 4 — If ORANGE |
| 85 | |
| 86 | Emit recommendation to the calling orchestrator: |
| 87 | |
| 88 | > "[X] tool calls. Recommend /compact at next phase boundary (after current module completes)." |
| 89 | |
| 90 | Identify the next safe boundary (end of current loop iteration, end of current file being processed) and flag it. |
| 91 | |
| 92 | **Auto-activate Caveman Output Mode** (see `references/caveman-mode.md`) — emit `output.density.set` with `mode=caveman, scope=session, source=context-orange`. Reduces output token cost ~75% with no information loss; persists until /compact returns context to GREEN. Manual override (`/caveman` or "stop caveman") always wins. |
| 93 | |
| 94 | ### Step 5 — If RED |
| 95 | |
| 96 | Immediately trigger state save via `rune:session-bridge` (Save Mode) before any compaction occurs. If caveman mode was not already active |