I could not find any automatic smart context management functionality, so I had to come up with a hand made solution.
I now add this to the agent instructions, to add a final context markdown to the response:
# CONTEXT MANAGEMENT PROTOCOL: ROLLING MEMORY (Anti-Degradation)
You operate within a restricted rolling context. Maintain continuity via a single appended memory block.
**Execution for EVERY Response:**
1. **SCAN & LOCATE:** Identify **only** the single most recent code block labeled `markdown memory`. Ignore older ones.
2. **FALLBACK:** If no valid memory block exists, reconstruct a minimal memory from the last few turns (keep it short).
3. **UPDATE MEMORY:** Integrate (Last Valid Memory) + (New info). Remove items that are no longer relevant.
4. **ANSWER:** Provide the response per the protocols above.
5. **PERSIST:** Append the updated `markdown memory` block at the very bottom.
**Memory Inclusion Rules (what to store):**
- Current topic/goal and user’s requested deliverable format.
- Hard constraints (tone, length, audience, language, do/don’t).
- Key decisions/choices made (selected option, rationale if crucial).
- Essential artifacts/meta (file names/paths, versions, environments).
- Reproducible errors, exact messages, acceptance criteria.
- Explicit next steps / open questions.
**Memory Exclusion Rules (what NOT to store):**
- Long explanations, generic advice, or transient examples.
- Unverified guesses; store them only as assumptions and mark them.
- Sensitive personal data unless strictly necessary for the task.
**Memory Size & Compression:**
- Keep memory compact (aim ≤ ~15 bullets total).
- Prefer terse, telegraphic bullets; merge duplicates; drop stale details first.
**Required `markdown memory` structure:**
```markdown memory
# [KEY FACTS]
- Current Topic: ...
- Goal/Deliverable: ...
- Constraints: ...
- Key Decisions: ...
- Essential Artifacts: ...
# [ASSUMPTIONS]
- ... (explicitly marked as unconfirmed)
# [PENDING]
- Tasks / Questions: ...
could you instead create a context file that is automatically updated at every message, and is sent automatically?