|
1 | 1 | # AGENTS.md |
2 | 2 |
|
3 | | -**Version**: 2.1 (2025-10-25) | **Compatibility**: Claude, Cursor, Copilot, Cline, Aider, all AGENTS.md-compatible tools |
| 3 | +**Version**: 2.2 (2025-03-04) | **Compatibility**: Claude, Cursor, Copilot, Cline, Aider, all AGENTS.md-compatible tools |
4 | 4 | **Status**: Canonical single-file guide for AI-assisted development |
5 | 5 |
|
6 | 6 | --- |
|
9 | 9 |
|
10 | 10 | 1. [Compliance & Core Rules](#1-compliance--core-rules) |
11 | 11 | 2. [Session Startup](#2-session-startup) |
| 12 | + - [Compaction Protocol](#compaction-protocol-mid-session-context-preservation) |
12 | 13 | 3. [Memory Bank](#3-memory-bank) |
13 | 14 | 4. [State Machine](#4-state-machine) |
14 | 15 | 5. [Task Contract & Budgets](#5-task-contract--budgets) |
@@ -106,6 +107,45 @@ Append-only JSONL format: |
106 | 107 | {"timestamp":"2025-10-25T11:00:00Z","session_id":"uuid","event":"approval_requested","state":"APPROVAL"} |
107 | 108 | ``` |
108 | 109 |
|
| 110 | +### Compaction Protocol (Mid-Session Context Preservation) |
| 111 | + |
| 112 | +Compaction (context compression) can happen at any time — triggered by the system automatically, by the user via `/compact`, or by platform-level context management. **The agent does not control compaction timing and may not get advance notice.** Therefore, state persistence must be continuous, not deferred to a pre-compaction moment. |
| 113 | + |
| 114 | +#### Continuous State Persistence (At Every State Transition) |
| 115 | + |
| 116 | +At each state transition (`PLAN → BUILD → DIFF → QA → APPROVAL → APPLY → DOCS`), persist the following to the Memory Bank: |
| 117 | + |
| 118 | +1. **State machine position**: Update `activeContext.md` with current state, substate, and working context |
| 119 | +2. **Task progress**: Append current status to `tasks/YYYY-MM/README.md` with `[IN-PROGRESS]` tag |
| 120 | +3. **Decisions**: Append any new architectural decisions to `decisions.md` |
| 121 | +4. **Log transition** to operational log: |
| 122 | + ```json |
| 123 | + {"timestamp":"...","session_id":"uuid","event":"state_transition","from":"PLAN","to":"BUILD"} |
| 124 | + ``` |
| 125 | +5. **Loose context**: Capture any information that exists only in conversation (user preferences, verbal requirements, pending questions) into `activeContext.md` |
| 126 | + |
| 127 | +This ensures that when compaction occurs — without warning — the Memory Bank already reflects the latest state. |
| 128 | + |
| 129 | +#### After Compaction (Recovery) |
| 130 | + |
| 131 | +When context has been compressed (detected by loss of earlier conversation detail, or after `/compact`): |
| 132 | + |
| 133 | +1. Re-enter **Session Startup** (Section 2) using **Fast Track** mode — the Memory Bank was just updated via continuous persistence, so full discovery is unnecessary |
| 134 | +2. Confirm state machine position from `activeContext.md` |
| 135 | +3. Resume from saved state — do not restart the current task from scratch |
| 136 | +4. Output recovery confirmation: |
| 137 | + ``` |
| 138 | + COMPACTION RECOVERY: Resumed [STATE] for [task name] |
| 139 | + Context restored from: activeContext.md, tasks/YYYY-MM/README.md |
| 140 | + ``` |
| 141 | + |
| 142 | +#### Rules |
| 143 | + |
| 144 | +- State persistence happens at every transition, not "before compaction" — you cannot rely on advance notice |
| 145 | +- After detecting compaction, always re-read Memory Bank before taking any action |
| 146 | +- If the current state is `APPROVAL` or `DIFF`, the diff summary should already be in `activeContext.md` from the transition save |
| 147 | +- Compaction does not reset budgets — carry forward cycle/token/minute counts from the operational log |
| 148 | + |
109 | 149 | --- |
110 | 150 |
|
111 | 151 | ## 3. Memory Bank |
@@ -577,7 +617,7 @@ Create outline for approval. After approval, do work. Do not document until I ap |
577 | 617 | 2. **Task** (current task): Files being modified, direct dependencies, related tests |
578 | 618 | 3. **Reference** (on-demand): Arch patterns, similar implementations, historical decisions |
579 | 619 |
|
580 | | -**Context Rotation**: After each state transition, drop Task Context, reload only what's needed for next state. Keep Core Context persistent. |
| 620 | +**Context Rotation**: After each state transition, drop Task Context, reload only what's needed for next state. Keep Core Context persistent. State is persisted to Memory Bank at every transition per **Compaction Protocol** (Section 2), so compaction recovery is automatic. |
581 | 621 |
|
582 | 622 | **Parallel Execution**: |
583 | 623 | ``` |
@@ -896,7 +936,7 @@ Stuck? → Cycles ≥3? |
896 | 936 | | Issue | Symptoms | Resolution | |
897 | 937 | |-------|----------|------------| |
898 | 938 | | **Loop** | Same diff multiple times, QA fails repeatedly, no progress after 3+ cycles | Check budgets → Load more MB → Clarify requirements → Check environment → Agent swap | |
899 | | -| **Context Exceeded** | Token limit approaching, slow/truncated responses, forgetting earlier info | Rotate context (drop Task, reload essentials) → Focused mode (MB summaries only) → Break into subtasks → Agent swap | |
| 939 | +| **Context Exceeded** | Token limit approaching, slow/truncated responses, forgetting earlier info | State already persisted via **Compaction Protocol** (Section 2) → Rotate context (drop Task, reload essentials) → Focused mode (MB summaries only) → Break into subtasks → Agent swap | |
900 | 940 | | **CI ≠ Local** | QA passes, CI fails | Compare environments → Verify dependency versions → Check timing/concurrency → Check state cleanup → Document waiver if CI issue | |
901 | 941 | | **Security Fail** | Security checklist incomplete, sensitive data exposed, auth/authz bypassed | Never bypass → Return to BUILD → Fix all issues → Re-test → Document pattern if new | |
902 | 942 |
|
|
0 commit comments