|
| 1 | +--- |
| 2 | +title: Context Engineering for Coding Agents |
| 3 | +description: Prompt packing, retrieval, and compaction patterns for reliable agent work. |
| 4 | +alwaysApply: true |
| 5 | +priority: 15 |
| 6 | +--- |
| 7 | + |
| 8 | +# Context Engineering for Coding Agents |
| 9 | + |
| 10 | +This rule strengthens agent performance by making context **intentional**, **minimal**, and **reproducible**. |
| 11 | + |
| 12 | +> [!NOTE] |
| 13 | +> This rule complements `010-workflow.mdc` (phases + context files) and `100-core.mdc` (anti-slop guardrails). |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Goals |
| 18 | + |
| 19 | +- Keep the agent’s working context small and high-signal |
| 20 | +- Make work resumable across sessions without “reloading the world” |
| 21 | +- Prefer retrieval over memory: fetch exactly what you need, when you need it |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Mental Model: Three Layers of Context |
| 26 | + |
| 27 | +- **Working context (volatile)**: what the model sees right now |
| 28 | +- **Repo context (retrievable)**: files, diffs, logs, docs that can be re-read on demand |
| 29 | +- **Durable context (portable)**: short summaries + decisions captured in `extras/` context files |
| 30 | + |
| 31 | +> [!IMPORTANT] |
| 32 | +> If it’s important and will matter later, it must be captured in **durable context**. |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +## Prompt Packing (What to Carry vs. What to Fetch) |
| 37 | + |
| 38 | +### Always carry (paste/recap) |
| 39 | + |
| 40 | +- **Goal**: one sentence |
| 41 | +- **Non-goals**: 1–3 bullets |
| 42 | +- **Constraints**: security, tooling, performance, “don’t refactor”, etc. |
| 43 | +- **Current plan**: 3–7 steps max |
| 44 | +- **Current state**: what’s done / what’s blocked |
| 45 | +- **Critical artifacts**: exact file paths, key identifiers, and error messages (verbatim) |
| 46 | + |
| 47 | +### Never carry (fetch instead) |
| 48 | + |
| 49 | +- Entire files or long logs “just in case” |
| 50 | +- Repeated restatements of requirements |
| 51 | +- Unbounded command output without a reason |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## Retrieval Strategy (Search → Read Narrow → Cite) |
| 56 | + |
| 57 | +Use a tight loop: |
| 58 | + |
| 59 | +1. **Search** for the smallest set of candidate files (prefer repo-local sources first) |
| 60 | +2. **Read** only the minimal sections needed to decide or implement |
| 61 | +3. **Cite** exact file paths / relevant snippets in your reasoning and updates |
| 62 | + |
| 63 | +> [!TIP] |
| 64 | +> Prefer “retrieve just-in-time” over “load everything up-front”. It is faster and more accurate. |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Compaction Strategy (Frequent Intentional Compaction) |
| 69 | + |
| 70 | +After any meaningful milestone (plan approved, a module completed, a bug fixed), compact: |
| 71 | + |
| 72 | +- Update `extras/active-context.md` with: |
| 73 | + - **Current focus** |
| 74 | + - **Key decisions** (what and why) |
| 75 | + - **Blockers** (and next action) |
| 76 | + - **Context from previous sessions** (only what must persist) |
| 77 | +- Update `extras/progress.md` (if used) with “done/next” |
| 78 | +- Add a **Handoff Bundle** (copy/pasteable) for resuming work |
| 79 | + |
| 80 | +### Handoff Bundle (copy/paste) |
| 81 | + |
| 82 | +```text |
| 83 | +Goal: |
| 84 | +Non-goals: |
| 85 | +Constraints: |
| 86 | +Current state: |
| 87 | +Next 3 actions: |
| 88 | +Key files/paths: |
| 89 | +Key commands + outputs (verbatim, minimal): |
| 90 | +``` |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +## Context Reset Triggers (Start Fresh on Purpose) |
| 95 | + |
| 96 | +Prefer a fresh chat/session when: |
| 97 | + |
| 98 | +- Plan is approved and you’re moving into implementation (see `010-workflow.mdc`) |
| 99 | +- The conversation has accumulated long tool output or multiple competing threads |
| 100 | +- You are switching to a new, independent task |
| 101 | + |
| 102 | +When restarting, bring only: |
| 103 | + |
| 104 | +- The approved plan (or the updated `extras/tasks.md`) |
| 105 | +- The Handoff Bundle |
| 106 | +- The minimal file/line references needed to continue |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## Guardrails (Quality + Security) |
| 111 | + |
| 112 | +- Follow `100-core.mdc` anti-slop rules: be minimal, anchored, specific |
| 113 | +- Never paste secrets into context; redact tokens/keys/passwords |
| 114 | +- Prefer deterministic artifacts: file paths, commands, exit codes, and diffs |
| 115 | + |
0 commit comments