# Context Pruning Problem — For Claude Code Local

*Created: 2026-02-25 19:52 AEDT*
*Purpose: Problem description for Claude Code Local to investigate*

---

## The Problem

Our Clawdbot multi-agent fleet loses conversation context after periods of inactivity. When Michael comes back after ~1 hour idle, the agent has lost its memory of the previous conversation. This costs extra tokens (re-caching the full prompt) and breaks continuity.

## What's Happening

Clawdbot has a feature called **context pruning** (`contextPruning` in config). It trims old messages from the conversation before each LLM API call to manage context window size and cache costs.

The mode we use is `cache-ttl`:
- It checks: "When was the last API call for this session?"
- If that time exceeds the configured TTL, it prunes old tool results and compacts messages
- After pruning, the full prompt has to be re-cached with the LLM provider (expensive)

## Current Config Across All Agents

| Agent | Port | TTL | Version |
|-------|------|-----|---------|
| **Rivet** | 18789 | 8h (just patched today, was 1h) | 2026.1.24-3 |
| **Builder** | 18790 | 3h | 2026.1.24-3 |
| **Susan** | 18792 | 2h | 2026.1.24-3 |
| **Harper** | 18796 | 2h | 2026.1.24-3 |
| **Sentinel** | 18800 | 2h | 2026.1.24-3 |
| **Radar** | 18804 | 2h | 2026.1.24-3 |
| **Herald** | 18808 | 2h | 2026.1.24-3 |
| **Cog** | 18812 | 2h | 2026.1.24-3 |

## The Bug (Upstream, Version-Dependent)

In Clawdbot version `2026.1.24-3` (what we're running), there's a known bug:

> **cache-ttl mode is silently skipped for non-Anthropic providers** (Kimi, DeepSeek, MiniMax, etc.)

This means: when a session or cron job uses a Kimi or DeepSeek model, the `cache-ttl` pruning mode does nothing. It silently falls through. The context either doesn't get pruned at all (memory bloat) or gets pruned by a different mechanism entirely.

**This was fixed in a newer Clawdbot version** (around 2026.2.17):
> "Extend cache-ttl eligibility to Moonshot/Kimi and ZAI/GLM providers (including OpenRouter model refs), so contextPruning.mode: 'cache-ttl' is no longer silently skipped for those sessions."

## What Needs Investigating

1. **How does `cache-ttl` mode actually work in the code?**
   - Where is the TTL comparison done?
   - What happens when pruning triggers vs. doesn't trigger?
   - How does it interact with Anthropic's server-side cache (max 1 hour)?

2. **The provider eligibility bug:**
   - Where in the code does it check provider before applying cache-ttl?
   - Why was it limited to Anthropic only in the first place?
   - Does the fix in newer versions actually resolve it, or is it a partial fix?

3. **Subagent model fallback bug (related):**
   - GitHub issues #3237 and #10883
   - Subagents hardcode fallback to `DEFAULT_PROVIDER` (anthropic) in `defaults.js`
   - Even when you specify `model: "kimi"`, the subagent ignores it and uses Anthropic
   - This means subagent model overrides are broken

4. **The 8h TTL question:**
   - Anthropic's server-side prompt cache has max 1h retention
   - Setting Clawdbot's TTL to 8h means: don't prune for 8h (keeps more context locally)
   - But Anthropic's cache still expires after 1h idle, so re-caching cost is unavoidable
   - Is there a way to keep the Anthropic cache warm? (heartbeats?)

5. **What should TTL actually be set to across all agents?**
   - Michael wants 8h for all
   - But does 8h actually help if Anthropic's cache expires at 1h regardless?
   - The benefit of 8h: more context preserved (tool results, conversation history not pruned)
   - The cost: larger context window = more tokens per API call

## Config Files

All agent configs are at:
- Rivet: `/root/.clawdbot/clawdbot.json`
- Builder: `/root/.clawdbot-builder/clawdbot.json`
- Susan: `/root/.clawdbot-susan/clawdbot.json`
- Harper: `/root/.clawdbot-harper/clawdbot.json`
- Sentinel: `/root/.clawdbot-sentinel/clawdbot.json`
- Radar: `/root/.clawdbot-radar/clawdbot.json`
- Herald: `/root/.clawdbot-herald/clawdbot.json`
- Cog: `/root/.clawdbot-cog/clawdbot.json`

## Clawdbot Source Code

Installed at: `/usr/lib/node_modules/clawdbot/`

Key areas to investigate:
- Context pruning logic (search for `cache-ttl`, `contextPruning`, `pruning`)
- Provider eligibility checks (search for provider checks near pruning)
- Subagent model resolution (search for `DEFAULT_PROVIDER`, `defaults.js`, subagent spawn)
- Cache control headers/parameters sent to Anthropic API

## The Fix Plan (Once We Understand)

1. **Update Clawdbot** from 2026.1.24-3 to latest (2026.2.24+) — fixes the provider eligibility bug
2. **Set all agents to 8h TTL** — preserves context across Michael's workday
3. **Clean up model aliases** — remove broken direct provider routes, keep OpenRouter
4. **Increase contextTokens** from 200k toward 1M (Opus 4.6 supports it)
5. **Verify heartbeat intervals** keep caches warm within the 1h Anthropic window

---

*Context: This is a fleet of 8 AI agents running on a DigitalOcean VPS (2 vCPU, 8GB RAM). Each agent is a separate Clawdbot gateway instance. They coordinate via shared files and gateway-to-gateway API calls.*
